PERFORM VARYING
PERFORM VARYING is the for loop of COBOL: it names a counter, its first value, its step and the condition that stops it. The statements live in the same block as before, so a reader who knows UNTIL already knows most of it.
FROM, BY and UNTIL
The clause is read as: set the counter FROM its first value, run the body, add the BY step, and repeat while the UNTIL condition is false:
IDENTIFICATION DIVISION.
PROGRAM-ID. VARYING.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 I-F PIC 9(2) VALUE 0.
01 TOTAL-F PIC 9(4) VALUE 0.
PROCEDURE DIVISION.
PERFORM VARYING I-F FROM 1 BY 2 UNTIL I-F > 7
ADD I-F TO TOTAL-F
DISPLAY "i " I-F " total " TOTAL-F
END-PERFORM.
DISPLAY "after the loop, i is " I-F.
STOP RUN.
cobc -x -free -Wall -o varying varying.cob
./varying
i 01 total 0001
i 03 total 0004
i 05 total 0009
i 07 total 0016
after the loop, i is 09
The step is two, so the body saw the odd values only, and the total is the sum of the odd numbers up to seven. The counter is an ordinary field of the program, PIC 9(2) here, and the loop wrote the last value into it after the body had finished with 7: that is why it is 9 after the statement.
A step that is not one
BY takes any numeric value, and the counter does not have to start at one. A step of two is how a program walks the even and the odd positions of a record, and a start other than one is how it skips a header. Both are the same clause:
PERFORM VARYING I-F FROM 10 BY 10 UNTIL I-F > 40
DISPLAY "block " I-F
END-PERFORM.
That fragment is not a whole program and the checker does not build it. Its shape is the one of the run above: five lines, from 10 to 40, and a counter left at 50.
A countdown with BY -1
A negative step counts down, and the counter has to be signed to hold the value below zero that the statement leaves in it:
IDENTIFICATION DIVISION.
PROGRAM-ID. COUNTDOWN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 K-F PIC S9(2) VALUE 0.
PROCEDURE DIVISION.
PERFORM VARYING K-F FROM 5 BY -1 UNTIL K-F < 1
DISPLAY "k " K-F
END-PERFORM.
DISPLAY "after the loop, k is " K-F.
STOP RUN.
cobc -x -free -Wall -o countdown countdown.cob
./countdown
k +05
k +04
k +03
k +02
k +01
after the loop, k is +00
A signed field prints its sign, so every line has a plus in front of the digits. The loop ended at zero and not at minus one, because the condition K-F < 1 is already true when the counter reaches zero, so the step from 1 to 0 ends the loop before a negative value is reached.
AFTER and two counters
A second counter is declared with AFTER, and it runs its whole cycle inside each iteration of the first one:
IDENTIFICATION DIVISION.
PROGRAM-ID. NESTED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ROW-F PIC 9 VALUE 0.
01 COL-F PIC 9 VALUE 0.
PROCEDURE DIVISION.
PERFORM VARYING ROW-F FROM 1 BY 1 UNTIL ROW-F > 3
AFTER COL-F FROM 1 BY 1 UNTIL COL-F > 2
DISPLAY "row " ROW-F " col " COL-F
END-PERFORM.
DISPLAY "done, row " ROW-F " col " COL-F.
STOP RUN.
cobc -x -free -Wall -o nested nested.cob
./nested
row 1 col 1
row 1 col 2
row 2 col 1
row 2 col 2
row 3 col 1
row 3 col 2
done, row 4 col 1
Six lines for a grid of three rows and two columns, and the inner counter starts again at one for every row, because FROM is applied each time the AFTER clause is reached.
What the counters hold afterwards
The last line of the run is the one to remember: the outer counter is 4, one step past its limit, and the inner counter is 1, its FROM value. The compiler incremented the inner counter to 3, ended its loop, and then reset it when the outer loop ended, so the value after the statement is not the value of the last iteration.
This is why a program does not read a counter after the loop to find out how many records it processed. The count belongs in a field that the body increments, as in the lesson on a running total, and the counter belongs to the loop. The next lesson performs a range of paragraphs, which is the form most old code uses and the one that needs the most care.
Exercises
- Change the first program to a step of three and start at 2. Which values does the body see, and what is the total?
- Run the countdown with UNTIL K-F = 0 instead of < 1. Does the loop still end, and where?
- Make the grid program print the product of the two counters instead of the two numbers. Which line shows 6?
- Add a third counter with a second AFTER and run the grid again. How many lines print for three rows, two columns and two layers?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the VARYING clause do before the first iteration?
Answer
It stores the FROM value in the counter
The counter is set from FROM once, then the body runs, and the step is added after each iteration.
-
What does the counter hold after PERFORM VARYING I FROM 1 BY 2 UNTIL I > 7 ends?
Answer
9: the last value plus one more step
The step is added after the body, so 7 became 9 and the condition ended the loop there.
-
What does AFTER declare?
Answer
A second counter that runs its whole cycle inside each pass of the first
The grid printed six lines, the inner counter restarting at one for each row.
-
Why is a counter not a safe record count after the loop?
Answer
It holds a value past the last iteration, and an inner counter is reset
The outer counter ends one step past the limit and the inner one at its FROM value, neither of which is a count.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 I-F PIC 9(2) VALUE 0. 01 T-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING I-F FROM 1 BY 1 UNTIL I-F IS GREATER THAN 3 ADD I-F TO T-F END-PERFORM. DISPLAY T-F. STOP RUN.Answer
006
The body adds 1, 2 and 3, and the total prints in the three digits of its picture.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 R-F PIC 9 VALUE 0. 01 C-F PIC 9 VALUE 0. 01 N-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING R-F FROM 1 BY 1 UNTIL R-F IS GREATER THAN 2 AFTER C-F FROM 1 BY 1 UNTIL C-F IS GREATER THAN 3 ADD 1 TO N-F END-PERFORM. DISPLAY N-F. STOP RUN.Answer
06
Two rows of three columns give six passes through the body.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 I-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING I-F FROM 10 BY 10 UNTIL I-F IS GREATER THAN 20 END-PERFORM. DISPLAY I-F. STOP RUN.Answer
30
The body is empty, the counter goes 10, 20, 30, and the loop stops when 30 is greater than 20.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 K-F PIC S9(2) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING K-F FROM 3 BY -1 UNTIL K-F IS LESS THAN 1 DISPLAY "k " K-F END-PERFORM. STOP RUN.Answer
k +03 / k +02 / k +01
The signed field prints a plus, and the condition stops the loop when the counter reaches zero.
-
A program uses a counter after PERFORM VARYING to report how many rows it read. What is wrong?
Answer
The counter ends one step past the last row, so the report is too large
The step is applied before the condition is tested again, so the counter ends past the limit; a separate count field is the fix.
-
Why does the inner AFTER counter start again at one on every row?
Answer
The FROM clause is applied each time the AFTER clause is reached
FROM belongs to the inner clause, so each pass of the outer loop sets the inner counter again.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev