Repeat/Until Block Loops
until loop is a loop that executes a block of statements repeatedly, until a given condition evaluates to
true. The condition will be re-evaluated at the end of each iteration of the loop, allowing code inside the loop to affect the condition in order to terminate it.
Since the condition is evaluated at the end of each iteration, a
until loop will always be executed at least once, even if the condition is already
true when execution arrives at the loop.
As an alternative to the
until block loop, the
do loop will evaluate a condition at the start of each iteration, thus providing a loop that can skip even the first iteration.
The basic syntax for a
until loop looks like this:
repeat DoSomething(); DoSomethingElse(); until x ≥ 10;
where a conditional expression is specified after the closing
until keyword, and a list of statements can be provided between the
If the condition is a simple boolean, the
until loop will execute as long as the condition evaluates to
false (in other words until it is true).
If the condition is a Nullable Boolean type, then the additional case of the condition evaluating to
nil needs to be considered. While a
nil nullable boolean strictly speaking is not equivalent to
until loop treats them the same, and will continue executing the loop if the condition evaluates to either
false. Only a value of
true will terminate the loop.
until Loops and
Unlike most other statements, and all the other loop types, the
until loop is a block statement, and encloses a list of statements, rather than looping an individual statement. As such, a separate or explicit
end block statement is not necessary in order to execute a loop with two or more statements.
Prematurely Exiting the Loop or a Loop Iteration
Like all loops,
until loops can be exited prematurely using the
exit statements, and a single loop iteration can be cut short by using the
continue statement, which jumps to the next loop iteration.