DO...UNTIL
Topic group Related topics Example
Executes the statements between DO and UNTIL at least once while a specified condition is false.
Syntax
DO
[ <statements> ]
UNTIL <condition expL>
<statements>
Zero or more statements executed in each iteration of the loop.
UNTIL<condition expL>
The statement that marks the end of the DO...UNTIL loop. <condition expL> is a logical expression that is evaluated after each iteration of the loop to determine whether the iteration should occur again. If it evaluates to false, the statements are executed. Once it evaluates to true, the loop is terminated and execution continues with the statement following the UNTIL.
Description
Use a DO...UNTIL loop to repeat a block of statements until a condition is true (in other words, while the condition is false). Because the condition is evaluated at the end of the loop, a DO...UNTIL loop always executes at least once, even when the condition is initially true.
You may also exit the loop with EXIT, or restart the loop with LOOP.
DO...UNTIL is rarely used. In most condition-based loops, you don’t want to execute the loop at all if the condition is initially invalid. DO WHILE loops are much more common, because they check the condition before they begin.
In a DO WHILE loop, the condition fails—that is, the loop should not be executed—when it evaluates to false; in a DO...UNTIL loop, the condition fails when it evaluates to true. This is simply the result of the wording of the looping commands. You can easily reverse any logical condition by using the logical NOT operator or the opposite comparison operator (for example, less than instead of greater than or equal, or not equal instead of equal).