Share via


for Statement

Executes a block of statements for as long as a specified condition is true.

for ([initialization]; [test]; [increment])
...statement

Arguments

  • initialization
    Optional. An expression. This expression is executed only once, before the loop is executed.

  • test
    Optional. A Boolean expression. If test is true, statement is executed. If test if false, the loop is terminated.

  • increment
    Optional. An expression. The increment expression is executed at the end of every pass through the loop.

  • statement
    Optional. Statement or statement block to be executed if test is true.

Remarks

You usually use a for loop when the loop is to be executed a known number of times. A for loop is useful for iterating over arrays and for performing sequential processing.

The test of a conditional expression occurs before the execution of the loop, so a for statement executes zero or more times.

On any line in a for loop statement block, you can use the break statement to exit the loop, or you can use the continue statement to transfer control to the next iteration of the loop.

Example

In the following example, the for statement executes the enclosed statements as follows:

  • First, the initial value of the variable i is evaluated.

  • Then, as long as the value of i is less than or equal to 9, the document.write statements are executed and i is reevaluated.

  • When i is greater than 9, the condition becomes false and control is transferred outside the loop.

// i is set to 0 at the start and is incremented by 1 at the
// end of each iteration.
// The loop terminates when i is not less than or equal to
// 9 before a loop iteration.
var s = "";
for (var i = 0; i <= 9; i+)
{
    s += i + " ";
}

print (s);
// Output: 0 1 2 3 4 5 6 7 8 9

All of the expressions of the for statement are optional. In the following example, the for statements implement an infinite loop, and a break statement is used to exit the loop.

var s = "";

var j = 0;
for (;;)
{
    if (j >= 5)
        {
        break;
        }
    j++;
    s += j + " ";
}

print (s);
// Output: 1 2 3 4 5

Requirements

Version 1

See Also

Reference

for...in Statement

while Statement

Change History

Date

History

Reason

July 2009

Modified example.

Content bug fix.

March 2009

Modified syntax, argument definitions, remarks, and examples.

Information enhancement.