COBOL GnuCOBOL 3.2.0 ยท
โ verified by execution on 2026-08-16
In the previous lesson, you learned that PERFORM can act as a subroutine caller by temporarily transferring control to a named paragraph. But COBOL provides a variety of powerful variations to this statement, including executing a range of paragraphs, repeating logic a fixed number of times, and running loops entirely inline.
PERFORM THRU
The PERFORM THRU statement transfers control explicitly to a starting procedure and implicitly returns control after the execution of an ending procedure is completed.
A common misconception is that PERFORM PARA-1 THRU PARA-3 only executes PARA-1 and PARA-3. This is false.
PERFORM THRU executes all code sequentially starting from the first paragraph and continuing downwards until it reaches the end of the second paragraph, including any other paragraphs physically located between them.
Warning: Physical Order Matters
If you move PARA-2 below PARA-3 in the source code, it will no longer execute during the PERFORM THRU. COBOL relies on the physical layout of the file!
The EXIT Paragraph
Because PERFORM THRU executes downwards, you often need a way to say โskip the rest of this block and return nowโ. You do this using a GO TO statement that jumps to the very last paragraph in the THRU range.
The EXIT statement is a no-op designed specifically for this purpose. It provides a common endpoint for a series of procedures.
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. EX-EXIT. DATA DIVISION. WORKING-STORAGE SECTION. 01 COUNTER PIC 9. PROCEDURE DIVISION. PERFORM LOOP-START THRU LOOP-END VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > 2. STOP RUN. LOOP-START. DISPLAY 'LOOP ' COUNTER. IF COUNTER = 2 GO TO LOOP-END. DISPLAY 'AFTER-IF ' COUNTER. LOOP-END. EXIT.
Output
LOOP 1
AFTER-IF 1
LOOP 2
Your output
In iteration 2, the GO TO LOOP-END causes the program to jump to the EXIT statement, which ends the PERFORM THRU range and triggers the return.
Inline PERFORM and PERFORM TIMES
Instead of out-of-line paragraphs, you can put code directly inside an inline PERFORM bounded by END-PERFORM. This is highly recommended for modern COBOL development as it prevents fall-through bugs and keeps logic tightly scoped.
The PERFORM n TIMES variant allows you to run an inline (or out-of-line) block a fixed number of times.
In modern COBOL (like GnuCOBOL and recent IBM Enterprise COBOL), you can use EXIT PERFORM CYCLE within an inline or out-of-line loop to bypass the remaining statements in the current iteration and immediately begin the next one.
Predict the outputcobol
What will this print when I=2?
IDENTIFICATION DIVISION. PROGRAM-ID. EX-CYCLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 I PIC 9. PROCEDURE DIVISION. PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3 IF I = 2 EXIT PERFORM CYCLE END-IF DISPLAY 'I is: ' I END-PERFORM. STOP RUN.
Output
I is: 1
I is: 3
You predicted
When I equals 2, the EXIT PERFORM CYCLE statement executes, jumping straight to the loop condition and beginning the third iteration, bypassing the DISPLAY. This is identical to the continue keyword in languages like C or Java!
Check yourself
If you execute PERFORM PARA-1 THRU PARA-3, and PARA-2 is physically between them in the source code, what happens to PARA-2?
Reveal answer
It is executed normally after PARA-1 and before PARA-3. โ PERFORM THRU executes all code sequentially from the start paragraph down to the end paragraph, including any paragraphs physically located in between.
How do you define the end of an inline PERFORM block?
Reveal answer
Using the END-PERFORM scope terminator. โ Inline PERFORM loops enclose their statements directly and must be terminated with the END-PERFORM keyword.
What does the EXIT statement actually do?
Reveal answer
It is a no-op that provides a nameable paragraph to use as an endpoint (THRU) or jump target. โ EXIT does nothing on its own. It provides a named endpoint for a PERFORM THRU or a GO TO so you can reach the end of a block without executing any other statements.
Challenges
Challenge 1 +25 XP
Use PERFORM THRU to execute the code from PROCESS-START up to and including PROCESS-END. Do not modify the paragraphs themselves.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.
Use an inline PERFORM block bounded by END-PERFORM to DISPLAY the word 'HELLO' exactly 3 times.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 โ expects "HELLO\nHELLO\nHELLO\n"
Need a hint? (โ25% XP)
Use PERFORM n TIMES followed by the statements, then END-PERFORM.