Previous Topic: LIST StatementNext Topic: MOVE Statement


LOOP Statement

The LOOP statement executes one or more statements repeatedly under the control of one or more conditions. You can also perform looping implicitly with the FOR EACH statement. (See the description of the FOR EACH statement.)

This statement has the following format:

[<<label>>]
   LOOP
       statements
   [ {WHILE}           ]
   [ {UNTIL} condition ] ...
   [    statements     ]
   ENDLOOP
[<<label>>]
   LOOP numeric_expression_1 TIMES
    statements
   [{WHILE}           ]
   [{UNTIL} condition ] ...
   [     statements   ]
   ENDLOOP
[<<label>>]
   LOOP VARYING identifier
       [FROM numeric_expression_2]
       [BY numeric_expression_3]
     [[UP  ]                          ]
     [[DOWN] THRU numeric_expression_4]
                 statements
[ {WHILE} condition ] ...
[ {UNTIL}           ]
[     statements    ]
   ENDLOOP
<<label>>

An optional 1‑ to 15‑character label for a LOOP construct. The label on the LOOP identifies the LOOP construct. You can use it to refer to the LOOP from other statements, such as the QUIT or PROCESS NEXT statements, or as the operand of certain functions, such as $COUNT.

WHILE

A condition that indicates that the loop executes repeatedly as long as the condition remains true. If the condition is false or unknown, the loop is terminated. You can use multiple WHILE clauses.

UNTIL

A condition that indicates that the loop executes repeatedly until the condition becomes true (as long as the condition remains false or unknown). You can use multiple UNTIL clauses.

numeric_expression_1 TIMES

Specifies the maximum number of times the loop executes. If the value of this expression is less than or equal to zero, no iterations are performed. If the TIMES clause results in a number that has decimal places, the number of iterations is rounded to the next higher integer.

VARYING clause

Specifies the identifier of a numeric field whose value is varied each time through the loop. There can be only one VARYING clause in a LOOP.

ENDLOOP

A reserved word that designates the end of a LOOP construct.

The numeric expressions used as arguments in this statement are not nullable.

In PDL, WHILE and UNTIL indicate whether to continue or to quit if the condition is true. WHILE and UNTIL imply nothing about testing before or after each iteration of the loop. The location of the tests in the loop is determined by the placement of the WHILE and UNTIL statements, as shown in the examples.

Statements can appear before and after a WHILE or UNTIL clause. Placement of the statements in relation to the tests affects whether the statements ever executes.

When a PROCESS NEXT statement is encountered in a loop, the current loop terminates execution and the loop is reiterated.

When a QUIT statement is encountered in a loop, execution continues with the statement that follows ENDLOOP.

In the following loop, the test is made before any statements are processed. Therefore, the statements cannot execute at all. When using UNTIL, no iterations of the loop are performed if the test is true. When using WHILE, no iterations of the loop are performed if the test is false or unknown.

LOOP
UNTIL/WHILE condition
  statements
ENDLOOP

In the following loop, the test is first made after the statements were processed for the first time. Therefore, the statements in the following LOOP execute at least once.

LOOP
  statements
UNTIL/WHILE condition
ENDLOOP

In a loop of the following form, the test is first performed after the first set of statements processes and can exit the LOOP before the second set of statements is processed.

LOOP
  statements_1
UNTIL/WHILE condition
  statements_2
ENDLOOP

In the following loop, the VARYING clause processes array items. This loop varies LOOP‑INDEX in descending order, beginning the process at 10 and continuing until 1 process. After the loop ends, LOOP‑INDEX has a value of zero.

LOOP
VARYING LOOP‑INDEX FROM 10 BY ‑1 DOWN THRU 1
    CALL CHECK USING A (LOOP‑INDEX)
ENDLOOP

You can use FROM, BY, and THRU clauses in any order.

The following is an infinite loop.

LOOP
VARYING LOOP‑INDEX FROM 100 BY 1 DOWN THRU 100
    statements
ENDLOOP

This loop repeatedly transmits a panel until you enter TRANSCODE T on the panel or press the PF3 key. If the TRANSMIT does not present the application with terminating data, TRANSCODE determines further processing on each subsequent LOOP iteration.

<<MAIN>> PROCEDURE
  LOOP
   TRANSMIT MAINPNL
  UNTIL $PF3
  UNTIL TRANSCODE = 'T'
  SET MAINPNL.MSG = ' '
  SELECT TRANSCODE
  WHEN 'A'
   DO ADD_REC
  WHEN 'B'
   DO DEL_REC
  WHEN OTHER
   DO OTHER_PROC
  ENDSEL
 ENDLOOP
ENDPROC

This loop processes a sequential file until the first header record (in a group of records with multiple types) is encountered. If currently positioned at a non‑header, records are read until a header is found.

<<POSITION‑HDR>>
  LOOP
     FOR NEXT MASTER‑FILE
        : BYPASS
     WHEN NONE
         QUIT POSITION‑HDR
     ENDFOR
  UNTIL MASTER‑FILE.RECORD‑TYPE = 'A'
  ENDLOOP
  DO PROCESS‑A

Examples

The following are further examples that show various positions of the loop termination test and expressions used for the VARYING clause.

LOOP
UNTIL BALANCE NOT > AMOUNT
   DO PRINT‑BALANCE
   SUBTRACT AMOUNT FROM BALANCE
ENDLOOP
LOOP VARYING I FROM 1 BY 1 THRU N
   MOVE STATE(I) TO X‑STATE
WHILE NOT ERROR‑COND
   DO PROC‑STATE
ENDLOOP
LOOP
   statements
WHILE condition‑1
   statements
WHILE condition‑2
   statements
ENDLOOP
LOOP VARYING I
      FROM X + 3
      BY 2
      THRU (A + B)/2
      statements
ENDLOOP