[Contents] [Previous] [Next] [Index]

for

Creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop.

Implemented in

Navigator 2.0, LiveWire 1.0

Syntax

for ([initial-expression;] [condition;] [increment-expression]) {
   statements
}

Arguments

initial-expression
Statement or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword.

condition
Evaluated on each pass through the loop. If this condition evaluates to true, the statements in statements are performed. This conditional test is optional. If omitted, the condition always evaluates to true.

increment-expression
Generally used to update or increment the counter variable.

statements
Block of statements that are executed as long as condition evaluates to true. This can be a single statement or multiple statements. Although not required, it is good practice to indent these statements from the beginning of the for statement.

Examples

The following for statement starts by declaring the variable i and initializing it to 0. It checks that i is less than nine, performs the two succeeding statements, and increments i by 1 after each pass through the loop.

for (var i = 0; i < 9; i++) {
   n += i
   myfunc(n)
}


[Contents] [Previous] [Next] [Index]

Last Updated: 10/31/97 12:29:59


Copyright © 1997 Netscape Communications Corporation