PERFORM THRU pitfalls
A range is positional, so two ranges can cover the same paragraph, and a perform inside a range can repeat a paragraph that the outer range will reach again. Both are legal, neither is reported, and both show up as a count that is larger than the writer expected.
Two ranges that share paragraphs
Four paragraphs, two statements, and the middle two are performed twice:
IDENTIFICATION DIVISION.
PROGRAM-ID. OVERLAP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-F PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
DISPLAY "first range:".
PERFORM A-P THRU C-P.
DISPLAY "second range:".
PERFORM B-P THRU D-P.
DISPLAY "total " N-F.
STOP RUN.
A-P.
ADD 1 TO N-F.
DISPLAY "A".
B-P.
ADD 10 TO N-F.
DISPLAY "B".
C-P.
ADD 100 TO N-F.
DISPLAY "C".
D-P.
ADD 1000 TO N-F.
DISPLAY "D".
cobc -x -free -Wall -o overlap overlap.cob
./overlap
first range:
A
B
C
second range:
B
C
D
total 01221
B and C ran twice and printed twice, which the writer of the second statement may not have wanted: the ranges were meant to be two units of work and they share a middle. The counter is how the overlap becomes visible: 1 + 10 + 100 in the first range, 10 + 100 + 1000 in the second, 1221 in all.
A range inside a range
The overlap becomes a repeat when one of the paragraphs performs a range that the outer range already covers. Here B-P performs C-P THRU D-P while the outer statement is performing A-P THRU C-P:
IDENTIFICATION DIVISION.
PROGRAM-ID. NESTED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-F PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
PERFORM A-P THRU C-P.
DISPLAY "total " N-F.
STOP RUN.
A-P.
ADD 1 TO N-F.
DISPLAY "A".
B-P.
ADD 10 TO N-F.
DISPLAY "B".
PERFORM C-P THRU D-P.
C-P.
ADD 100 TO N-F.
DISPLAY "C".
D-P.
ADD 1000 TO N-F.
DISPLAY "D".
cobc -x -free -Wall -o nested nested.cob
./nested
A
B
C
D
C
total 01211
C printed twice: once from the inner range and once because the outer range had not finished with it. The inner call returned to B-P, B-P ended, and the outer range continued with the next paragraph, which is C-P. The counter is 1 + 10 + 100 + 1000 + 100.
The rule for ranges
The rule that keeps this readable is the one old code follows with its exit paragraphs: a range has a name of its own and every paragraph of it belongs to that range only, so no second statement covers the same lines. The EXIT paragraph of PERFORM THRU is the marker that keeps the next paragraph out; it does not keep a second range from covering the same block.
A new program uses PERFORM with one paragraph or an inline group, and then no range exists to overlap. Recursion is the next subject and the last of this part: a paragraph that performs itself, which the language allows only when the program says so.
Exercises
- Change the second range of the first program to PERFORM C-P THRU D-P. Which paragraphs run once now?
- Print the counter after the first range only. Which value is it, and which additions made it?
- Make the inner perform of the second program PERFORM C-P alone. What prints now, and why?
- Rewrite both programs with one PERFORM per paragraph and show that the totals do not change.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What makes two PERFORM THRU ranges overlap?
Answer
A paragraph that lies between both pairs of names
The ranges are positional, so a shared paragraph is performed by both statements.
-
What happens when a performed range contains a PERFORM of a range that overlaps it?
Answer
The shared paragraphs run more than once and nothing reports it
C printed twice in the second program, once from each range.
-
Which statement makes an overlap visible?
Answer
A counter that every paragraph adds to, printed at the end
The count on this page is larger than the writer expected, and that is the only symptom.
-
How does an EXIT paragraph help a range?
Answer
It marks where the range ends, so later paragraphs stay outside it
The marker fixes the last name of the range; it does not prevent a second range from covering the same block.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM A-P THRU B-P. PERFORM B-P THRU C-P. DISPLAY N-F. STOP RUN. A-P. ADD 1 TO N-F. B-P. ADD 10 TO N-F. C-P. ADD 100 TO N-F.Answer
121
The first range adds one and ten, the second adds ten and a hundred.
-
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-P THRU C-P. DISPLAY N-F. STOP RUN. A-P. ADD 1 TO N-F. B-P. ADD 10 TO N-F. PERFORM C-P. C-P. ADD 100 TO N-F.Answer
211
The inner perform adds a hundred, and the outer range reaches C-P once more before it returns, so the hundred is added twice.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM A-P. PERFORM B-P. DISPLAY N-F. STOP RUN. A-P. ADD 1 TO N-F. B-P. ADD 10 TO N-F. C-P. ADD 100 TO N-F.Answer
011
Two single-paragraph calls add one and ten, and C-P is outside both.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(3) VALUE 0. PROCEDURE DIVISION. PERFORM A-P THRU C-P. PERFORM A-P THRU C-P. DISPLAY N-F. STOP RUN. A-P. ADD 1 TO N-F. B-P. ADD 10 TO N-F. C-P. ADD 100 TO N-F.Answer
222
Each call runs the three paragraphs, and the whole range is performed twice.
-
A program prints its total line twice. The line is in a paragraph that two ranges cover. What is the fix?
Answer
Give the paragraph its own range, or perform it by name once
A paragraph covered by two ranges runs twice; disjoint ranges or single-paragraph calls remove the overlap.
-
Why does a new program avoid PERFORM THRU?
Answer
A single paragraph or an inline group has no positional range to overlap
Without a range there is nothing for a second range to cover, and a reader sees the whole block in one place.
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