Flash Lite 2 |
|||
| Flash Lite 2.x ActionScript Language Reference > ActionScript language elements > Statements > do..while statement | |||
do { statement(s) } while (condition)
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.
Availability: ActionScript 1.0; Flash Lite 1.0
condition:Boolean - The condition to evaluate. The statement(s) within the do block of code will execute as long as the condition parameter evaluates to true .
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
*/