AS3: Label Statements
I’ve read about this in a couple of places and never had much of a use for it until now. But it’s really relevant and (as Josh would say) real slick.
Label statements allow you to keep track of different loops or switch statements (or anything that can break or continue, for that matter). By referencing a loop with a label statement, you can specifically force ActionScript to exit that loop.
Consider this:
for(var i:int=0;i<100;i++)
{
for(var ii=0;ii<100;ii++)
{
if(ii == 5) // I want to break right here.
}
}
There are lots of ways to skin this cat, but this strikes me as the easiest (and most readable):
outerLoop: for(var i:int=0;i<100;i++)
{
innerLoop: for(var ii:int=0;ii<100;ii++)
{
if(ii == 5) break outerLoop; // done.
}
}
By calling break on the outerLoop, I automatically exit both simultaneously. And my coder colleagues will thank me for specifying what is happening here. Especially if I have a loop within a loop within a switch within a loop.
Read all about it on the livedocs.
September 27th, 2007 by Stephen / 6 Comments


