There’s a little-known feature of ActionScript 3 called labels which can give you better control over nested loops. In particular, labels give you control over the depth of your continue and break statements.
Consider the following piece of code which compares two arrays to find which elements exist in outerArray that do not exist in innerArray:
var outerArray:Array = ["coffee", "juice", "water", "beer"];
var innerArray:Array = ["liquor", "beer", "wine", "juice"];
for each (var foo:String in outerArray)
{
var found:Boolean = false;
for each (var bar:String in innerArray)
{
if (foo == bar)
{
found = true;
}
}
if (!found)
{
trace(foo);
}
}
(Note: I know there are better ways of doing this particular comparison; this technique is for demonstration purposes only.)
The same code could be written using labels like this:
var outerArray:Array = ["coffee", "juice", "water", "beer"];
var innerArray:Array = ["liquor", "beer", "wine", "juice"];
outerLoop: for (var i:uint = 0; i < outerArray.length; ++i)
{
var foo:String = outerArray[i];
innerLoop: for (var j:uint = 0; j < innerArray.length; ++j)
{
var bar:String = innerArray[j];
if (foo == bar)
{
continue outerLoop;
}
}
trace(foo);
}
Notice how the labels outerLoop and innerLoop precede my for loops, and how I can reference the outerLoop label in my continue statement. Without specifying a label, continue would have continued from the top of innerLoop rather than outerLoop.
Labels will also work with break statements, and can even be used to create their own blocks as in the following example:
dateCheck:
{
trace("Good morning.");
var today:Date = new Date();
if (today.month == 11 && today.date == 24) // Christmas!
{
break dateCheck;
}
trace("Time for work!");
}
Some things to keep in mind about labels:
- You can’t use a
continuestatement in the context of a labeled code block since the result would be an infinite loop. This will be caught by the compiler. - You obviously can’t reference labels that don’t exist. The compiler will catch your mistake and let you know that the target wasn’t found.
- In using labels yesterday for new application I’m working on, I discovered that they don’t play well with
for each..inloops. If you’re going to use labels, you’ll want to make sure you’re using regularfororwhileloops. (A bug has been filed and will hopefully be fixed soon.)

Wow – I had no idea this feature was in AS3. Cool! But, kind of terrifying in terms of code cleanliness.
Wow, I didn’t know about labels in AS3. It looks like goto instruction from others, older, programming languages ex. pascal, but it’s not recommended to use it if i remember. It looks good and simplier in your example but i’m not sure what will happen when someone start using this construction in the whole project in more complicated loops. I’m convinced it affect code readability.
Wow, that’s really awesome! It’s amazing that this managed to slip under the radar for so long. Thanks for the post on it!
wow, this is very cool! thanx a lot!
Reminds me of goTo Statements, which i consider pretty ugly
Did you do any performance test with that?What kind of bytecode is it creating when using labels?Is it a low-level API or a “flash shortcut”?
I almost wish you hadn’t blogged about this particular topic. The ratio of ActionScript programmers who can responsibly use this tool to those that cannot is dangerously low.I’d argue that the only acceptable case for these is in a doubly nested for loop that cannot be extrapolated into its own function (thus using return instead of goto) where you’re testing for a specific condition. This is a relatively rare construct in modern code.That said, in two years, all the AS freelancers out there will be gutting and fixing spaghetti goto statements for great profit.
Why?Seems goto-ey.
I wish you did not write this post. I am scared somebody can use it and I’d not like to be the one who has to fix that code
Pointless.
Cory Petosky:One good case would be for doing iterative implementation of recursive algotithms.(ex: iterative sorting)So instead of pusing and poping scope, you use labels.But then again I have no idea of the performances in flash!
There is an AVM2 bytecode instruction jump, which is used very often, so absolutely no problems with performance.
Also i want to say that gotophobia is very common illness nowadays.
In good hands labels are harmless and helpful.
In bad hands… they cannot appear in bad hands nowadays, because all bad hands’ owners are gotophobs.
Is there such feature for controlling nested if statements?
Well, you can just use the label together with the if statements.
P.S.: Christmas is actually on month 11 day 24 if you count from the index 0
:p