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

"for" Loop Statements

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

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

for ([initial_exp]; [loop_condition]; [increment_exp])
   statement or statement_block

If you want to know exactly how a "for" statement will be executed, here is my understanding:

Step 1: "intial_exp", usually an assignment expression, is evaluated.

Step 2: "loop_condition", a Boolean expression, is evaluated.

Step 3: If "loop_condition" is evaluated to "true", continue with Step 5.

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

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

Step 6: "increment_exp", usually an assignment expression, is evaluated.

Step 7: Continue with Step 2.

"for" 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 "for" 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
"for" Loop Statements