Archive for April 10th, 2008

10
Apr

Label Statements

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

One of the pimping new features of AS3 is the ability to label your loops. At first you may think this is pointless but once you start using them you will find out how wonderful they truly are.

They come into their own when you have nested loop structure and you want to exit loops when a certain condition is met.

Kudos goes to senocular for me finding out about this cool feature.

In AS2 you would do the following to break a loop.

var i:Number;

var j:Number;

var exit:Boolean = false;

for (i=0; i<10; i++) {

    for (j=0; j<10; j++) {

        if (i > 3 && j > 3) {

            exit = true;

            break;

        }

    }

    if (exit) {

        break;

    }

}

But with AS3 and labels you can break from a specific loop.
E.G

var i:Number;

var j:Number;

mainLoop: for (i=0; i<10; i++) {

    for (j=0; j<10; j++) {

        if (i > 3 && j > 3) {

            break mainLoop;

        }

    }

}

With labels the nested loop is able to break the mainloop. This makes the code cleaner and easier to read.

Nice.

Found this useful? How about buying me a coffee - simply click here.




Close
E-mail It