LOOP

Purpose:

Executes simple FOR-Loop iterative statements.

Syntax:

LOOP(var = array, statements)

LOOP (var = array) { statements; }

var

-

A variable used as the iteration index.

array

-

A series or array. If array is a single column series, var is assigned the next row element of array after every iteration. If array is a multi-column array, var is assigned the next column of array after each iteration.

statements

-

Any valid expressions separated by semicolons. The statements to execute after each iteration.

Example:

loop(j = 1..10, echo(j))

 

sets j equal to 1 and increments j by 1 until j equals 10 while echoing j to the status line.

Example:

The SPL function, WinSines:

 

WinSines()
{
    local i, N;
 
    N = numwin;
 
    loop (i = 1..N)
    {
        eval(sprintf("W%d := gsin(100,.01, %d)", i, i));
    }
}

 

increments local variable, i, and fills each Window in the Worksheet with a sinewave of the same frequency as the Window number. Note since i is declared as a local, it does not conflict with the built-in constant i == sqrt(-1).

Remarks:

The expression:

 

loop (j = M..N)
{
    statement1;
    statement2;
}

 

is equivalent to:

 

loop(j = M..N, statement1;statement2)

 

and equivalent to:

 

for (j = M; j <= N; j++)
{
    statement1;
    statement2;
}

 

Because LOOP is a simpler and less flexible iteration construct than FOR , LOOP generally runs faster than FOR. LOOP is preferred for simple iterations where the iteration index is not modified.

 

As mentioned, unlike FOR, the iteration index cannot be modified. However, the BREAK and CONTINUE statements can alter the iteration behavior.

 

See FOR or WHILE for more flexible iteration constructs.

 

For best performance, try to avoid loops altogether by exploiting the vectorized nature of SPL. For example:

 

y = {};
t = 0..0.01..1;
 
loop (n = 1..101)
{
    y[n] = sin(2*pi*10*t[n]);
}

 

can be performed much faster, more intuitively and concisely with:

 

t = 0..0.01..1;
y = sin(2*pi*10*t);

 

or even faster with:

 

y = gsin(101, .01, 10);

See Also:

BREAK

CONTINUE

FOR

SPL: Series Processing Language

WHILE