PERFORM THRU
PERFORM has a second form that names two paragraphs, and it is the form that most COBOL written before 1985 uses. It reads PERFORM A THRU C and runs everything from A to C, including the paragraphs in between that the statement does not name.
PERFORM A THRU C
Three paragraphs, one statement, and control comes back after the last of them:
IDENTIFICATION DIVISION.
PROGRAM-ID. THRU.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-F PIC 9 VALUE 0.
PROCEDURE DIVISION.
DISPLAY "main: before".
PERFORM STEP-ONE THRU STEP-THREE.
DISPLAY "main: after, N is " N-F.
STOP RUN.
STEP-ONE.
ADD 1 TO N-F.
DISPLAY "step one, N " N-F.
STEP-TWO.
ADD 1 TO N-F.
DISPLAY "step two, N " N-F.
STEP-THREE.
ADD 1 TO N-F.
DISPLAY "step three, N " N-F.
cobc -x -free -Wall -o thru thru.cob
./thru
main: before
step one, N 1
step two, N 2
step three, N 3
main: after, N is 3
The three paragraphs ran in the order they appear in the source, and the statement after the PERFORM ran when the last one ended. A paragraph in the range does not need a PERFORM A or a PERFORM B of its own: being inside the range is enough.
The paragraphs in between
That is the trap. A paragraph that happens to sit between the two names is part of the range, whether the writer of the PERFORM wanted it or not, and the compiler says nothing:
IDENTIFICATION DIVISION.
PROGRAM-ID. TRAP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-F PIC 9 VALUE 0.
PROCEDURE DIVISION.
PERFORM FIRST-PARA THRU LAST-PARA.
DISPLAY "main: done".
STOP RUN.
FIRST-PARA.
ADD 1 TO N-F.
DISPLAY "first".
UNWANTED-PARA.
ADD 1 TO N-F.
DISPLAY "unwanted".
LAST-PARA.
ADD 1 TO N-F.
DISPLAY "last".
AFTER-LAST.
DISPLAY "after last, never performed".
cobc -x -free -Wall -o trap trap.cob
./trap
first
unwanted
last
main: done
UNWANTED-PARA ran and AFTER-LAST did not: the range ends with the last paragraph named, not with the next one in the source. A program that inserts a paragraph inside an existing range changes what a distant PERFORM does, and no compiler in this image reports it.
The EXIT paragraph
The older style answers the problem with an empty paragraph named for the range, so that a new paragraph can be added after it without joining the range:
IDENTIFICATION DIVISION.
PROGRAM-ID. EXITIDIOM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-F PIC 9 VALUE 0.
PROCEDURE DIVISION.
PERFORM WORK THRU WORK-EXIT.
DISPLAY "main: N is " N-F.
STOP RUN.
WORK.
ADD 1 TO N-F.
DISPLAY "work, N " N-F.
WORK-EXIT.
EXIT.
cobc -x -free -Wall -o exitidiom exitidiom.cob
./exitidiom
work, N 1
main: N is 1
EXIT is a statement that does nothing, and a paragraph that holds only EXIT is the marker for the end of a range. Anything added after WORK-EXIT is outside the range, which is the property the trap program does not have.
EXIT on its own is not a return: the paragraph ran to its end and the PERFORM returned, as it did in the first program. Old code sometimes writes EXIT PROGRAM here, which returns from a subprogram and has nothing to do with a range; the lesson on CALL and USING uses it in the place it belongs.
Where control comes back
The return address is the statement after the PERFORM, and a range does not change that. What it changes is the amount of code between the two names that runs before the return, and that amount is decided by the layout of the source, not by the statement.
A reader who meets PERFORM A THRU B in old code should read the source from A to B before believing the statement, and a writer who adds a paragraph to such a program should look for the ranges that cross the place. The next lesson is the other construct of that era, GO TO, and why the course writes structured code without it.
Exercises
- Change the first program to PERFORM STEP-ONE THRU STEP-TWO. Which lines print, and what is N-F at the end?
- Move UNWANTED-PARA below LAST-PARA and run the trap program again. What changes?
- Add a second paragraph after WORK-EXIT and perform it from the main flow. Is it inside the first range?
- Write a range that runs three paragraphs and ends with an EXIT paragraph, then add a fourth paragraph below the marker and show that it does not run.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does PERFORM A THRU C run?
Answer
Every paragraph from A to C in source order, including the ones not named
The trap program printed the unwanted paragraph that sat between the two names.
-
Which paragraphs belong to the range of PERFORM A THRU C?
Answer
Those between A and C in the source, up to the end of C
The range is positional: a paragraph between the two names is inside it whatever its name is.
-
What is an EXIT paragraph for?
Answer
It marks the end of a range so later paragraphs stay outside it
The paragraph holds the no-op EXIT statement and gives PERFORM THRU a stable last name.
-
Does EXIT in a paragraph return to the caller?
Answer
No: the paragraph ends and the PERFORM returns, as with any paragraph
EXIT does nothing at all; the return happens because the performed paragraph reached its end.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM A-PARA THRU C-PARA. DISPLAY "end " N-F. STOP RUN. A-PARA. ADD 1 TO N-F. B-PARA. ADD 1 TO N-F. C-PARA. ADD 1 TO N-F.Answer
end 3
The range runs all three paragraphs, and each one adds one.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM A-PARA. PERFORM C-PARA. DISPLAY N-F. STOP RUN. A-PARA. ADD 1 TO N-F. B-PARA. ADD 10 TO N-F. C-PARA. ADD 100 TO N-F.Answer
101
Two single-paragraph PERFORMs skip B entirely, so the field only receives one and one hundred.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. PERFORM ONE THRU TWO. DISPLAY "done " N-F. STOP RUN. ONE. ADD 1 TO N-F. TWO. ADD 1 TO N-F. THREE. DISPLAY "three".Answer
done 2
The range covers ONE and TWO; THREE is outside it and STOP RUN keeps the flow out of the paragraphs.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 5. PROCEDURE DIVISION. PERFORM SHOW THRU SHOW-EXIT. DISPLAY "N is " N-F. STOP RUN. SHOW. MOVE 1 TO N-F. SHOW-EXIT. EXIT. AFTER-EXIT. MOVE 9 TO N-F.Answer
N is 1
The range ends at the EXIT paragraph, so the paragraph after the marker never runs.
-
A paragraph is inserted between the two names of an existing PERFORM THRU in a program someone else wrote. What is the effect?
Answer
The new paragraph joins the range and runs whenever the range runs
The range is positional, so an inserted paragraph is performed by a statement that never mentions it.
-
Which form should new code prefer?
Answer
A PERFORM of one paragraph, or an inline PERFORM, with ranges avoided
A single name cannot pick up a neighbour by accident, and the inline form has no range at all.
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