|
|
|
#1 |
|
Sporadic FK User
Join Date: Dec 2003
Location: Houston
Posts: 317
|
Break embedded loops simultaneously
Does anyone know how to break out of embedded for loops with the same command from the inner loop?
example: Code:
for(i = 0;i < 10;i ++){
for(n = 0;n < 10;n ++){
if(myFunction(i,n)){
//do something
//break out of both loops
}
}
}
Thanks for any help.
__________________
undefined |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Unfortunately, there's no 'goto' command in Flash, although it would come in handy in this case - this is what happens when language designers get overly politically correct.
Here's a few things you could do: 1. Put the whole thing in a function and use return. Costs a little extra for the function call, but possibly not as much as adding a condition to the outer loop.
2. Use a single loop. Unfortunately, more processor intensive than adding a condition on the outer loop.
3. Your original suggestion. Probably not that painful, since the bulk of your CPU time happens in the inner loop. This is the choice I'd probably use myself.
Last edited by jbum; 08-06-2004 at 04:22 PM. |
|
|
|
|
|
#3 |
|
Sporadic FK User
Join Date: Dec 2003
Location: Houston
Posts: 317
|
Thanks! I didn't think of putting it in a funciton, but that should work well since I use it twice anyway.
__________________
undefined |
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Dec 2008
Posts: 1
|
consider also changing the outer loop's variable to something that will break it.
i.e. Code:
for ( i=0; i<40; i++ ) {
for ( k=0; k<100; k++ ) {
if (somecondition) {
i = 40;
break;
}
}
}
|
|
|
|
![]() |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|