for

Availability

Flash Player 5.

Usage

for(init; condition; next) {
  statement(s); 
}

Parameters

init An expression to evaluate before beginning the looping sequence, typically an assignment expression. A var statement is also permitted for this parameter.

condition An expression that evaluates to true or false. The condition is evaluated before each loop iteration; the loop exits when the condition evaluates to false.

next An expression to evaluate after each loop iteration; usually an assignment expression using the ++ (increment) or -- (decrement) operators.

statement(s) An instruction or instructions to execute within the body of the loop.

Description

Statement; a loop construct that evaluates the init (initialize) expression once, and then begins a looping sequence by which, as long as the condition evaluates to true, statement is executed and the next expression is evaluated.

Some properties cannot be enumerated by the for or for..in actions. For example, the built-in methods of the Array class (such as Array.sort() and Array.reverse()) are not included in the enumeration of an Array object, and movie clip properties, such as _x and _y, are not enumerated. In external class files, instance members are not enumerable; only dynamic and static members are enumerable.

Example

The following example uses for to add the elements in an array:

my_array=new Array();
for(i=0; i<10; i++) {
  my_array [i] = (i + 5)*10;
  trace(my_array[i]);
}

The following results are displayed in the Output panel:

50
60
70
80
90
100
110
120
130
140

The following is an example of using for to perform the same action repeatedly. In the following code, the for loop adds the numbers from 1 to 100:

var sum = 0;
  for (var i=1; i<=100; i++) {
    sum = sum + i;
  }

See also

++ (increment), -- (decrement), for..in, var