JavaScript Tutorials - Herong's Tutorial Examples - 2.33, by Herong Yang
"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: "initial_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.
Table of Contents
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Conditional "if" Statement Examples
"switch ... case" Statement Example
"while" Loop Statement Example
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
Defining Your Own Object Types
Inheritance of Properties and Methods through the Prototype Object Chain
'jrunscript' - JavaScript Shell Command from JDK
Using Functions as "Function" Objects
Introduction to Built-in Object Types
W3C's Document Object Model (DOM) Specifications