User Guide

do while 145
do while
Availability
Flash Player 4.
Usage
do {
statement(s)
} while (condition)
Parameters
condition
The condition to evaluate.
statement(s) The statement(s) to execute as long as the condition parameter evaluates
to
true.
Returns
Nothing.
Description
Statement; similar to a while loop, except that the statements are executed once before the initial
evaluation of the condition. Subsequently, the statements are executed only if the condition
evaluates to
true.
A
do..while loop ensures that the code inside the loop executes at least once. Although this can
also be done with a
while loop by placing a copy of the statements to be executed before the
while loop begins, many programmers believe that do..while loops are easier to read.
If the condition always evaluates to
true, the do..while loop is infinite. If you enter an infinite
loop, you encounter problems with Flash Player and eventually get a warning message or crash the
player. Whenever possible, you should use a
for loop if you know the number of times you want
to loop. Although
for loops are easy to read and debug, they cannot replace do..while loops in
all circumstances.
Example
The following example uses a do..while loop to evaluate whether a condition is true, and
traces
myVar until myVar is greater than 5. When myVar is greater than 5, the loop ends.
var myVar:Number = 0;
do {
trace(myVar);
myVar++;
} while (myVar<5);
/* output:
0
1
2
3
4
*/
CHAPTER 5
ActionScript Core Language Elements