justnajm
justnajm

Reputation: 4534

does as3 support loop label?

I have a herarchery of loop and want to quit all parent loop, do AS3 support labels like we have them in php ?

for(var i:int=0;i<100;i++)
{
    for(var j:int=0;j<100;j++)
    {
        if(j == 15){

           i = 99;
           break;

        }
    }
}

Upvotes: 3

Views: 915

Answers (2)

robkuz
robkuz

Reputation: 9934

You could incorporate the break_condition into the for loop as well

var outer_break = false;
var inner_break = false;
for(var i = 0; i < 100 && !outer_break; i++) {
     for(var j = 0; j < 100 && !inner_break;j++) {
         if (some_condition) inner_break = true;
     }
}

I would recommend NOT to use labels (much to much goto) here and I would also strongly suggest not to use for loops at all. but instead use foreach to iterate over whatever you need to iterate.

Upvotes: 0

Marty
Marty

Reputation: 39466

Yes, you can use label: in front of the loop like this:

// Label this loop 'outer'.
outer: for(var i:int = 0; i < 100; i++)
{
    inner: for(var j:int = 0; j < 100; j++)
    {
        if(j === 15)
        {
            // Break the outer loop.
            break outer;
        }
    }
}

This also works for continue.

Upvotes: 5

Related Questions