REPEAT Statement
REPEAT subject_statement UNTIL condition_expr
The REPEAT statement repetitively executes its subject statement until a condition is true. The condition is checked after the subject statement is executed. Therefore, the subject statement is always executed at least once.
Below are some examples of the use of the REPEAT statement:
A = 1
REPEAT A = A * 2 UNTIL A GT B
This code finds the smallest power of 2 that is greater than B. The subject statement may also be in the form of a block, as shown in the following block of code that sorts an array:
REPEAT BEGIN
NOSWAP = 1
; Init flag to true.
FOR 1 = 0, N - 2 DO IF ARR(I) GT ARR(I + 1)
THEN BEGIN
NOSWAP = 0
; Swapped elements, clear flag.
T = ARR(I)
ARR(I) = ARR(I + 1)
ARR(I + 1) = T
ENDFOR
; Keep going until nothing is moved.
ENDREP UNTIL NOSWAP
The above example sorts the elements of ARR using the inefficient bubble sort method. A more efficient way to sort array elements is to use the SORT function.
note | The ending statement for a REPEAT loop is ENDREP, not ENDREPEAT. |