Looping
Usage
- Braces are optional (for single-statement blocks).
Syntaxdo while
while (expr) {
statements;
}
while (expr);
Syntaxfor
do {
statements;
} while (expr);
Syntaxfor ... in
for (initial-expr; cond-expr; loop-expr) {Usage
statements;
}
Examples
- initial-expr : Evaluated once before the loop is executed.
- cond-expr : Evaluated before each iteration of the loop; if the value is false, the execution of the loop stops immediately.
- loop-expr : Evaluated at the end of each iteration of the loop, or after a "continue" statement is encountered.
for (var i:Number = 0; i < 10; ++i) {
...
}
SyntaxLoop control statements
for (var property-name in object-name) {Usage
statements;
}
Examples
- Iterates through the properties of an object
for (var propName in myObject) {
propertyValue = myObject[propName];
}
Usage
Examples
- break : Break from the loop immediately.
- continue : Continue on to the next iteration of the loop.
var sum:Number = 0;
var i:Number = 1;
while (i < 10) {
if (i == 3) {
continue; // Skip the rest of this loop iteration.
}
sum += i;
if (sum > 15) {
break; // Exit the loop.
}
}