Navigation: Language Reference > 12 - Execution Control > Control Structures >====== LOOP (iteration structure) ====== | |
label | LOOP [ | count TIMES | ] | |
i = initial TO limit [ BY step ] | ||||
UNTIL logical expression | ||||
WHILE logical expression | ||||
statements | ||||
END | ||||
UNTIL | logical expression | |||
WHILE | logical expression |
LOOP | Initiates an iterative statement execution structure. |
count | An integer constant, variable, or expression specifying the number of TIMES statements in the LOOP execute. |
TIMES | Executes count number of iterations of the statements. |
i | The label of a variable which automatically increments (or decrements, if step is negative) on each iteration. |
= initial | A numeric constant, variable, or expression specifying the value of the increment variable ( i ) on the first pass through the LOOP structure. |
TO limit | A numeric constant, variable, or expression specifying the terminating value for the LOOP. When i is greater than limit (or less than, if the step is a negative value) the LOOP structure control sequence terminates. The i variable contains the last incremental value greater than (or less than) the limit after the LOOP terminates. Functions used in expressions used as a limit are only evaluated once (unlike the conditions of UNTIL and WHILE below). |
BY step | A numeric constant, variable, or expression specifying the quantity by which the i variable increments (or decrements, if the value is negative) on each iteration of the LOOP. If BY step is omitted, i increments by 1. |
UNTIL | When placed on the LOOP statement, UNTIL evaluates the logical expression before each iteration. When terminating the LOOP structure, UNTIL evaluates the logical expression after each iteration. If the logical expression evaluates to true, the LOOP terminates. |
WHILE | When placed on the LOOP statement, WHILE evaluates the logical expression before each iteration. When terminating the LOOP structure, WHILE evaluates the logical expression after each iteration. If the logical expression evaluates to false, the LOOP terminates. |
logical expression | A numeric or string variable, expression, or procedure. A logical expression evaluates a condition. Control is determined by the result (true or false) of the expression. A zero numeric or blank string value evaluates as false, anything else is true. |
statements | An executable statement, or a sequence of executable statements. |
A LOOP structure repetitively executes the statements within its structure. LOOP structures may be nested within other executable code structures. Other executable code structures may be nested within a LOOP structure. Each LOOP structure must terminate with an END statement (or period), an UNTIL, or a WHILE statement.
A LOOP with no condition at the top or bottom iterates continuously until a BREAK or RETURN statement executes. BREAK discontinues the LOOP and continues program execution with the statement following the LOOP structure. All statements within a LOOP structure executes unless a CYCLE statement executes. CYCLE immediately sends program execution back to the top of the LOOP for the next iteration, without executing any further statements in the LOOP following the CYCLE.
LOOP UNTIL or LOOP WHILE logical expressions are always evaluated at the top of the LOOP, before the LOOP statements execute. Therefore, if the logical expression is false on the first pass, the LOOP statements will not execute even once. To create a LOOP that always executes its statements at least once, the UNTIL or WHILE clause must terminate the LOOP structure.
Example:
LOOP !Continuous loop
Char = GetChar() !get a character
IF Char <;> CarrReturn !if it's not a carriage return
Field = CLIP(Field) & Char !append the character
ELSE !otherwise
BREAK !break out of the loop
END !End if
END !end loop
IF ERRORCODE() !On error
LOOP 3 TIMES !loop three times
BEEP !sound the alarm
END !End loop
END !end if
LOOP I# = 1 TO 365 BY 7 !Loop, increment I# by 7 each time
GET(DailyTotal,I#) !read every 7th record
DO WeeklyJob
END !I# contains 372 when the LOOP terminates
LOOP I# = 10 TO 1 BY -1 !Loop, decrementing I# by 1 each time
DO SomeRoutine
END !I# contains zero (0) when the LOOP terminates
SET(MasterFile) !Point to first record
LOOP !Process all the records
NEXT(MasterFile) !read a record
IF ERRORCODE() = 33 THEN BREAK. !End of file check
ProcMaster !call the procedure
END
LOOP WHILE KEYBOARD() !Empty the keyboard buffer
ASK !without processing keystrokes
UNTIL KEYCODE() = EscKey !but break the loop for Escape
See Also: