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

while

Creates a loop that evaluates an expression, and if it is true, executes a block of statements. The loop then repeats, as long as the specified condition is true.

Implemented in

Navigator 2.0, LiveWire 1.0

Syntax

while (condition) {
   statements
}

Arguments

condition
Evaluated before each pass through the loop. If this condition evaluates to true, the statements in the succeeding block are performed. When condition evaluates to false, execution continues with the statement following statements.

statements
Block of statements that are executed as long as the condition evaluates to true. Although not required, it is good practice to indent these statements from the beginning of the statement.

Examples

The following while loop iterates as long as n is less than three.

n = 0
x = 0
while(n < 3) {
   n ++
   x += n
}
Each iteration, the loop increments n and adds it to x. Therefore, x and n take on the following values:

After completing the third pass, the condition n < 3 is no longer true, so the loop terminates.


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

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


Copyright � 1997 Netscape Communications Corporation