COBOLbeginner
PERFORM Varieties: THRU, VARYING, UNTIL, and Inline
Master the different forms of PERFORM, COBOL's core control-flow verb for looping and paragraph invocation.
PERFORM does the work that function calls and loops do in other languages. It has several forms and they behave differently.
PERFORM THRU is like telling someone to read pages 10 through 15 of a binder — it works fine until someone inserts a new page 12 and quietly changes what 'THRU 15' actually covers.
Key Concepts
1
The plain call.
PERFORM CALCULATE-TOTALS2
Runs that paragraph once and returns. A subroutine call with no arguments and no return value.
3
THRU runs a range of paragraphs, and it is the one to be careful with:
THRU
4
PERFORM PARA-1 THRU PARA-35
That executes everything from PARA-1 to PARA-3 — meaning whatever paragraphs physically sit between them in
the source.
6
Insert a new paragraph in the middle for some unrelated reason and it is now silently part of the range.
Nothing flags it. This is a genuine source of maintenance bugs, and it is why new code avoids THRU.
THRU
7
UNTIL is a pre-test loop.
PERFORM PROCESS-RECORD UNTIL WS-EOF = 'Y'UNTIL
8
The condition is checked before each pass, so if it is already true the paragraph never runs at all.
That is usually what you want when the loop reads a file that might be empty.
9
For a post-test loop, say so explicitly:
10
PERFORM PROCESS-RECORD WITH TEST AFTER UNTIL WS-EOF = 'Y'11
VARYING is the counted loop.
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 100
...
END-PERFORMVARYING
12
And it nests with AFTER, for tables with more than one dimension:
AFTER
13
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10
AFTER WS-J FROM 1 BY 1 UNTIL WS-J > 514
One statement, fifty iterations. The inner counter resets each time the outer advances, exactly as nested
loops would.
15
Inline PERFORM puts the body where it is used.
PERFORM UNTIL WS-EOF = 'Y'
READ CUST-FILE
AT END MOVE 'Y' TO WS-EOF
END-READ
END-PERFORM16
No separate paragraph, so nothing to scroll to and no THRU range to break. This reads much like a loop in
any other language, and it is what new COBOL is written with.
THRU
17
The judgement being tested. A separate paragraph is right when the logic is reused or long enough to
deserve a name. Inline is right when it is used once. THRU is right almost never in new code.
THRU
18
What the interviewer is probing.1. "What is the risk of PERFORM THRU?" Probing: the maintenance trap. Stalls: "None." *Moves
up:* someone inserting a paragraph between the two named ones silently changes what runs — which is
why many shops ban it.
19
2. "What is the difference between WITH TEST BEFORE and AFTER?" Probing: the semantics.
Stalls: "Nothing." Moves up: TEST BEFORE may execute zero times; TEST AFTER always executes at
least once — the COBOL equivalent of while versus do-while.
20
3. "What does PERFORM VARYING AFTER give you?" Probing: the capability. Stalls: "A loop."
Moves up: nested loops in one statement, which is how multi-dimensional tables are walked.
21
4. "When is an inline PERFORM better?" Probing: judgement. Stalls: "Never." Moves up: for
short, local loops where a separate paragraph adds navigation cost without adding clarity.