O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loops

In the preceding section, we saw that a conditional causes a statement block to execute once if the value of its test expression is true. A loop, on the other hand, causes a statement block to be executed repeatedly, for as long as its test expression remains true.

ActionScript provides five different types of loops: while, do-while, for, for-in, and for-each-in. The first three types have very similar effects but with varying syntax. The remaining two types are used to access the dynamic instance variables of an object. We haven't studied dynamic instance variables yet, so for now we'll consider the first three types of loops. For information on for-in and for-each-in, see Chapter 15.

The while Statement

Structurally, a while statement is constructed much like an if statement: a main statement encloses a code block that is executed only when a given test expression is true:

while (testExpression) {
  codeBlock
}

If testExpression is true, the code in codeBlock (called the loop body) is executed. But, unlike the if statement, when the codeBlock is finished, execution begins again at the beginning of the while statement (that is, ActionScript "loops" back to the beginning of the while statement). The second pass through the while statement works just like the first: the testExpression is evaluated, and if it is still true, codeBlock is executed again. This process continues until testExpression becomes false, at which point execution continues with any statements that follow the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required