Repeat/Until Block Loops

The repeat/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 repeat/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 repeat/until block loop, the while/do loop will evaluate a condition at the start of each iteration, thus providing a loop that can skip even the first iteration.

Syntax

The basic syntax for a repeat/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 repeat and until keywords.

Nullable Conditions

The condition expression for the repeat/until loop must be of Boolean or Nullable Boolean type.

If the condition is a simple boolean, the repeat/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 false, the repeat/until loop treats them the same, and will continue executing the loop if the condition evaluates to either nil or false. Only a value of true will terminate the loop.

This behavior symmetrically extends to if/then statements and while/do loops, which also treat a nil condition as equivalent to false.

repeat/until Loops and begin/end blocks.

Unlike most other statements, and all the other loop types, the repeat/until loop is a block statement, and encloses a list of statements, rather than looping an individual statement. As such, a separate or explicit begin/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, repeat/until loops can be exited prematurely using the break and exit statements, and a single loop iteration can be cut short by using the continue statement, which jumps to the next loop iteration.

See also