COBOL Tutorial

Progress0 of 0 lessons

COBOL program control

Program control is how execution moves from one part of the program to another and how the program (or run unit) ends. The main tools are PERFORM (call a paragraph and return), GOBACK, STOP RUN, and EXIT PROGRAM. This page explains what each does and when to use it. For the order in which statements run, see Program flow.

Control verbs at a glance

Common program control statements
StatementEffect
PERFORM paragraph/sectionCall the paragraph/section; when it ends, return to the next statement
GOBACKIn main program: end run. In called program: return to caller
STOP RUNEnd the entire run unit and return to the operating system
EXIT PROGRAMIn a called program: return to the caller (no effect in main program)
GO TOJump to a paragraph/section (avoid for clear, maintainable flow)

PERFORM: call and return

PERFORM paragraph-name (or PERFORM section-name) runs that paragraph or section. When it finishes (reaches the next paragraph/section name or the end of an in-line block), control returns to the statement after the PERFORM. So the main flow “calls” the paragraph and then continues. PERFORM is the normal way to structure reusable logic without losing your place.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PROCEDURE DIVISION. MAIN. PERFORM INIT-WORK PERFORM PROCESS-ONE PERFORM CLEANUP GOBACK. INIT-WORK. MOVE ZEROS TO WS-COUNTER *> Control returns to PERFORM PROCESS-ONE . PROCESS-ONE. *> Do work . CLEANUP. CLOSE INFILE OUTFILE .

Execution order: MAIN runs, then INIT-WORK (and returns), then PROCESS-ONE (and returns), then CLEANUP, then GOBACK. So program control is “where we go next”; PERFORM keeps that predictable by always returning.

Ending the program: GOBACK vs STOP RUN

GOBACK means “exit this program.” In a main program (the one that was run by the job or CICS), GOBACK ends execution and returns to the operating system or CICS. In a called program (invoked by CALL or INVOKE), GOBACK returns control to the caller; the run unit continues.

STOP RUN always ends the entire run unit: the main program and all programs it called. Control goes back to the OS. Use STOP RUN when the whole job or transaction is done. In a subprogram, use GOBACK or EXIT PROGRAM so that only the subprogram exits and the caller continues.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
*> Main program PROCEDURE DIVISION. MAIN. CALL "SUB-PGM" USING WS-AREA *> Control returns here after SUB-PGM does GOBACK or EXIT PROGRAM GOBACK. *> Or: STOP RUN. (ends run unit) *> Called program (SUB-PGM) PROCEDURE DIVISION USING WS-AREA. SUB-MAIN. *> Do work EXIT PROGRAM. *> Or: GOBACK. (same effect: return to caller)

EXIT PROGRAM

EXIT PROGRAM is used in a program that was called by another. It returns control to the caller. In a main program (not called by anyone), EXIT PROGRAM has no effect. Prefer GOBACK in subprograms when your compiler supports it; GOBACK works in both main and called programs and reads clearly as “exit this program.”

GO TO (use sparingly)

GO TO paragraph-name sends control to that paragraph and does not return. Overuse makes flow hard to follow (“spaghetti code”). It is still used in some legacy code and for simple one-way jumps (e.g. error exit). Prefer PERFORM for normal structure and IF/EVALUATE for branching.

Step-by-step: structuring control

  • Start in the first paragraph of PROCEDURE DIVISION (or the entry point your environment defines).
  • Use PERFORM to call other paragraphs or sections; control will return after each PERFORM.
  • Use IF/EVALUATE for decisions; use scope terminators (END-IF, END-PERFORM) for clear boundaries.
  • In the main program, end with GOBACK or STOP RUN. In a called program, end with GOBACK or EXIT PROGRAM.

Test Your Knowledge

1. In a called program, what should you use to return to the caller?

  • STOP RUN
  • GOBACK or EXIT PROGRAM
  • PERFORM
  • GO TO

2. After PERFORM SOME-PARA runs, where does control go?

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

Related concepts

Related Pages