User Guide

Using condition statements 59
gotoAndPlay("startProgram");
}
If you want to check for one of several conditions, you can use the switch statement rather than
multiple
else if statements.
Repeating actions
ActionScript can repeat an action a specified number of times or while a specific condition exists.
Use the
while, do..while, for, and for..in actions to create loops.
To repeat an action while a condition exists:
Use the while statement.
A
while loop evaluates an expression and executes the code in the body of the loop if the
expression is
true. After each statement in the body is executed, the expression is evaluated again.
In the following example, the loop executes four times:
i = 4;
while (i>0) {
my_mc.duplicateMovieClip("newMC"+i, i, {_x:i*20, _y:i*20});
i--;
}
You can use the do..while statement to create the same kind of loop as a while loop. In a
do..while loop, the expression is evaluated at the bottom of the code block so the loop always
runs at least once.
This is shown in the following example:
i = 4;
do {
my_mc.duplicateMovieClip("newMC"+i, i, {_x:i*20, _y:i*20});
i--;
} while (i>0);
To repeat an action using a built-in counter:
Use the for statement.
Most loops use some kind of counter to control how many times the loop executes. Each
execution of a loop is called an iteration. You can declare a variable and write a statement that
increases or decreases the variable each time the loop executes. In the
for action, the counter and
the statement that increments the counter are part of the action. In the following example, the
first expression (
var i = 4) is the initial expression that is evaluated before the first iteration. The
second expression (
i > 0) is the condition that is checked each time before the loop runs. The
third expression (
i--) is called the post expression and is evaluated each time after the loop runs.
for (var i = 4; i > 0; i--){
my_mc.duplicateMovieClip("newMC"+ i, i, {_x:i*20, _y:i*20});
}