Coding a loop

Use the PERFORM . . . TIMES statement to execute a procedure a specified number of times.

About this task


PERFORM 010-PROCESS-ONE-MONTH 12 TIMES
INSPECT . . .

In the example above, when control reaches the PERFORM statement, the code for the procedure 010-PROCESS-ONE-MONTH is executed 12 times before control is transferred to the INSPECT statement.

Use the PERFORM . . . UNTIL statement to execute a procedure until a condition you choose is satisfied. You can use either of the following forms:


PERFORM . . . WITH TEST AFTER  . . . . UNTIL . . .
PERFORM . . . [WITH TEST BEFORE] . . . UNTIL . . . 

Use the PERFORM . . . WITH TEST AFTER . . . UNTIL statement if you want to execute the procedure at least once, and test before any subsequent execution. This statement is equivalent to a do-until structure:

This image depicts the processing flow of PERFORM with TEST AFTER. Link to detail.

In the following example, the implicit WITH TEST BEFORE phrase provides a do-while structure:


PERFORM  010-PROCESS-ONE-MONTH
  UNTIL MONTH GREATER THAN 12
INSPECT . . .

When control reaches the PERFORM statement, the condition MONTH GREATER THAN 12 is tested. If the condition is satisfied, control is transferred to the INSPECT statement. If the condition is not satisfied, 010-PROCESS-ONE-MONTH is executed, and the condition is tested again. This cycle continues until the condition tests as true. (To make your program easier to read, you might want to code the WITH TEST BEFORE clause.)

This image depicts the processing flow of PERFORM with TEST BEFORE, as described in the preceding procedure.