Looping


Usage
  • Braces are optional (for single-statement blocks).
while
Syntax
while (expr) {
    statements;
}

while (expr);
do while
Syntax
do {
    statements;
} while (expr);
for
Syntax
for (initial-expr; cond-expr; loop-expr) {
    statements;
}
Usage
  • 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.
Examples
for (var i:Number = 0; i < 10; ++i) {
    ...
}
for ... in
Syntax
for (var property-name in object-name) {
    statements;
}
Usage
  • Iterates through the properties of an object
Examples
for (var propName in myObject) {
    propertyValue = myObject[propName];
}
Loop control statements
Usage
  • break : Break from the loop immediately.
  • continue : Continue on to the next iteration of the loop.
Examples
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.
    }
}