User Guide
90 ActionScript Language and Syntax
You can also iterate through the elements of an array, as this example shows:
var myArray:Array = ["one", "two", "three"];
for each (var item in myArray)
{
trace(item);
}
// output:
// one
// two
// three
You cannot iterate through the properties of an object if the object is an instance of a sealed
class. Even for instances of dynamic classes, you cannot iterate through any fixed properties,
which are properties defined as part of the class definition.
while
The while loop is like an if statement that repeats as long as the condition is true. For
example, the following code produces the same output as the
for loop example:
var i:int = 0;
while (i < 5)
{
trace(i);
i++;
}
One disadvantage of using a while loop instead of a for loop is that infinite loops are easier
to write with
while loops. The for loop example code does not compile if you omit the
expression that increments the counter variable, but the
while loop example does compile if
you omit that step. Without the expression that increments
i, the loop becomes an infinite
loop.
do..while
The do..while loop is a while loop that guarantees that the code block is executed at least
once, because the condition is checked after the code block is executed. The following code
shows a simple example of a
do..while loop that generates output even though the condition
is not met:
var i:int = 5;
do
{
trace(i);
i++;