For …

The 'for …' loop statement is similar to the 'while …' loop but is a bit more complicated. Like the 'while ...' it also allows the same statement or block to be repeated. Usually a count is kept to make sure that the repetition doesn't go on forever. The syntax is:

for ( setup; condition; change )
    statement or block

The statement or block part is the repeated part. 'setup' is a JavaScript statement that occurs before the first repetition occurs. 'condition' is a JavaScript expression (usually a condition) that is evaluated before the repeated part is started. If it evaluates to true, then another repetition starts. 'change' is a JavaScript statement that occurs just before the condition is re-checked, and which usually effects whether the condition will pass or not. These examples show a very common idiom using the 3 parts within the brackets of the for statement to set, test and update a counter which controls the number of repeats:

var fruit = Array(3);
fruit[0] = 'apple';
fruit[1] = 'pear';
fruit[2] = 'orange';

for (count=0; count <=2; count++)      // display all fruit.
    document.write(fruit[count]+ ' ');

var new_line = '<BR>';
for (loop1=0; loop1 < 5; loop1++)      // display a triangle of T's.
{
    for (loop2=0; loop2 < loop1; loop2++)
    {
      document.write('T');
    }
    document.write(new_line);
}

for (;;)                  // do nothing forever.
   ;

The first loop produces this output:

apple pear orange

The second loop, which has a further loop inside it, produces this output:

T<BR>
TT<BR>
TTT<BR>
TTTT<BR>
TTTTT<BR>

The last loop produces nothing and never ends.

Zero or more of the three parts can be left out if they're not needed, but for this command, there must always be exactly two semi-colons between the for parentheses, no carefree omissions allowed.

There is a second form of the for statement that is specifically for objects. See the objects section for details.

The only other standard flow-control features are the continue statement and the break statement.

Both should only appear inside a block that is the repeated part of a for or while statement. These statements give finer control over how a block of statements is repeated. Break causes any repetition to stop immediately with no further repetitions. Continue causes the current repetition to stop immediately, but the test for another repetition still goes ahead. Some examples:

var loop = 0;               // Example 1. Display all multiples of 7.
while ( ++loop < 200 )
{
    if ( loop % 7 != 0 )
     continue;
    document.write(loop + ' ');
}

for (loop=1000; loop < 1099; loop++)   // Example 2. Find the first number bigger
{                     // than 1000 that is divisible by 99.
    if (loop % 99 == 0)
     break;
}
document.write(loop);

In the 'while' loop, the loop variable will increment through all the values from 0 to 199. The first thing that happens in the loop is the 'if' test, which will pass for all numbers not divisible by seven. Therefore, the branch or body of the 'if' statement will execute. This is the 'continue' statement, which sends control back to the while statement. Therefore, the number will only be written out if the if statement fails, which occurs when the number IS divisible by seven.

In the 'for' loop, the loop variable will increment through all the values from 1000 to 1098 in the ordinary case. However, if the 'if' statement is ever satisfied, the 'break' statement will execute. When or if that occurs, control will pass to the statement immediately following the 'for' statement. So in this case, either 1099 or the last value of the loop variable is written out. Of course, the number written out will be 1089 ( = 11 times 99 ).

© 1997 by Wrox Press. All rights reserved.