JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.00

"while" Loop Statements

This section provides a quick description of the 'while' loop statement.

The "while" statement is a very common loop statement in many programming languages. The JavaScript "while" statement has the following generic syntax format:

while (loop_condition)
   statement or statement_block

A "while" statement is executed as follows:

Step 1: "loop_condition" expression is evaluated as a Boolean value.

Step 2: If "loop_condition" is evaluated to "true", continue with Step 4.

Step 3: If "loop_condition" is evaluated to "false", terminate the loop.

Step 4: Execute "statement" or "statement_block".

Step 5: Continue with Step 1.

There is another form of "while" loop, called "do ... while" loop statement, with the following syntax format:

do
   statement or statement_block
while (loop_condition);

"while" loop statement could be executed infinitely, if the "loop_condition" is always evaluated to "true".

A "break" statement could be used inside the loop statement block to terminate the loop early.

A "continue" statement could be used inside the loop statement block to skip the rest of the block and start the next iteration of the loop.

A tutorial example will be given in the next section showing an example of the "while" loop statement.

Sections in This Chapter

What Is a Statement?

Conditional "if" Statements

Conditional "if" Statement Examples

"switch ... case" Statements

"switch ... case" Statement Example

"for" Loop Statements

"for" Loop Statement Example

"while" Loop Statements

"while" Loop Statement Example

Dr. Herong Yang, updated in 2008
"while" Loop Statements