COBOL Program Structure
Every COBOL program has the same skeleton. It is divided into up to four divisions, always in this order:
| Division | Purpose | Required? |
|---|---|---|
IDENTIFICATION DIVISION |
Names the program (PROGRAM-ID), optional author/date notes |
Yes |
ENVIRONMENT DIVISION |
Describes the computer and connects file names to real files | No |
DATA DIVISION |
Declares every variable, record and file layout | No |
PROCEDURE DIVISION |
The statements that do the work | Yes (in practice) |
Think of it like a recipe card: title, kitchen equipment, ingredients, method.
The hierarchy inside a program
COBOL code is organised like a document, from large to small:
| Level | Example | Ends with |
|---|---|---|
| Division | DATA DIVISION. |
the next division |
| Section | WORKING-STORAGE SECTION. |
the next section header |
| Paragraph | MAIN-LOGIC. (a name you choose) |
the next paragraph name |
| Sentence | one or more statements | a period . |
| Statement | ADD 1 TO WS-COUNT |
the next verb |
Here is a program that uses all four divisions, two paragraphs and several sentences:
IDENTIFICATION DIVISION.
PROGRAM-ID. STRUCTURE-DEMO.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. ANY-COMPUTER.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-ORDERS PIC 9(3) VALUE 0.
PROCEDURE DIVISION.
MAIN-LOGIC.
DISPLAY "Starting order count"
PERFORM COUNT-ORDERS
DISPLAY "Orders counted: " WS-ORDERS
STOP RUN.
COUNT-ORDERS.
ADD 3 TO WS-ORDERS
ADD 4 TO WS-ORDERS.Starting order count
Orders counted: 007
Paragraphs
A paragraph is a named block of statements. The name starts in Area A (column 8) and ends with a period.
The paragraph runs until the next paragraph name. PERFORM COUNT-ORDERS jumps into the paragraph, runs it and comes
back, much like calling a function with no parameters.
Execution starts at the first statement of the PROCEDURE DIVISION and falls through paragraphs in order. That
is why MAIN-LOGIC ends with STOP RUN: without it, the program would carry on into COUNT-ORDERS a second time.
IDENTIFICATION DIVISION.
PROGRAM-ID. FALL-THROUGH.
PROCEDURE DIVISION.
FIRST-PARA.
DISPLAY "In FIRST-PARA".
SECOND-PARA.
DISPLAY "In SECOND-PARA".
THIRD-PARA.
DISPLAY "In THIRD-PARA"
STOP RUN.In FIRST-PARA
In SECOND-PARA
In THIRD-PARA
Sentences and periods
A period ends a sentence. Inside a paragraph you can write many statements in one sentence (modern style) or
end every statement with a period (older style). Both are legal, but periods also end any open IF, so modern
code puts a single period at the end of each paragraph and uses END-IF, END-PERFORM and friends inside.
*> Older style: a period after each statement
MOVE 0 TO WS-TOTAL.
ADD WS-PRICE TO WS-TOTAL.
*> Modern style: one period at the end of the paragraph
MOVE 0 TO WS-TOTAL
ADD WS-PRICE TO WS-TOTAL
.Sections in the PROCEDURE DIVISION
Paragraphs can be grouped into sections. PERFORM on a section runs every paragraph in it. Many shops use
sections as the main unit of structure:
IDENTIFICATION DIVISION.
PROGRAM-ID. SECTION-DEMO.
PROCEDURE DIVISION.
MAIN SECTION.
PERFORM REPORT-SECTION
STOP RUN.
REPORT-SECTION SECTION.
PRINT-HEADER.
DISPLAY "== Daily Report ==".
PRINT-BODY.
DISPLAY "No exceptions today.".== Daily Report ==
No exceptions today.
Note:
STOP RUNends the whole run unit. Inside a subprogram you useGOBACKinstead, which returns to the caller.GOBACKalso works in a main program, so many teams use it everywhere.
Test yourself
What is the correct order of the four divisions?
- DATA, IDENTIFICATION, ENVIRONMENT, PROCEDURE
- IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE
- IDENTIFICATION, DATA, ENVIRONMENT, PROCEDURE
- ENVIRONMENT, IDENTIFICATION, DATA, PROCEDURE
What ends a COBOL sentence?
- A semicolon
- A newline
- A period
- The keyword END
What happens if the first paragraph has no STOP RUN and no PERFORM?
- The program stops after the first paragraph
- Execution falls through into the next paragraph
- The compiler refuses the program
- The program loops forever