Skip to main content
Previous section   Next section

LOOP...REPEAT

Repeats a block of statements while a condition is true or until a condition becomes true.

Synopsis

LOOP [{WHILE | UNTIL} condition [DO] ]
statements
REPEAT

LOOP statements
[WHILE | UNTIL} condition [DO] ]
REPEAT

Arguments

condition Optional — Numeric or string expression that evaluates to True or False. Loop repeats either WHILE condition is True, or UNTIL condition is True. If this clause is omitted, an infinite loop occurs.
statements One or more statements that are repeated while or until condition is True.

Description

The LOOP...REPEAT statement is a flow-of-control statement that repeats a block of program statements zero or more times. The loop is performed either UNTIL condition becomes true, or WHILE condition remains true. The two syntax forms are equivalent.

The REPEAT keyword is mandatory, signalling the end point of the loop. The DO keyword is optional; if specified it must be on the same line as the condition clause.

You can use the CONTINUE statement to cause execution to jump to the next iteration of the loop.

LOOP...REPEAT statements can be nested.

Examples

The following examples illustrate use of the LOOP...REPEAT statement. All four examples are exactly equivalent; each executes the loop 10 times:

x=0
LOOP UNTIL x=10
  PRINT RND(100)
  ! Generate a random number between 1 and 100
  x=x+1
REPEAT
Copy code to clipboard
x=0
LOOP WHILE x<10
  PRINT RND(100)
  ! Generate a random number between 1 and 100
  x=x+1
REPEAT
Copy code to clipboard
x=0
LOOP
  PRINT RND(100)
  ! Generate a random number between 1 and 100
  x=x+1
  UNTIL x=10
REPEAT
Copy code to clipboard
x=0
LOOP
  PRINT RND(100)
  ! Generate a random number between 1 and 100
  x=x+1
  WHILE x<10
REPEAT
Copy code to clipboard

See Also

Previous section LOCK    MATNext section