for( ... ) ... (Iterator)

An iterator mechanism - a loop construct.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera browser - 3.0
JavaScript syntax:-aLabel: for(anInitializer; aCondition; aModifier)
Argument list:aConditionAn expression that yields a Boolean value
aLabelAn optional label to name the iterator
aModifierModifies the value being enumerated
anInitializerAssigns 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.

Example code:

   // 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();

   }

See also:break, continue, do ... while( ... ), Flow control, for( ... in ... ) ..., Iteration statement, Label, Off by one errors, while( ... ) ...

Cross-references:

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