Iteration statement (Definition)

A block of repeating code - a loop.

Availability:

ECMAScript edition - 2

An iteration statement executes a body of code iteratively - that is, over and over again, while a certain condition is true or until a terminating condition becomes true.

An iteration statement consists of a header and a body. The header is a keyword and a set of control parameters in parentheses. The body is a statement block enclosed in braces.

The following iteration statements are supported by ECMA compliant implementations:

All of these loop-controlling statements test the condition each time round the loop. In the case of some iterators, the test happens before the code is executed. For others it happens after the code has been executed.

An infinite loop is one for which the terminating condition never happens or where the iterating condition remains true forever.

Here are some infinite loops:

   for(;;)

   {

   

   // do this forever

   

   }

   

   while(true)

   

   {

   

   // do this forever

   

   }

   

You can break out of a loop with a break or return. The return statement should only be used when the loop is contained in a called function body. You can call the next iteration prematurely with a continue statement.

Warnings:

See also:break, continue, for( ... ) ..., for( ... in ... ) ..., return, Statement, while( ... ) ...

Cross-references:

ECMA 262 edition 2 - section - 12.6

ECMA 262 edition 3 - section - 12.6

Wrox Instant JavaScript - page - 25