LOOP Statement
Constructs program loops, using WHILE and/or UNTIL conditions.
Syntax
LOOP
{VARYING counter.var = start.expr
{STEP step.expr}}
{statement{;statement}...}
{
{WHILE limit-expr {DO}
{statement{;statement}...}}
{UNTIL limit-expr {DO}
{statement{;statement}...}}
}...
REPEAT
Syntax Elements
counter.var is a variable for counting interations through the loop.
start.expr is an expression, the value of which is to be used as the starting counter value in counter.var.
step.expr is an expression, the value of which is to be added to the counter value in counter.var each time through the loop. If an expression is not specified, the default is 1.
limit-expr is an expression that can be evaluated to true or false (1 or 0). It is similar to the test expression in the IF statement.
statement is any valid DataBasic statement.
Operation
Any statement(s) following LOOP are executed first. Then limit-expr is evaluated.
If limit-expr following WHILE evaluates to true (nonzero), the statement(s) following DO, if any, are executed and control goes back to the beginning of the loop. If limit-expr evaluates to false (zero), control passes to the next sequential statement following REPEAT.
If limit-expr following UNTIL evaluates to false (zero), the statement(s) following DO, if any, are executed and control goes back to the beginning of the loop. If limit-expr evaluates to true (nonzero), control passes to the next sequential statement following REPEAT.
Statements used within the loop can be placed on one line separated by semicolons, or they can be placed on multiple lines.
Because the WHILE and UNTIL clauses require a logical condition (one which evaluates to 0 or 1), the compiler allows a READNEXT, READPREV or LOCATE statement to provide this condition. These statements always returns a 0 or 1, depending on whether or not another item-id or the locate string is found.
The VARYING clause provides a running counter without specification of a termination value.
Loop Control Statements
The following statements can be used within a LOOP construct to control loop execution:
-
BREAK or EXIT - Exits the loop immediately and continues from the statement following the appropriate REPEAT statement.
-
CONTINUE - Branches to the start of the loop in the same way as a REPEAT statement. Note, however, that CONTINUE can be used anywhere in the loop to force the next iteration to start immediately.
Note
The BREAK, EXIT and CONTINUE statements can be used only within loop constructs. If you use any of these outside a loop, your program will not compile.
Examples
A=0
LOOP UNTIL A=4 DO A=A+1; PRINT A REPEAT
Prints sequential values of A from 1 through 4 (loop executes 4 times).
J=0
LOOP
PRINT J
J=J+1
WHILE J<4 DO REPEAT
Prints sequential values of J from 0 through 3 (loop executes 4 times).
X=100
LOOP X=X-10 WHILE X>40 DO PRINT X REPEAT
Prints values of X from 90 down through 50 in increments of -10, so the loop executes 5 times.
Q=6
LOOP Q=Q-1 WHILE Q DO
PRINT Q
REPEAT
Prints value of Q in this order: 5, 4, 3, 2, 1.
B=1
LOOP UNTIL B=6 DO
B=B+1
PRINT B
REPEAT
Prints values of B from 2 through 6, as loop executes 5 times.
LOOP I=I+1 WHILE READNEXT ID DO REPEAT
Increments I as long as item-ids are read from select-list.
LOOP VARYING INPCTR = 1 WHILE INPCTR LT 21 PRINT "NUMBER";INPUT INPVAL UNTIL INPVAL = "" DO INPTAB(INPCTR) = INPVAL REPEAT INPTABMAX=INPCTR-1
Inputs values and stores them in table INPTAB until 20 values have been input or null is input. Variable INPTABMAX will contain the number of values input.
LOOP VARYING INPCTR = 1 IF INPCTR GE 21 THEN BREAK PRINT "NUMBER";INPUT INPVAL IF INPVAL = "" THEN BREAK INPTAB(INPCTR) = INPVAL REPEAT INPTABMAX=INPCTR-1
Does the same as the previous example, but uses BREAK statements to exit from the loop.
LOOP VARYING X IF X GT LISTSIZE THEN BREAK IF LIST1<X> EQ "" THEN CONTINUE LIST2<X> = LIST1<X> REPEAT
Copies LIST1 to LIST2, skipping null elements.