DADiSP Worksheet Functions > Function Categories > Control Flow > WHILE

 

WHILE

Purpose:

Evaluates an expression while the condition is non-zero.

Syntax:

WHILE(expr, statements)

WHILE (expr) { statements; }

expr

-

Any valid expression that evaluates to a scalar.

statements

-

Any valid statements separated by semicolons to evaluate while expr is non-zero.

Returns:

Result of statements.

Example:

while(max(W0) < 10.0, deriv(W0))

 

differentiates the current Window until the maximum value is greater than 10.0

Example:

while(max(a) < 25, a *= 2)

 

multiplies variable a by 2 until the resulting value is greater than 25.

Example:

while (max(a) < 25)
{
    a *= 2;
}

 

same as above, except in SPL form.

Example:

f := 1.0
W1: gsin(100,.01,f);label(sprintf("Frequency: %g", f))
W2: spectrum(W1, 1024)
f:=1;while(f <= 100,  f++)

 

W2 displays a remarkably simple demonstration of aliasing errors due to undersampling the sinewave in W1.

Remarks:

See LOOP for a faster but less flexible iteration construct.

 

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

 

y = {};
t = 0..0.01..1
n = 1;
 
while (n <= 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

IF

LOOP

RETURN

SPL: Series Processing Language