The UNTIL command is a looping command, similar to the WHILE command. The main difference is that the UNTIL command will execute the commands between UNTIL and END one or more times until its logical expression is true, whereas the WHILE command executes its block of commands zero or more times as long as its logical expression is true. The UNTIL command evaluates its logical expression following each execution of its block, whereas the WHILE command evaluates its expression prior to each execution.
The UNTIL 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 UNTIL command is as follows:
UNTIL logicalExpression command1 command2 ... END
UNTIL repeatedly executes the sequence of commands between the UNTIL and the corresponding END command as many times as necessary until the logical expression evaluates to true. Obviously, some variable in the logical expression must be changed within the loop or the loop will never terminate.
No matter what the value of the logical expression is at the start, the loop will execute its sequence of commands at least once. If the logical expression evaluates to false, then the body of the UNTIL command will be executed again. Otherwise, the UNTIL command will terminate and control will pass to the command following the END command.
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 UNTIL loop can be instigated by a BREAK command.
The following example program finds all the perfect squares below 1000 and generates a histogram from them:
COPY 1 number COPY 1 numberSquared UNTIL numberSquared > 1000 SCORE numberSquared squares LET number = number + 1 LET numberSquared = square(number) END LET numberOfSquares = size(squares) PRINT numberOfSquares HISTOGRAM squares