-
Breaking out of multiple loops
Imagine you have code in a nested loop. You want to break out of both loops as a result of a condition. Something like this:
Code:
for (var j = 0; j < HEIGHT; j++){
for (var k = 0; k < WIDTH; k++) { if (matrix[j][k] == 0) { break;
break;
} } }
Ugh, that looks horrible, sorry. My point is, the code above won't work, because the actionscript "reader" or "compiler" or whatever isn't going to recognize any code that continues past the first "break;" statement.
Is there some way of writing, "break every loop"?
-
Senior Member
try this
for (var j = 0; j < HEIGHT; j++){
for (var k = 0; k < WIDTH; k++) {
if (matrix[j][k] == 0) {
}
break;
}
break;
}
to breaks inside if will only affect if!
-
First, I'm pretty sure that "break;" doesn't break an if-else block of code; it just seemingly ignores all statements after "break;" that come before the "}" signifying the end of the loop body.
But also, I'm trying to make the two loops break as a result of the condition inside. Meaning, there could be all sorts of statements inside those loops, but I'd like some kind of escapeway if all that code is, after some number of loops, unnecessary.
-
Senior Member
then simply write this in if statement
k=WIDTH;
j=HEIGHT;
this will seth both for loop conditions to false and break them
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
|