LOOP statement
Syntax
LOOP
[loop.statements]
[CONTINUE | EXIT]
[{WHILE | UNTIL} expression [DO] ]
[loop.statements]
[CONTINUE | EXIT]
REPEAT
Description
Use the LOOP statement to start a LOOP...REPEAT program loop. A program loop is a series of statements that executes for a specified number of repetitions or until specified conditions are met.
Use the WHILE clause to indicate that the loop should execute repeatedly as long as the WHILE expression evaluates to true (1). When the WHILE expression evaluates to false (0), repetition of the loop stops, and program execution continues with the statement following the REPEAT statement.
Use the UNTIL clause to put opposite conditions on the LOOP statement. The UNTIL clause indicates that the loop should execute repeatedly as long as the UNTIL expression evaluates to false (0). When the UNTIL expression evaluates to true (1), repetition of the loop stops, and program execution continues with the statement following the REPEAT statement.
If a WHILE or UNTIL expression evaluates to the null value, the condition is false.
expression can also contain a conditional statement. Any statement that takes a THEN or an ELSE clause can be used as expression, but without the THEN or ELSE clause. When the conditional statement would execute the ELSE clause, expression evaluates to false; when the conditional statement would execute the THEN clause, expression evaluates to true. A LOCKED clause is not supported in this context.
You can use multiple WHILE and UNTIL clauses in a LOOP...REPEAT loop. You can also nest LOOP...REPEAT loops. If a REPEAT statement is encountered without a previous LOOP statement, an error occurs during compilation.
Use the CONTINUE statement within LOOP...REPEAT to transfer control to the next iteration of the loop from any point in the loop.
Use the EXIT statement within LOOP...REPEAT to terminate the loop from any point within the loop.
Although it is possible to exit the loop by means other than the conditional WHILE and UNTIL statements (for example, by using GOTO or GOSUB in the DO statements), it is not recommended. Such a programming technique is not in keeping with good structured programming practice.
Examples
- Source Lines
- Program Output
- X=0 LOOP UNTIL X>4 DO PRINT "X= ",X X=X+1 REPEAT
X= 0 X= 1 X= 2 X= 3 X= 4- A=20 LOOP PRINT "A= ", A A=A-1 UNTIL A=15 REPEAT
A= 20 A= 19 A= 18 A= 17 A= 16- Q=3 LOOP PRINT "Q= ",Q WHILE Q DO Q=Q-1 REPEAT
Q= 3 Q= 2 Q= 1 Q= 0- EXECUTE "SELECT VOC FIRST 5" MORE=1 LOOP READNEXT ID ELSE MORE=0 WHILE MORE DO PRINT ID REPEAT
5 record(s) selected to SELECT list #0. LOOP HASH.TEST QUIT.KEY P CLEAR.LOCKS- EXECUTE "SELECT VOC FIRST 5" LOOP WHILE READNEXT ID DO PRINT ID REPEAT
5 record(s) selected to SELECT list #0. LOOP HASH.TEST QUIT.KEY P CLEAR.LOCKS