Routines (alphabetical) > Routines: R > REPEAT...UNTIL

# REPEAT...UNTIL

REPEAT...UNTIL loops are used to repetitively execute a 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. See Definition of True and False for details on how the “truth” of an expression is determined.

## Syntax

REPEAT statement UNTIL expression

or

REPEAT BEGIN

statements

ENDREP UNTIL expression

## Examples

The following example finds the smallest power of 2 that is greater than B:

A = 1

B = 10

REPEAT A = A * 2 UNTIL A GT B

The subject statement can also be in the form of a block:

A = 1

B = 10

REPEAT BEGIN

A = A * 2

ENDREP UNTIL A GT B

The next example sorts the elements of ARR using the inefficient bubble sort method.

;Sort array.

REPEAT BEGIN

;Set flag to true.

NOSWAP = 1

FOR I = 0, N - 2 DO IF arr[I] GT arr[I + 1]THEN BEGIN

;Swapped elements, clear flag.

NOSWAP = 0

T = arr[I] & arr[I] = arr[I + 1] & arr[I + 1] = T

ENDIF

;Keep going until nothing is moved.

ENDREP UNTIL NOSWAP

Tip: A more efficient way to sort elements is to use the SORT function.

## Version History

Original | Introduced

## See Also

BEGIN...END, BREAK, CASE, CONTINUE, FOR, FOREACH, GOTO, IF...THEN...ELSE, SWITCH, WHILE...DO

IDL Programming