WHILE logicalExpression

The WHILE command, like the IF command, is a "block" command. That means that it controls a block, or sequence, of other commands.

The general form of the WHILE command is as follows:

WHILE logicalExpression
   command1
   command2
   ...
END

WHILE repeatedly executes the sequence of commands between the WHILE command and the corresponding END command as many times as necessary until the logical expression no longer evaluates to true. Obviously, some variable in the logical expression must be changed within the loop or the loop will never terminate.

If the logical expression evaluates to true, then the body of the WHILE command will be executed. Otherwise, the body of the WHILE command sequence will be skipped.

The logical expression may be a simple test, such as a <= 3. Or it may be a more complex expression such as a <=3 AND b between 1 10.

In general, a logical expression is a simple test or a number of simple tests combined using the logical operators NOT, AND, OR, or XOR. For a full explanation of logical expressions, see the section titled "Logical Expressions" in the latter part of Statistics101 Resampling Stats commands.

An early exit from a WHILE loop can be instigated by a BREAK command.

See also the BREAK, REPEAT, FOREACH, UNTIL, and IF commands.

The following program finds all the perfect squares below 1000 and generates a histogram from them.

It also demonstrates how to increment a counter in a loop.

COPY 1 number
COPY 1 numberSquared
WHILE numberSquared <1000
   SCORE numberSquared squares
   ADD number 1 number
   MULTIPLY number number numberSquared
END
HISTOGRAMPLOT squares

The above program produces the following output:

       Bin     Bin             Cum
Bin#   Start   Center  Freq    Freq    z
0      0.0     25.0    7       7       XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1      50.0    75.0    2       9       XXXXXXXXXXXX
2      100.0   125.0   3       12      XXXXXXXXXXXXXXXXXXX
3      150.0   175.0   2       14      XXXXXXXXXXXX
4      200.0   225.0   1       15      XXXXXX
5      250.0   275.0   2       17      XXXXXXXXXXXX
6      300.0   325.0   1       18      XXXXXX
7      350.0   375.0   1       19      XXXXXX
8      400.0   425.0   2       21      XXXXXXXXXXXX
9      450.0   475.0   1       22      XXXXXX
10     500.0   525.0   1       23      XXXXXX
11     550.0   575.0   1       24      XXXXXX
12     600.0   625.0   1       25      XXXXXX
13     650.0   675.0   1       26      XXXXXX
14     700.0   725.0   1       27      XXXXXX
15     750.0   775.0   1       28      XXXXXX
16     800.0   825.0   1       29      XXXXXX
17     850.0   875.0   0       29      
18     900.0   925.0   1       30      XXXXXX
19     950.0   975.0   1       31      XXXXXX