tutorials flash labels
Label with break and continue
While browsing through the AS3 reference, i came across a nice new feature of the language. It’s now possible to give labels to our code (loops, etc…) which use break or continue.
This method is very practical when you want to cleanly exit from a large block of nested code without having to create lots of conditionals to return to the end of the code.
First of all, I’ll give you a simple example of using break without labels, which will then demonstrate the effectiveness of using a label within our loops :
var i:uint ; var j:uint ; var k:uint ; for (i = 0 ; i<2 ; i++) // first { for (j = 0 ; j<2 ; j++) // second { for (k = 0 ; k<2 ; k++) // first { trace( " third : " + i + " : " + j + " : " + k) ; if (k == 1) break ; } trace( " second : " + i + " : " + j + " : " + k) ; if (k==1) break ; // i’ve got to redo another test here to exit from the loop, which isn’t very practical } trace( " first : " + i + " : " + j + " : " + k) ; }
In the example above, performing a break in the « third » loop, I end up in the « second » loop. Now if i want to completely exit from the nested code I’ve got to again do another break and create another conditional to be certain to exit from the loop at the right moment !
Now lets see the same example using AS3 labels :
var i:uint ; var j:uint ; var k:uint ; first1: for (i = 0 ; i<2 ; i++) { second1: for (j = 0 ; j<2 ; j++) { third1: for (k = 0 ; k<2 ; k++) { trace( " third : " + i + " : " + j + " : " + k) ; if (k == 1) break first1 ; } trace( " second : " + i + " : " + j + " : " + k) ; } trace( " first : " + i + " : " + j + " : " + k) ; }
As you can see above, it’s very simple to label a loop by putting « MyLabelName: » before the code. Then you just have to put in the code – after a break (or a continue) – the label name you want to reach !
I’ll give you the same example using a continue instead of a break.
var i:uint ; var j:uint ; var k:uint ; first2: for (i = 0 ; i<2 ; i++) { trace( " first : " + i + " : " + j + " : " + k) ; second2: for (j = 0 ; j<2 ; j++) { trace( " second : " + i + " : " + j + " : " + k) ; third2: for (k = 0 ; k<2 ; k++) { trace( " third : " + i + " : " + j + " : " + k) ; if (k == 1) continue first2 ; } } }
Note : it’s not possible to use the same label name more than once in the same block of code, it’s logical really ! :)
I’d advise you, like me, that if you discover new functionality like this, to do some tests in Flex2 to really understand everything that’s possible to achieve with it… it’s really a very, very practical tool.
By EKAMELEON - ALCARAZ Marc (2006) You can view this article and its comments on my blog.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


