Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 38 ยท Procedures and Control Flow

PERFORM, Paragraphs, Sections and GO TO

Learn how COBOL organises logic: paragraphs and sections, PERFORM and PERFORM THRU, fall-through, EXIT, GO TO, STOP RUN versus GOBACK, and structured top-down design.

COBOL has no functions in the usual sense inside a program. Instead you break logic into named paragraphs (and optionally sections) and run them with PERFORM. A paragraph is a label followed by statements; PERFORM runs it and then comes back to the statement after the PERFORM. It is simple, and it is how nearly every COBOL program is structured.

Paragraphs and sections

COBOL
       PROCEDURE DIVISION.
       MAIN-LOGIC.
           PERFORM INITIALISE
           PERFORM PROCESS-ALL
           PERFORM FINISH
           STOP RUN.

       INITIALISE.
           MOVE ZERO TO TOTAL
           DISPLAY "Starting".

       PROCESS-ALL.
           ADD 10 TO TOTAL
           ADD 20 TO TOTAL.

       FINISH.
           DISPLAY "Total: " TOTAL.
  • A paragraph starts with a name in area A followed by a period. It ends where the next paragraph or section name begins.
  • A section is NAME SECTION. and contains paragraphs. Sections let you PERFORM a group of paragraphs at once.
  • The last statement of the paragraph carries the paragraph's period. Inside, statements have no periods.
  • Execution starts at the first paragraph of the procedure division and runs downward.

PERFORM

Form Runs
PERFORM PARA-A the one paragraph
PERFORM PARA-A THRU PARA-C every paragraph from A to C in source order
PERFORM SECTION-X all paragraphs in the section
PERFORM PARA-A 3 TIMES the paragraph three times (see PERFORM loops)
PERFORM with inline statements a block without a paragraph (see loops)

After the paragraph's last statement, control returns to the next statement after PERFORM. Paragraphs can PERFORM other paragraphs, to any depth, but not recursively unless the program is declared RECURSIVE.

flowchart TD
    M["MAIN-LOGIC"] -->|PERFORM| I["INITIALISE"]
    I -->|returns| M
    M -->|PERFORM| P["PROCESS-ALL"]
    P -->|returns| M
    M -->|PERFORM| F["FINISH"]
    F -->|returns| M
    M --> S["STOP RUN"]

MAIN-LOGIC is the driver: each PERFORM runs a paragraph and comes back.

The fall-through trap

If you let control reach the end of a paragraph that was not called with PERFORM, execution simply continues into the next paragraph. This is called fall-through, and it is the first big difference from a function-based language:

COBOL
       MAIN-LOGIC.
           PERFORM STEP-ONE
           DISPLAY "main done".

       STEP-ONE.
           DISPLAY "one".

       STEP-TWO.
           DISPLAY "two".

Because MAIN-LOGIC has no STOP RUN, after printing one and main done the program flows on into STEP-ONE and then STEP-TWO and prints one and two again, which is more than you expect. Always end the main paragraph with STOP RUN (or GOBACK) and put the subordinate paragraphs below it, so nothing flows into them by accident.

Python Fall-through (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

PERFORM THRU and EXIT

PERFORM A THRU A-EXIT is a classic pattern. The exit paragraph holds only EXIT. (a do-nothing statement that gives GO TO a target):

COBOL
           PERFORM VALIDATE-INPUT THRU VALIDATE-INPUT-EXIT.

       VALIDATE-INPUT.
           IF AMOUNT NOT NUMERIC
               DISPLAY "Bad amount"
               GO TO VALIDATE-INPUT-EXIT
           END-IF
           IF AMOUNT = ZERO
               DISPLAY "Amount is zero"
           END-IF.
       VALIDATE-INPUT-EXIT.
           EXIT.

GO TO jumps without coming back. Using it to skip to an exit paragraph inside the range of a PERFORM THRU is the one widely accepted use; everywhere else it makes programs hard to follow ("spaghetti code"). Modern code prefers EVALUATE, nested IF/END-IF, and EXIT PARAGRAPH or EXIT SECTION (COBOL 2002, supported by GnuCOBOL), which leave the current paragraph or section cleanly.

Avoid ALTER, which changed a GO TO's target at run time. It is obsolete and made programs almost impossible to read.

Ending the program

Statement Use
STOP RUN Ends the whole run unit (the program and everything it called).
GOBACK Returns to the caller: ends a called subprogram, and ends the run when used in the main program.
EXIT PROGRAM Returns from a called subprogram (ignored in a main program).

Use GOBACK in programs that might be called as well as run alone.

Structured design: top-down

Treat the first paragraph as a table of contents. Each PERFORM line says what happens, and the detail sits in the paragraph below. A common numbering convention gives paragraphs a prefix so you can find them in a long listing: 0000-MAIN, 1000-INIT, 2000-PROCESS, 3000-FINISH; sub-steps are 2100-READ-NEXT, 2200-WRITE-LINE.

COBOL
       0000-MAIN.
           PERFORM 1000-INIT
           PERFORM 2000-PROCESS
           PERFORM 3000-FINISH
           STOP RUN.

Stand-in: running paragraphs by name

Python A paragraph dispatcher (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Forgetting STOP RUN so control falls into the next paragraph.
  • A period in the middle of a paragraph, which ends the sentence and can end nested IFs early.
  • PERFORM A THRU B where B comes before A in the source (the range then never ends properly).
  • Using GO TO between unrelated paragraphs.
  • Naming two paragraphs the same.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens after a PERFORMed paragraph finishes its last statement?

    PERFORM is like a call: run the paragraph, then come back.
  2. 2. Why should MAIN-LOGIC end with STOP RUN?

    Without a stop statement, execution continues downward through the following paragraphs.
  3. 3. What does PERFORM A THRU C do?

    All paragraphs from A through C run in the order they appear in the source.
  4. 4. Which statement should a subprogram use to return to its caller?

    GOBACK returns to the caller. STOP RUN would end the entire run unit, including the caller.

Summary

  • Logic is organised in paragraphs (and sections); execution starts at the first paragraph and runs downward.
  • PERFORM runs a paragraph (or a THRU range) and returns.
  • Falling off the end of a paragraph continues into the next one: end the main paragraph with STOP RUN or GOBACK.
  • GO TO is acceptable only to a THRU exit paragraph; prefer EXIT PARAGRAPH and structured statements.
  • Number paragraphs (1000-INIT) and keep the main paragraph as a table of contents.

Next: PERFORM Loops