Beginning ActionScript 3.0 – Looping 4

 

As you know, the break statement can terminate the current loop, while the continue statement can skip the left code in current iteration and begin next iteration. Today, we introduce something new, which can cooperate with the break and continue.

Maybe you are also interested in knowing which are the best applications to download videos, you can see them from this link aplicaciones para descargar videos

 

label

When you using nested loop, you may want flow jump out from the inner loop to the main program directly, not from one loop to another loop. Or, when you using the switch statement inside the looping statement, and you want the break statement inside switch statement terminates the whole loop, not just jump out the switch block. In these situations, you need the label statement.

The genera form of label is:

label: statement

The label here refers to an identifier that you want to link with the statement. And the statement here refers to the statements you want to link with the identifier. So, you can use the label statement like this:

  1. var i:int;
  2. initial: i = 0;

or like this:

  1. outer: while (expression ) {
  2. //code block a
  3. while ( expression ) {
  4. if (expression)
  5. break outer;
  6. // code block b
  7. }
  8. //code block c
  9. }

Or even follow the label by a code block.

  1. initial: {
  2. var i:int = 0;
  3. var j:int = 1;
  4. var k:int = 2;
  5. }

If you follow the label by a code block, you can’t use the continue statement with the label. That’s because if you use the continue statement, the code block will become an infinite loop. And you can use the break statement to jump out from the current code block.

If you try to use the continue statement in the code block, you will get the following result.

 

But you can use the break statement in the code block, because the flash player knows that you want to join out from the current code block. So, the following code can get the result you want.

  1. testLabel:{
  2. trace(”hello”);
  3. break testLabel;
  4. trace(”System bug”);
  5. }
  6. trace(”ActionScript”);

Remember, you can use the break statement to join out from the current code block, which means, you should use the break statement inside the block.

If you try to use the break statement outside the block, you will get an error.

 

After some wrong cases, let’s take a look at the correct usage of the label statement with continue.

Remember our example about print the prime numbers between 100 and 200?

  1. var isPrime:Boolean = false;
  2. for(var i:int = 100; i <= 200; i++) {
  3. var max:int = int(Math.sqrt(i));
  4. isPrime = true;
  5. for(var j:int = 2; j <= max; j++) {
  6. if(i % j == 0) {
  7. isPrime = false;
  8. break;
  9. }
  10. }
  11. if(isPrime)
  12. trace(i);
  13. }

Now, let’s use the label statement with continue to rewrite that example.

  1. outer:for(var i:int = 100; i <= 200; i++) {
  2. var max:int = int(Math.sqrt(i));
  3. for(var j:int = 2; j <= max; j++) {
  4. if(i % j == 0) {
  5. continue outer;
  6. }
  7. }
  8. trace(i);
  9. }

When flash player meets the continue with a label, the flow will jump to the code block designated by the label and execute the code from the top of the code block; and when the flash play meets the break statement with a label, the flow will jump out from the code block and execute the code after the code block designated by the label.

For example, if the code is like:

  1. outer: while (true ) {
  2. //code block a
  3. while ( true ) {
  4. //code block b
  5. break outer;
  6. // code block c
  7. }
  8. //code block d
  9. }
  10. // code block e

Then the execution order will be:

  1. 1. code block a
  2. 2. code block b
  3. 3. break outer
  4. 4. code block e

If the code is like:

  1. outer: while (true ) {
  2. //code block a
  3. while ( true ) {
  4. //code block b
  5. continue outer;
  6. // code block c
  7. }
  8. //code block d
  9. }
  10. // code block e

Then the execution order will be:

  1. 1. code block a
  2. 2. code block b
  3. 3. continue outer
  4. 4. code block a
  5. ……

Though the label statement can may your code looks more concise, but I recommended you not to use the label statement unless it is necessary. Because you only need to use the label statement in nested loop, and use the label statement can make the program’s logic more complicated.

Keep things simple is a good style So, you just need to know the usage of the label statement, don’t use them unless it is necessary.