PERFORM loops

PERFORM TIMES

PERFORM n TIMES runs a sentence or paragraph a fixed number of times. Use it when the count is known before the loop starts:

PERFORM 5 TIMES DISPLAY "Payroll run" END-PERFORM.

PERFORM UNTIL

PERFORM UNTIL loops while a condition is false, and checks the condition before each pass. The counter must be set before the loop and advanced inside it:

MOVE 1 TO WS-I. PERFORM UNTIL WS-I > 10 DISPLAY "Row " WS-I ADD 1 TO WS-I END-PERFORM.

PERFORM VARYING

PERFORM VARYING folds the counter into the loop itself. FROM sets the start, BY the step, and UNTIL the end condition:

PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10 DISPLAY "Row " WS-I END-PERFORM.

The code on this page was run with GnuCOBOL 3.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev