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.
| Aspect | Meaning |
|---|---|
| Entry | First statement in PROCEDURE DIVISION (after declaratives) |
| Sequence | Statements run in order unless PERFORM, IF, etc. change flow |
| PERFORM | Calls a paragraph; when it ends, control returns after the PERFORM |
| Exit | STOP RUN or GOBACK ends the program |
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.
12345678910111213141516171819202122PROCEDURE 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.
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.
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.
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.
1. When you PERFORM a paragraph, where does control go when the paragraph finishes?
2. Where does execution begin in a COBOL program?