COBOL PERFORM Loops
COBOL has no for or while keywords. All loops are forms of PERFORM, either running a paragraph repeatedly
(out-of-line) or running the statements up to END-PERFORM repeatedly (inline).
| Form | Like |
|---|---|
PERFORM n TIMES |
repeat n times |
PERFORM UNTIL condition |
while not condition (test before each pass) |
PERFORM WITH TEST AFTER UNTIL condition |
do … while not condition (runs at least once) |
PERFORM VARYING i FROM a BY b UNTIL condition |
for (i = a; !condition; i += b) |
PERFORM TIMES
IDENTIFICATION DIVISION.
PROGRAM-ID. TIMES-LOOP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COPIES PIC 9 VALUE 3.
PROCEDURE DIVISION.
PERFORM PRINT-LABEL WS-COPIES TIMES
PERFORM 2 TIMES
DISPLAY "-- inline --"
END-PERFORM
STOP RUN.
PRINT-LABEL.
DISPLAY "SHIP TO: TAD Market, Dock 4".SHIP TO: TAD Market, Dock 4
SHIP TO: TAD Market, Dock 4
SHIP TO: TAD Market, Dock 4
-- inline --
-- inline --
PERFORM UNTIL
The condition is tested before each pass, so the body may run zero times. Note that UNTIL means
"stop when true", the opposite of while:
IDENTIFICATION DIVISION.
PROGRAM-ID. UNTIL-LOOP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BALANCE PIC 9(5)V99 VALUE 1000.00.
01 WS-YEAR PIC 99 VALUE 0.
PROCEDURE DIVISION.
PERFORM UNTIL WS-BALANCE >= 1500
COMPUTE WS-BALANCE ROUNDED = WS-BALANCE * 1.08
ADD 1 TO WS-YEAR
END-PERFORM
DISPLAY "Balance passed 1500 after " WS-YEAR " years"
DISPLAY "Final balance: " WS-BALANCE
STOP RUN.Balance passed 1500 after 06 years
Final balance: 01586.88
WITH TEST AFTER checks the condition after each pass, so the body always runs at least once:
IDENTIFICATION DIVISION.
PROGRAM-ID. TEST-AFTER.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-N PIC 99 VALUE 50.
PROCEDURE DIVISION.
PERFORM UNTIL WS-N > 10
DISPLAY "Test before: never printed"
END-PERFORM
PERFORM WITH TEST AFTER UNTIL WS-N > 10
DISPLAY "Test after: printed once, N = " WS-N
END-PERFORM
STOP RUN.Test after: printed once, N = 50
PERFORM VARYING
VARYING sets a counter, runs the body, adds the BY step, and repeats UNTIL the condition is true:
IDENTIFICATION DIVISION.
PROGRAM-ID. VARYING-LOOP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-I PIC 99.
01 WS-SQUARE PIC 999.
01 WS-COUNTDOWN PIC S99.
PROCEDURE DIVISION.
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5
COMPUTE WS-SQUARE = WS-I * WS-I
DISPLAY WS-I " squared is " WS-SQUARE
END-PERFORM
DISPLAY "After the loop WS-I = " WS-I
PERFORM VARYING WS-COUNTDOWN FROM 10 BY -3
UNTIL WS-COUNTDOWN < 0
DISPLAY "Countdown " WS-COUNTDOWN
END-PERFORM
STOP RUN.01 squared is 001
02 squared is 004
03 squared is 009
04 squared is 016
05 squared is 025
After the loop WS-I = 06
Countdown +10
Countdown +07
Countdown +04
Countdown +01
Notice that after the loop WS-I is 6: it was incremented once more before the test failed. Code after
the loop must not assume the counter holds the last processed value.
Watch out: A loop counter must be big enough to hold the value that ends the loop. With
PIC 9andUNTIL WS-I > 9, the counter goes from 9 to 10, which truncates to 0, and the loop never ends.
Nested loops with AFTER
PERFORM VARYING … AFTER … runs nested counters in one statement (the AFTER counter varies fastest). Here is a
small multiplication table:
IDENTIFICATION DIVISION.
PROGRAM-ID. TIMES-TABLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-ROW PIC 99.
01 WS-COL PIC 99.
01 WS-PRODUCT PIC ZZ9.
PROCEDURE DIVISION.
PERFORM PRINT-CELL
VARYING WS-ROW FROM 1 BY 1 UNTIL WS-ROW > 3
AFTER WS-COL FROM 1 BY 1 UNTIL WS-COL > 4
DISPLAY " "
STOP RUN.
PRINT-CELL.
IF WS-COL = 1 AND WS-ROW > 1
DISPLAY " "
END-IF
COMPUTE WS-PRODUCT = WS-ROW * WS-COL
DISPLAY WS-PRODUCT WITH NO ADVANCING. 1 2 3 4
2 4 6 8
3 6 9 12
You can write the same with two nested inline PERFORM VARYING loops, which many people find easier to read.
Leaving a loop early
There is no break in standard COBOL loops. The usual approach is a flag in the UNTIL condition:
IDENTIFICATION DIVISION.
PROGRAM-ID. EARLY-EXIT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-I PIC 99.
01 WS-FOUND PIC X VALUE "N".
88 FOUND VALUE "Y".
PROCEDURE DIVISION.
PERFORM VARYING WS-I FROM 1 BY 1
UNTIL WS-I > 20 OR FOUND
IF FUNCTION MOD(WS-I, 7) = 0
SET FOUND TO TRUE
DISPLAY "First multiple of 7: " WS-I
END-IF
END-PERFORM
STOP RUN.First multiple of 7: 07
Test yourself
How many times does PERFORM UNTIL X > 5 run its body if X is already 9?
- Once
- Zero times
- Five times
- Forever
After PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3 … END-PERFORM, what is I?
- 3
- 4
- 1
- 0
Why can PERFORM VARYING I FROM 1 BY 1 UNTIL I > 9 with I PIC 9 loop forever?
- It cannot
- Because 9 + 1 truncates to 0 in PIC 9, so I is never greater than 9
- Because VARYING ignores UNTIL
- Because PIC 9 is alphanumeric