-
December 19th, 2011, 11:59 AM
#1
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!
-
December 19th, 2011, 12:44 PM
#2
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.
-
December 20th, 2011, 09:29 AM
#3
Re: Proper way to get Rid of breaks (iForgot)
Originally Posted by keang
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!
-
December 20th, 2011, 10:19 AM
#4
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?
-
December 20th, 2011, 01:30 PM
#5
Re: Proper way to get Rid of breaks (iForgot)
Originally Posted by keang
You were talking about breaks and now you are talking about brackets - which one do you mean?
breaks sorry typo
-
December 20th, 2011, 01:50 PM
#6
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 Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|