Loops

The do..while loop does the test at the end of the loop. A do…while always executes at least once. After each execution, the Boolean expression is tested, if true the loop repeats, if false the next statement after the loop is executed. The program below is the same as the one above except that a do…while is used. Try the code below to display the numbers 0 to 5.

The format for a for loop is: for ( initialize list; Boolean expression to continue; execute at end) statement or block of statements;
Example: Start a new flash movie and write the code shown below in the Actions panel. Notice that we can declare a variable inside a for statement:

var  num:int=0;
  do  {
    trace(num);
    num++;
  } while(num<=5);
1
2
3
4
5
var  num:int=0;
  do  {
    trace(num);
    num++;
  } while(num<=5);
CODE

When you test the movie you will see the numbers 0 to 5 in the output window.
Notice the indentation: all statement within { and } are indented the same amount for readability.

Experiment: Try to generate the numbers 10,9,8,7,6,5,4,3,2,1 using while, for and do loops.

NEXT: Loop though all of the display objects