Notes: Loop/Statement Labels

Wednesday, January 13th, 2010 | Web Development | Gabriel Mariani
Below are some notes from Christian Cantrell’s blog that I just wanted to aggregate onto my site for my own personal reference. I am not claiming this as my own content, but I’ve been meaning to add this to my notes and Christian did a great job explaining it.

Labels are a relatively unknown feature in AS3. Basically what it does is give an ID to a specific loop or statement. So when you call break or continue, you can specify WHICH loop to break or continue.

Below is copied verbatim from Christian’s site.

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 == 25)  break dateCheck; // Christmas!
    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.)
Print Friendly

Tags: ,

Leave a comment

Please upgrade your Flash Player To submit a comment, you must have Flash Player 9.0.0 or higher installed. I use a flash form here to help prevent spam.

No comments yet.

Search

Ad