The for statement is use to e xecute a block of statements for as long as a specified condition is true.
Syntax
for (initialization; test; increment)
statement
The for statement syntax has these parts:
Part |
Description |
initialization |
An expression. This expression is executed only once, before the loop is executed. |
test |
A Boolean expression. If test is true, statement is executed. If test if false, the loop is terminated. |
increment |
An expression. The increment expression is executed at the end of every pass through the loop. |
statement |
The statement to be executed if test is true. Can be a compound statement. |
Example
You usually use a for loop when the loop is to be executed a specific number of times. The following example demonstrates a for loop.
/* i is set to 0 at start, and is incremented by 1 at the end
of each iteration. Loop terminates when i is not less
than 10 before a loop iteration. */
myarray = new Array();
for (i = 0; i < 10; i++)
{
myarray[i] = i;
}
|
See also: for...in Statement, while Statement |