COBOL Program Flow Control

Program flow control in COBOL is how execution moves through the PROCEDURE DIVISION: in sequence, or by conditionals (IF, EVALUATE) that choose which path to run, or by PERFORM that calls paragraphs or sections and can repeat them (TIMES, UNTIL, VARYING). Understanding flow helps you read and write COBOL: where control goes next, when blocks run once vs in a loop, and how conditionals and PERFORM interact with paragraphs and sections.

Explain Like I'm Five: What Is Flow Control?

Imagine following a list of instructions. Usually you do step 1, then step 2, then step 3 (sequence). Sometimes the list says "if it is raining, do A, else do B"—that is a choice (IF). Sometimes it says "depending on the day, do one of these things"—that is EVALUATE. And sometimes it says "repeat steps 4–6 five times" or "repeat until you finish"—that is PERFORM with TIMES or UNTIL. Flow control is the set of rules that tell the computer what to do next: continue in order, jump to another block, or repeat.

Sequential Execution

By default, statements in the PROCEDURE DIVISION execute in order. After one statement completes, the next one runs. Paragraphs and sections are just named points in the code; if you do not use PERFORM or GO TO, execution falls through from one paragraph to the next. So the order of your paragraphs and the placement of IF, EVALUATE, and PERFORM determine the actual flow. Many programs have a main paragraph that PERFORMs other paragraphs in a logical order, so the flow is clear: MAIN drives the flow, and each performed paragraph does one task.

Main Flow-Control Constructs

Flow-control constructs
ConstructMeaning
SequenceStatements run in order. No branching or looping.
IF / ELSE / END-IFConditional: run one block or another (or neither) based on a condition.
EVALUATE / WHEN / END-EVALUATEMulti-way selection: first matching WHEN runs.
PERFORM paragraphCall a paragraph; return when it ends (one-time call).
PERFORM TIMES / UNTIL / VARYINGRepeat a block a fixed number of times or until a condition is true.

IF, ELSE, and END-IF

IF condition THEN statements [ELSE statements] END-IF selects one of two paths. When the condition is true, the THEN block runs; when false, the ELSE block runs (if present). After the chosen block, execution continues after END-IF. You can nest IF inside IF for multiple conditions; for many branches EVALUATE is often clearer. Relational operators (=, <, >, <=, >=, <>) and logical operators (AND, OR, NOT) build the condition.

cobol
1
2
3
4
5
6
7
8
9
IF WS-CODE = "A" PERFORM PROCESS-A ELSE IF WS-CODE = "B" PERFORM PROCESS-B ELSE PERFORM PROCESS-OTHER END-IF END-IF.

EVALUATE and WHEN

EVALUATE is multi-way selection. You give a subject (e.g. a variable or TRUE) and list WHEN clauses. The first matching WHEN runs; then control goes past END-EVALUATE. WHEN OTHER catches any case that did not match. Use EVALUATE when you have several possible values or conditions (e.g. menu options, status codes) instead of deep nested IFs. EVALUATE TRUE ALSO TRUE lets you test multiple conditions with ALSO (like AND).

cobol
1
2
3
4
5
6
7
8
EVALUATE WS-OPTION WHEN 1 PERFORM OPTION-1 WHEN 2 PERFORM OPTION-2 WHEN OTHER PERFORM INVALID-OPTION END-EVALUATE.

PERFORM: Calling and Repeating

PERFORM transfers control to a paragraph (or section) or to an inline block. When the paragraph or block ends, control returns to the statement after the PERFORM. So PERFORM paragraph-name runs the paragraph once (like a subroutine call). PERFORM paragraph-name TIMES n runs it n times. PERFORM paragraph-name UNTIL condition runs it repeatedly until the condition is true (TEST BEFORE checks first, so zero iterations are possible; TEST AFTER runs at least once). PERFORM ... VARYING ... adds a loop variable that changes each time. PERFORM THRU paragraph-name-2 runs from paragraph-name through paragraph-name-2 (all paragraphs in between in sequence).

cobol
1
2
3
4
5
6
PERFORM INIT-PARAGRAPH. PERFORM PROCESS-RECORD UNTIL WS-EOF = "Y". PERFORM PARA-1 THRU PARA-3. PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10 ADD 1 TO WS-COUNT END-PERFORM.

INIT-PARAGRAPH runs once. PROCESS-RECORD runs until WS-EOF is "Y". PARA-1 THRU PARA-3 runs from PARA-1 through PARA-3. The inline PERFORM with VARYING runs the ADD statement with I from 1 to 10.

Paragraphs and Sections

A paragraph is a name followed by a period and then one or more statements. A section is a name followed by the word SECTION and a period, then paragraphs or statements. They define named blocks. Execution does not automatically "call" them unless you use PERFORM (or GO TO). So the flow is: start at the first paragraph/section that runs (often the first in the division, or the one specified by the program structure), then sequence and PERFORM/IF/EVALUATE determine what runs next. When PERFORM paragraph-name executes, control goes to that paragraph; when it falls through to the next paragraph or hits EXIT PROGRAM / STOP RUN, control returns or the program ends.

TEST BEFORE vs TEST AFTER (PERFORM UNTIL)

For PERFORM ... UNTIL condition, TEST BEFORE (the default) means the condition is evaluated before each iteration. If the condition is true at the start, the block does not run at all. TEST AFTER means the condition is evaluated after each iteration, so the block runs at least once. Use TEST BEFORE when the loop might need zero iterations (e.g. process records until end of file, but file might be empty). Use TEST AFTER when you want to ensure one execution before checking (e.g. read-then-check loop).

Step-by-Step: Following Flow in a Program

  1. Find where execution starts (usually the first paragraph in PROCEDURE DIVISION or the entry point).
  2. Follow statements in order. When you hit IF, only one branch runs; when you hit EVALUATE, only the matching WHEN runs.
  3. When you hit PERFORM, go to the named paragraph (or inline block). Execute until that paragraph or block ends, then return to the statement after PERFORM.
  4. For PERFORM UNTIL or TIMES, repeat the paragraph or block until the condition or count is satisfied, then continue after the PERFORM.
  5. When you hit EXIT PROGRAM, STOP RUN, or GOBACK, the program (or run unit) ends; no further flow in that program.

Best Practices

Test Your Knowledge

Test Your Knowledge

1. Execution in the PROCEDURE DIVISION normally:

  • Jumps at random
  • Runs in sequence unless IF/EVALUATE/PERFORM change it
  • Runs only PERFORM
  • Stops at each paragraph

2. PERFORM PARA-1 TIMES 5 does:

  • Runs PARA-1 five times
  • Runs PARA-1 at 5 o'clock
  • Runs step 5 only
  • Invalid

3. EVALUATE is best used for:

  • Opening files
  • Multi-way selection (many possible values or conditions)
  • Defining data
  • One simple condition