Break statement and labels

0 Flares Twitter 0 Facebook 0 Google+ 0 Email -- Filament.io 0 Flares ×

The break statement allows us to skip the execution of a loop or statement:
[sourcecode language=’cpp’]
for(var i:int = 0 ; i < 10 ; i++) { if ( i >= 5 )
{
break;
}
}
[/sourcecode]
Simple.
Now imagine we have a loop nested in another loop:
[sourcecode language=’cpp’]
for(var i:int = 0 ; i < 10 ; i++) { for(var i2:int = 0 ; i2 < 10 ; i2++) { if ( i2 >= 5 )
{
break;
}
}
}
[/sourcecode]
The break statement here will only skip the rest of the immediate loop. The first loop will keep on going until its condition is reached.
Now we want to break both loops. There come the labels:
[sourcecode language=’cpp’]
first_for_loop: for(var i:int = 0 ; i < 10 ; i++) { second_for_loop: for(var i2:int = 0 ; i2 < 10 ; i2++) { if ( i2 >= 5 )
{
break first_for_loop;
}
}
}
[/sourcecode]
In this case, “first_for_loop” and “second_for_loop” are labels. This way we can identify loops. Our break statement tells flash to skip the “first_for_loop” loop and thus will stop the execution of every other loops nested inside it.

0 Flares Twitter 0 Facebook 0 Google+ 0 Email -- Filament.io 0 Flares ×

Leave a Comment

*