CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2011
    Posts
    20

    Proper way to get Rid of breaks (iForgot)

    Hello all!


    I got the following cod And I absolutley forgot how to get rid of breakse:

    Code:
    		for (int xTemp = 0; xTemp < board.length; xTemp++) {
    					if (board[xTemp][0] == curpos[0][0] && board[xTemp][1] == (curpos[0][1] + 66) || nopos[xTemp][0] == curpos[0][0] && nopos[xTemp][1] == (curpos[0][1] + 66)) {
    						curpos[0][1] = curpos[0][1] + 66;
    						repaint();
    						break;
    					}
    				}
    More of crappy code with breaks:

    Code:
    	for (xTemp = 0; xTemp < board.length; xTemp++) {
    				// Rechts
    				if ((isBoardPos(board[xTemp][0] + 66, board[xTemp][1]) == true)
    						&& (isEmptyPos(board[xTemp][0] + 66 * 2,
    								board[xTemp][1]) == true)) {
    					LOG("Is not lost");
    					rechts = true;
    					break;
    				}
    				// Links
    				else if ((isBoardPos(board[xTemp][0] - 66, board[xTemp][1]) == true)
    						&& (isEmptyPos(board[xTemp][0] - 66 * 2,
    								board[xTemp][1]) == true)) {
    					LOG("Is not lost");
    					links = true;
    					break;
    				}
    				// Boven
    				else if ((isBoardPos(board[xTemp][0], board[xTemp][1] + 66) == true)
    						&& (isEmptyPos(board[xTemp][0],
    								board[xTemp][1] + 66 * 2) == true)) {
    					LOG("Is not lost");
    					boven = true;
    					break;
    				}
    				// Onder
    				else if ((isBoardPos(board[xTemp][0], board[xTemp][1] - 66) == true)
    						&& (isEmptyPos(board[xTemp][0],
    								board[xTemp][1] - 66 * 2) == true)) {
    					LOG("Is not lost");
    					onder = true;
    					break;
    				}
    			}

    Thanks a lot for some help

    Greetz!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Proper way to get Rid of breaks (iForgot)

    What do you mean by "you forgot how to get rid of breaks"?
    What are you trying to do and why do you think that using 'break' isn't the right approach.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2011
    Posts
    20

    Re: Proper way to get Rid of breaks (iForgot)

    Quote Originally Posted by keang View Post
    What do you mean by "you forgot how to get rid of breaks"?
    What are you trying to do and why do you think that using 'break' isn't the right approach.
    In school they always said that using brackets isn't clean!

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Proper way to get Rid of breaks (iForgot)

    In school they always said that using brackets isn't clean!
    You were talking about breaks and now you are talking about brackets - which one do you mean?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2011
    Posts
    20

    Re: Proper way to get Rid of breaks (iForgot)

    Quote Originally Posted by keang View Post
    You were talking about breaks and now you are talking about brackets - which one do you mean?
    breaks sorry typo

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Proper way to get Rid of breaks (iForgot)

    You have a few options when terminating a loop:

    1. Make the loop test fail.
    2. Use a break statement (or return statement if you also want to return from the method) .
    3. Throw an exception.

    The last option is clearly not useful in this circumstance, which leaves option 1 and 2. In your case artificially setting the xTemp counter to a value greater than board.length is even less intuitive than using a break.

    I suppose you could introduce a boolean variable which is checked as well as the counter but I'm not convinced this is any cleaner eg:

    Code:
        boolean finish = false;
        for (int xTemp = 0; xTemp < board.length && !finish; xTemp++) {
    	if ( someTest ) {
    		repaint();
    		finish = true;
    	}
        }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured