To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 08-06-2004, 03:35 PM   #1
Nialsh
Sporadic FK User
 
Nialsh's Avatar
 
Join Date: Dec 2003
Location: Houston
Posts: 312
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
		}
	}
}
I know I could have an extra conditional in the outer loop that checks a variable that gets set by the "if", but that's a lot of extra processing.
Thanks for any help.
__________________
undefined
Nialsh is offline   Reply With Quote
Old 08-06-2004, 04:19 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
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.

code:

myLoopy = function()
{
for(i = 0;i < 10;i ++) {
for(n = 0;n < 10;n ++) {
if(myFunction(i,n)) {
//do something
//break out of both loops
return;
}
}
}
}




2. Use a single loop. Unfortunately, more processor intensive than adding a condition on the outer loop.

code:

for(x = 0;x < 100; x++){
if (myFunction(Math.floor(x/10),x%10)) {
//do something
//break out of both loops
break;
}
}



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.

code:

over = false;
for (i = 0; i < 10 && !over; i++) {
for (n = 0; n < 10; n++) {
if (myFunction(i,n)) {
//do something
//break out of both loops
over = true;
break;
}
}
}

__________________

Last edited by jbum; 08-06-2004 at 04:22 PM.
jbum is offline   Reply With Quote
Old 08-06-2004, 05:29 PM   #3
Nialsh
Sporadic FK User
 
Nialsh's Avatar
 
Join Date: Dec 2003
Location: Houston
Posts: 312
Thanks! I didn't think of putting it in a funciton, but that should work well since I use it twice anyway.
__________________
undefined
Nialsh is offline   Reply With Quote
Old 12-03-2008, 04:02 PM   #4
Antago
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;
      }
    }
}
Antago is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:02 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.