Labels in ActionScript 3

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 continue statement 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..in loops. If you’re going to use labels, you’ll want to make sure you’re using regular for or while loops. (A bug has been filed and will hopefully be fixed soon.)

14 Responses to Labels in ActionScript 3

  1. Ben Garney says:

    Wow – I had no idea this feature was in AS3. Cool! But, kind of terrifying in terms of code cleanliness. ๐Ÿ™‚

  2. 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.

  3. JTtheGeek says:

    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!

  4. xero says:

    wow, this is very cool! thanx a lot!

  5. Timo says:

    Reminds me of goTo Statements, which i consider pretty ugly ๐Ÿ™‚

  6. jpauclair says:

    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”?

  7. Cory Petosky says:

    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. ๐Ÿ™‚

  8. Vadim P. says:

    Why?Seems goto-ey.

  9. Cesare says:

    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 ๐Ÿ™‚

  10. Stat says:

    Pointless.

  11. jpauclair says:

    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!

  12. Dan says:

    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.

  13. theNameOfTheFlame says:

    Is there such feature for controlling nested if statements?

    • william says:

      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