PERFORM THRU, Inline and Scope

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.

cobol โœ“ verified output
       IDENTIFICATION DIVISION.
       PROGRAM-ID. EX-THRU.
       PROCEDURE DIVISION.
           DISPLAY '1. MAIN'.
           PERFORM BLOCK-A THRU BLOCK-B.
           DISPLAY '5. MAIN'.
           STOP RUN.
       BLOCK-A.
           DISPLAY '2. BLOCK-A'.
       BLOCK-B.
           DISPLAY '3. BLOCK-B'.
       BLOCK-C.
           DISPLAY '4. BLOCK-C'.
Output
1. MAIN
2. BLOCK-A
3. BLOCK-B
5. MAIN

The Fall-Through Pitfall

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.

PERFORM PARA-1 THRU PARA-3 Next Statement PARA-1 PARA-2 (Included!) PARA-3

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

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.

cobol โœ“ verified output
       IDENTIFICATION DIVISION.
       PROGRAM-ID. EX-INLINE.
       PROCEDURE DIVISION.
           PERFORM 2 TIMES
               DISPLAY 'A'
               DISPLAY 'B'
           END-PERFORM.
           STOP RUN.
Output
A
B
A
B

Skipping Iterations: EXIT PERFORM CYCLE

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 output cobol

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

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.

  • Test 1 โ€” expects "A\nB\nX\nC\nD\n"
Need a hint? (โˆ’25% XP)

Use PERFORM procedure-1 THRU procedure-2.

Show solution (0 XP)
IDENTIFICATION DIVISION.
PROGRAM-ID. CHAL-THRU.
PROCEDURE DIVISION.
    DISPLAY 'A'.
    PERFORM PROCESS-START THRU PROCESS-END.
    DISPLAY 'D'.
    STOP RUN.
PROCESS-START.
    DISPLAY 'B'.
PROCESS-MIDDLE.
    DISPLAY 'X'.
PROCESS-END.
    DISPLAY 'C'.

Challenge 2 +25 XP

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.

Show solution (0 XP)
IDENTIFICATION DIVISION.
PROGRAM-ID. CHAL-TIMES.
PROCEDURE DIVISION.
    PERFORM 3 TIMES
        DISPLAY 'HELLO'
    END-PERFORM.
    STOP RUN.