F > for

 

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

Action; 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 object (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.

Example

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

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

The following results are displayed in the Output window:

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 code below, 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