A related function is While(), which repeatedly tests the condition and evaluates its body script as long as the condition is true. The syntax is:
For example, here are two different programs that use a While() loop to find the least power of 2 that is greater than or equal to x (287). The result of both programs is 512.
x = 287;
y = 1;
While( y < x, y *= 2 );
Show( y );
k = 0;
While( 2 ^ k < x, k++ );
Show( 2 ^ k );
Sets x to 287.
|
|
Sets y to 1.
|
|
Begins the While() loop.
|
|
Shows the value of y (512).
|
|
Sets k to 0.
|
|
Begins the While() loop.
|
|
Raises 2 to the exponent power of k and continues evaluating the loop as long as the result is less than 287.
|
|
As with For() loops, While() loops that always evaluate as true create an infinite loop, which never stops. To stop the script, press ESC on Windows (or COMMAND-PERIOD on Macintosh). You can also select Edit > Stop Script. On Macintosh, Edit > Stop Script is available only when the script is running.