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:
While( condition, body );
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;
// loop 1:
y = 1;
While( y < x, y *= 2 );
Show( y );
// loop 2:
k = 0;
While( 2 ^ k < x, k++ );
Show( 2 ^ k );
x = 287; |
Sets x to 287.
|
// loop 1 |
|
y = 1; |
Sets y to 1.
|
While( |
Begins the While() loop.
|
y < x, |
|
y *= 2 |
|
); |
|
Show(y); |
Shows the value of y (512).
|
// loop 2 |
|
k = 0; |
Sets k to 0.
|
While( |
Begins the While() loop.
|
2 ^ k < x, |
Raises 2 to the exponent power of k and continues evaluating the loop as long as the result is less than 287.
|
k++ |
|
); |
|
Show(2 ^ k); |
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.