## Iterative Statements

C/C++ like while and for loop statements are available. While loops assume the following form:

while (expression) statement

The while construct evaluates statement repeatedly while expression is non-zero. For example:

while (j < 10) j++;

Compound statements must be enclosed in { }. Example:

a = 0;

j = 1;

while (j < 10)

{

a = a + j;

j += 10;

}

The do statement has the following form:

do statement while (expression);

The statement is executed repeatedly until expression becomes zero. The statement is always executed at least once.

C/C++ like for loops are supported as follows:

for (expr1; expr2; expr3) statement

In the for loop, expr1 is evaluated first and serves as an initializer statement. The second expression, expr2, is tested before each iteration of statement. If expr2 is non-zero, statement is evaluated. The third expression, expr3, is evaluated after each iteration of statement. Example:

for (j = 1; j <= 10; j++) echo(j);

As with while, compound statements must be enclosed in { }.

a = 0; b = 0;

for (j = 1; j <= 10; j++)

{

a += j;

b += a;

}

The previous example is equivalent to:

a = 0; b = 0;

j = 1;

while (j <= 10)

{

a += j;

b += a;

j++;

}

Loops may be nested.

for (j = 0; j < 10; j++)

{

for (k = 1; k <= 10; k++)

{

a += (j * k);

}

}

The simple loop iterator has the following form:

loop (var = array) statement

Loop assigns a loop or iteration variable to the value of each row element of a series and executes the loop body after each iteration.

/* use loop to create Fibonocci numbers > 2 */

fibl(n)

{

local fib;

fib = {1, 1};

loop (j = 3..n)

{

fib[j] = fib[j-1] + fib[j-2];

}

return(fib);

}

The array can be any valid array. If the array has only one column, the iteration variable is assigned the next row element of the array after each iteration. If the array is multi-column, the variable is assigned the next column of the array after each iteration.

Unlike for or while, the loop variable cannot be modified during the iterations.

Because loop is a simpler and less flexible iteration construct, loop generally executes faster than for or while and is preferred for simple iterations where the loop variable is not modified.