COBOL Tutorial

Progress0 of 0 lessons

COBOL program flow

Program flow is the order in which statements run. In COBOL, execution starts at the first statement in the PROCEDURE DIVISION and goes top to bottom unless a PERFORM, IF, or similar statement sends control elsewhere. When you PERFORM a paragraph, that paragraph runs from start to finish, then control returns to the statement after the PERFORM. This page covers the basics; for IF, EVALUATE, and PERFORM UNTIL see Program flow control.

How flow works

Key points of program flow
AspectMeaning
EntryFirst statement in PROCEDURE DIVISION (after declaratives)
SequenceStatements run in order unless PERFORM, IF, etc. change flow
PERFORMCalls a paragraph; when it ends, control returns after the PERFORM
ExitSTOP RUN or GOBACK ends the program

Entry point

The run time starts at the first executable statement in the PROCEDURE DIVISION. Declaratives (USE procedures, etc.) are skipped for normal entry. So the first paragraph you write is usually the “main” logic; it often PERFORMs other paragraphs and then runs STOP RUN or GOBACK.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
PROCEDURE DIVISION. MAIN. *> Execution starts here PERFORM INIT-PROGRAM PERFORM PROCESS-DATA PERFORM CLEANUP STOP RUN. INIT-PROGRAM. *> Runs when PERFORM INIT-PROGRAM executes MOVE ZEROS TO WS-COUNTER *> Control returns to PERFORM PROCESS-DATA . PROCESS-DATA. *> Runs next; when done, control returns to PERFORM CLEANUP PERFORM READ-AND-HANDLE . CLEANUP. CLOSE INFILE OUTFILE .

MAIN runs first. PERFORM INIT-PROGRAM runs INIT-PROGRAM; when it hits the period (end of paragraph), control returns to the next statement, PERFORM PROCESS-DATA. So the flow is: MAIN → INIT-PROGRAM (then back) → PROCESS-DATA (then back) → CLEANUP → STOP RUN.

Paragraphs and return

A paragraph is a name followed by a period, then one or more sentences. When you PERFORM that name, the run time jumps to the paragraph, runs every statement until the next paragraph name or section name (or end of division), then returns to the statement after the PERFORM. Fall-through (running into the next paragraph without PERFORM) is possible but usually avoided; use one PERFORM per paragraph for clear flow.

Sections

A section is a paragraph whose name ends with SECTION and a period (e.g. READ-LOOP SECTION.). All following paragraphs belong to that section until the next section. You can PERFORM a section (runs from the first paragraph in the section to the end) or PERFORM a specific paragraph. Sections group related paragraphs; flow and return work the same as for paragraphs.

Step-by-step: following the flow

  • Find the first statement in the PROCEDURE DIVISION; that is where execution starts.
  • Follow statements in order. When you see PERFORM name, jump to that paragraph, run it to its end, then come back to the next statement after the PERFORM.
  • When you see STOP RUN or GOBACK, the program ends (or returns to the caller).

Explain like I'm five

The program is like a list of instructions. You start at the top and do each one. When the list says “go do the washing-up,” you do the washing-up steps, then come back and do the next instruction on the list. “Washing-up” is a paragraph; “go do it” is PERFORM. When the list says “stop,” you stop.

Test Your Knowledge

1. When you PERFORM a paragraph, where does control go when the paragraph finishes?

  • To the start of the program
  • Back to the statement after the PERFORM
  • To the next paragraph
  • Nowhere; the program stops

2. Where does execution begin in a COBOL program?

  • In the DATA DIVISION
  • At the first statement in the PROCEDURE DIVISION
  • In the last paragraph
  • Where PERFORM points

Related concepts

Related Pages