De-I > for
forSyntax
for(
init; condition; next
) {
statement;
}
Arguments
init
An expression to evaluate before beginning the looping sequence, typically an assignment expression. A var
statement is also permitted for this argument.
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
A statement within the body of the loop to execute.
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 can not 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.
Player
Flash 5 or later.
Example
The following example uses for
to add the elements in an array:
for(i=0; i<10; i++) {
array [i] = (i + 5)*10;
}
Returns the following array:
[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