Availability: |
| ||||||||
JavaScript syntax: | - | aLabel: for(anInitializer; aCondition; aModifier) | |||||||
Argument list: | aCondition | An expression that yields a Boolean value | |||||||
aLabel | An optional label to name the iterator | ||||||||
aModifier | Modifies the value being enumerated | ||||||||
anInitializer | Assigns the starting value |
A for loop is established by setting up the control construct in the header and associating a statement block to be evaluated each time the loop is iterated.
The control construct in a for loop has three semi-colon-separated expressions. They are all optional.
The first one initializes the enumerator. It can also declare and initialize a variable to be used for this purpose if one has not already been created.
The second is the condition to test for exit when the required number of loops has been iterated. The for() loop exits when this value becomes false. While it is true, the loop will continue to cycle.
The third is the incrementor (or decrementor if you prefer).
The code in the statement block can be completed early with the break, continue or return statements.
A break will cause the for() loop to drop out and execution to continue at the line following its statement block.
A continue statement will cycle to the next iteration and begin executing the statement block without executing the remaining lines in the block.
A return will exit the loop and its enclosing function and can only be used if the loop is executing inside a function block otherwise the return is meaningless.
You can understand how the for() loop works by considering how it can be restated as a while() loop.
for(anInitializer; aCondition; aModifier)
Can be recast as:
anInitializer; while(aCondition) { someCode; aModifier; }
Although the items in the head of a for() iterator heading are optional, the semicolons must all be present to indicate the placement of any expressions in the heading.
There is an alternative construction called the for ... in ... statement which is especially useful for operating on objects. Refer to the for ... in ... topic for details.
At version 1.2 of JavaScript, a named continue can be used with this iterator. If the named continue is executed, control passes to the top of the named for loop. The increment expression is evaluated, then the test expression. If necessary the loop iterates once more.
// Reccomended form for(ii=0; ii<100; ii++) { document.write("-"); } // Possibly dangerous during maintenance for(ii=0; ii<100; ii++) document.write("-"); // Loop within a loop for(ii=0; ii<100; ii++) { for(jj=0; jj<ii; jj++) { document.write("-"); } document.write("<BR>"); } // Loop forever doing some task for(;;) { animateSpinningGraphic(); }
ECMA 262 edition 2 - section - 12.6.2
ECMA 262 edition 2 - section - 12.7
ECMA 262 edition 2 - section - 12.8
ECMA 262 edition 3 - section - 12.6.3
Wrox Instant JavaScript - page - 24
Prev | Home | Next |
FONT.size | Up | for( ... in ... ) ... |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |