About statements 161
4. Select Frame 1 of the Timeline, and then type the following ActionScript in the
Actions panel:
var users_ds:mx.data.components.DataSet;
//
users_ds.addItem({name:"Irving", age:34});
users_ds.addItem({name:"Christopher", age:48});
users_ds.addItem({name:"Walter", age:23});
//
users_ds.first();
while (users_ds.hasNext()) {
trace("name:" + users_ds.currentItem["name"] + ", age:" +
users_ds.currentItem["age"]);
users_ds.next();
}
5.
Select Control > Test Movie to test the document.
The following information is displayed in the Output panel:
name:Irving, age:34
name:Christopher, age:48
name:Walter, age:23
For more information, see the while statement in the ActionScript 2.0 Language
Reference.
About do..while loops
You can use the do..while statement to create the same kind of loop as a while loop.
However, the expression is evaluated at the bottom of the code block in a
do..while loop (it’s
checked after the code block executes), so the loop always runs at least one time. The
statements execute only if the condition evaluates to
true.
The following code shows a simple example of a
do..while loop that generates output even
though the condition is not met.
var i:Number = 5;
do {
trace(i);
i++;
} while (i < 5);
// Output: 5
When you use loops, you need to avoid writing infinite loops. If the condition in a
do..while loop continuously evaluates to true, you create an infinite loop that displays a
warning or crashes Flash Player. Use a
for loop instead if you know how many times you
want to loop. For more information on and examples of
do..while statement, see the
ActionScript 2.0 Language Reference.