PERFORM TIMES and UNTIL
A loop in COBOL is a PERFORM with a clause. The clause either counts the iterations or names the condition that ends them, and the second form has a choice the other languages do not offer: the test can run before the body or after it, and that choice decides whether the loop can run with a body that never should have run.
PERFORM ... TIMES
The count goes after PERFORM and before the statements, and END-PERFORM closes the group:
IDENTIFICATION DIVISION.
PROGRAM-ID. TIMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 I-F PIC 9 VALUE 0.
01 TOTAL-F PIC 9(4) VALUE 0.
PROCEDURE DIVISION.
PERFORM 3 TIMES
ADD 1 TO I-F
ADD I-F TO TOTAL-F
DISPLAY "pass " I-F " total " TOTAL-F
END-PERFORM.
DISPLAY "done, I is " I-F.
STOP RUN.
cobc -x -free -Wall -o times times.cob
./times
pass 1 total 0001
pass 2 total 0003
pass 3 total 0006
done, I is 3
The count is a literal here and may be a numeric field, in which case it is read once at the start of the loop and a change to it inside the body does not extend the loop. The counter is the program's own field: PERFORM ... TIMES does not provide an index.
A paragraph performed a number of times
The same clause works with a paragraph name, and the count goes between the name and the clause:
IDENTIFICATION DIVISION.
PROGRAM-ID. TIMES2.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 K-F PIC 9 VALUE 0.
PROCEDURE DIVISION.
PERFORM TICK 2 TIMES.
DISPLAY "paragraph ran, K is " K-F.
STOP RUN.
TICK.
ADD 1 TO K-F.
DISPLAY "tick " K-F.
cobc -x -free -Wall -o times2 times2.cob
./times2
tick 1
tick 2
paragraph ran, K is 2
A paragraph performed several times is the shape of most batch code in this language: a main paragraph sets up, a worker is performed once per record, and a closing paragraph prints the totals. The counter is again a field of the program, and the paragraph may change it.
UNTIL and TEST BEFORE
UNTIL names the condition that stops the loop. The default is TEST BEFORE: the condition is checked before each iteration, so a condition that is already true runs the body zero times:
IDENTIFICATION DIVISION.
PROGRAM-ID. BOUNDARY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-F PIC 9 VALUE 5.
PROCEDURE DIVISION.
PERFORM SHOW UNTIL N-F > 4.
DISPLAY "TEST BEFORE finished, N is " N-F.
PERFORM WITH TEST AFTER UNTIL N-F > 4
DISPLAY "TEST AFTER body, N is " N-F
ADD 1 TO N-F
END-PERFORM.
DISPLAY "TEST AFTER finished, N is " N-F.
STOP RUN.
SHOW.
DISPLAY "TEST BEFORE body, N is " N-F.
ADD 1 TO N-F.
cobc -x -free -Wall -o boundary boundary.cob
./boundary
TEST BEFORE finished, N is 5
TEST AFTER body, N is 5
TEST AFTER finished, N is 6
The field is already 5 when the first loop starts, so N-F > 4 is true and the body never runs. That is the safe form for a loop that reads records: a file that is empty on the first run does nothing instead of reading past its end.
TEST AFTER and the extra iteration
WITH TEST AFTER moves the test to the bottom, so the body always runs at least once. In the run above it printed N is 5 once, added one, and stopped at 6.
That is exactly what a menu loop wants: show the menu, read the choice, repeat until the user asks to leave. It is exactly what a record loop must not do, because the body runs before the end-of-file test, and the record it processes on that last pass is the one that does not exist. The symptom is a report with one blank line at the end, or a total that includes a zero for a record that was never read.
The clause is written on the PERFORM and applies to the whole loop. The next lesson adds a counter to the condition, so that a table can be walked with an index instead of a field the body has to maintain.
Exercises
- Change the count in the first program to a field with the value 5 and run it. What is the total after five passes?
- Set N-F to 1 in the boundary program and run it again. How many lines does each loop print?
- Write a loop that counts down from 10 to 1 with UNTIL and prints only the even numbers. Which test do you need?
- Change the boundary program to UNTIL N-F = 5 with TEST AFTER and a starting value of 5. What happens to the loop, and why is = a worse condition than > here?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
When is the condition of a PERFORM UNTIL tested by default?
Answer
Before each iteration, so the body can run zero times
TEST BEFORE is the default, and the boundary program ran its body zero times with a condition already true.
-
What does WITH TEST AFTER change?
Answer
The body runs once before the condition is tested
The test moved to the bottom, so the body printed N is 5 before the loop stopped at 6.
-
Which loop is the dangerous one for reading a file?
Answer
TEST AFTER, because the body runs before the end-of-file test
With the test at the bottom the body processes a record before it knows there was one to read.
-
Where does the count of PERFORM TICK 2 TIMES go?
Answer
Between the paragraph name and the TIMES clause
The paragraph name comes first, then the count and TIMES, which is how the second program on this page is written.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 I-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM 2 TIMES ADD 1 TO I-F DISPLAY "pass " I-F END-PERFORM. STOP RUN.Answer
pass 1 / pass 2
The group runs twice and the counter is incremented before each DISPLAY.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 3. PROCEDURE DIVISION. PERFORM UNTIL N-F IS GREATER THAN 2 DISPLAY "body" ADD 1 TO N-F END-PERFORM. DISPLAY "N is " N-F. STOP RUN.Answer
N is 3
The condition is already true, so TEST BEFORE skips the body and only the closing DISPLAY runs.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 3. PROCEDURE DIVISION. PERFORM WITH TEST AFTER UNTIL N-F IS GREATER THAN 2 DISPLAY "body" END-PERFORM. DISPLAY "N is " N-F. STOP RUN.Answer
body / N is 3
The body runs once because the test is at the bottom, and nothing in the body changes N, so the loop stops after one pass.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 1. PROCEDURE DIVISION. PERFORM UNTIL N-F IS GREATER THAN 3 ADD 1 TO N-F END-PERFORM. DISPLAY N-F. STOP RUN.Answer
4
The body runs for 1, 2 and 3, and the loop stops when N becomes 4.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q9. DATA DIVISION. WORKING-STORAGE SECTION. 01 K-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM TICK 3 TIMES. DISPLAY K-F. STOP RUN. TICK. ADD 2 TO K-F.Answer
6
The paragraph runs three times and adds two each time, so the field ends at 6.
-
A report ends with one blank line and a zero total for a record that was never read. Which loop is the likely cause?
Answer
A PERFORM UNTIL with TEST AFTER around the read
The test at the bottom lets the body run once more after the last record, which the body processes as an empty record.
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