The four divisions
A COBOL program is not a list of statements: it is a stack of divisions. Each division begins with its name and a period, holds one kind of information, and gives way to the next. The order is fixed, the compiler enforces it, and a division that has nothing to say may be left out entirely.
Only two divisions appear in a program that prints one line: the one that names the program and the one that holds the statements.
A stack of divisions
IDENTIFICATION DIVISION.
PROGRAM-ID. MIN.
PROCEDURE DIVISION.
DISPLAY "two divisions".
STOP RUN.
cobc -x -free -Wall -o min min.cob
./min
two divisions
Those two are the ends of the stack: IDENTIFICATION says what the program is, PROCEDURE says what it does. The two divisions between them describe the machine and the storage, and a program that needs neither can leave both out.
IDENTIFICATION DIVISION
The first division is the only one a program must have. It names the program with PROGRAM-ID, and it may carry documentation paragraphs such as AUTHOR and DATE-WRITTEN. A source that starts anywhere else is refused at its first line with error: PROGRAM-ID header missing, and the snippet under the message points at the line that took the place of the header.
ENVIRONMENT DIVISION
The second division describes the machine the program runs on and the files it reads and writes. It has two sections: CONFIGURATION SECTION, with SOURCE-COMPUTER, OBJECT-COMPUTER and SPECIAL-NAMES, and INPUT-OUTPUT SECTION, with FILE-CONTROL and I-O-CONTROL. The SELECT of a file is written in FILE-CONTROL, which is the subject of the lesson on SELECT and FD.
The two computer paragraphs are documentation: the compiler reads the names and does nothing with them, so a program that names a 1970s mainframe builds on a laptop and prints the same output. A division that is nothing but those two paragraphs still shows where the file declarations will go when the program grows one.
DATA DIVISION
The third division describes every byte of storage the program uses: files, working storage, local storage and the parameters a caller passes in. It is built from sections, and the sections are optional as well: a division header with nothing under it is a program without storage, and it builds.
IDENTIFICATION DIVISION.
PROGRAM-ID. NODATA.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY "no sections".
STOP RUN.
cobc -x -free -Wall -o nodata nodata.cob
./nodata
no sections
The sections themselves are the subject of the lesson on the DATA DIVISION. For now the point is the shape: a division is a header, and a section is a header inside it.
PROCEDURE DIVISION
The last division holds the statements. It is written as paragraphs and sentences, and it may also be absent: a program that declares storage and never uses it is legal, does nothing, and leaves the exit status at 0.
IDENTIFICATION DIVISION.
PROGRAM-ID. NOPROC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N PIC 9 VALUE 5.
cobc -x -free -Wall -o noproc noproc.cob
./noproc
echo "status: $?"
status: 0
Nothing is printed, because there is nothing to print: the program has no statement at all. The next lesson on the PROCEDURE DIVISION builds a program out of paragraphs.
The order is fixed
The four divisions are read in one order and no other: IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE. A division written after the one that follows it is a syntax error, and the message names the word it did not expect in that place:
IDENTIFICATION DIVISION.
PROGRAM-ID. ORDER.
PROCEDURE DIVISION.
DISPLAY "PROCEDURE comes first here".
STOP RUN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N PIC 9 VALUE 0.
cobc -x -free -Wall -o order order.cob
order.cob:8: error: syntax error, unexpected DATA
6 | STOP RUN.
7 |
8 > DATA DIVISION.
9 | WORKING-STORAGE SECTION.
10 | 01 N PIC 9 VALUE 0.
Two more errors follow it, because the section and the declaration under the misplaced header have nowhere to go either. The first error is the one to fix: once the division is back in place, the others disappear.
A statement in the wrong division is the same mistake seen from the other side. A DISPLAY written under WORKING-STORAGE SECTION is refused with error: PROCEDURE DIVISION header missing, because the data division describes storage and cannot hold an action.
Exercises
- Write a program with all four divisions in which the first two lines of the PROCEDURE DIVISION print the values of two fields declared in WORKING-STORAGE. Which division did you not need to touch?
- Move the DATA DIVISION of that program below the PROCEDURE DIVISION and build it. Copy the first error line, then put the division back and confirm that the build is silent.
- Delete the ENVIRONMENT DIVISION from a program that has one and build again. What changes in the output, and what changes in the exit status?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
In which order does a COBOL program write its divisions?
Answer
IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE
The four divisions describe the program, the machine, the storage and the actions, and the compiler reads them in that order.
-
Which division must every program have?
Answer
IDENTIFICATION DIVISION
It carries PROGRAM-ID, and a source that starts with any other division is refused with
error: PROGRAM-ID header missing. -
In which division is the
SELECT ... ASSIGN TOof a file written?Answer
ENVIRONMENT DIVISION, in the INPUT-OUTPUT SECTION
The environment division declares which files the program uses; the data division describes the record each file holds.
-
Which division holds
WORKING-STORAGE SECTION?Answer
DATA DIVISION
Working storage is storage, and every description of storage belongs to the data division.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. PROCEDURE DIVISION. DISPLAY "two divisions". STOP RUN.Answer
two divisions
The environment and data divisions are optional, so a program of two divisions builds and prints its one line.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-370. OBJECT-COMPUTER. IBM-370. DATA DIVISION. WORKING-STORAGE SECTION. 01 AMOUNT PIC 9(4) VALUE 2024. PROCEDURE DIVISION. DISPLAY "AMOUNT: " AMOUNT. STOP RUN.Answer
AMOUNT: 2024
The computer paragraphs are documentation, and PIC 9(4) holds four digits, so a four-digit value prints as it was written.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. PROCEDURE DIVISION. DISPLAY "one". STOP RUN. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 9 VALUE 0.Answer
error: syntax error, unexpected DATA
The data division cannot follow the procedure division, and the compiler reports the header it did not expect at that place.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. DISPLAY "in the data division". PROCEDURE DIVISION. STOP RUN.Answer
error: PROCEDURE DIVISION header missing
The data division describes storage, so a statement there is read as a declaration, and the compiler finds no procedure division where it expected one.
-
What happens to a program that has no PROCEDURE DIVISION?
Answer
It builds, runs, prints nothing and exits with status 0
A procedure division is optional: without one the program has no statement to run, and a run that does nothing is a successful run.
-
A DATA DIVISION is written with no section under it. What does the build do?
Answer
It builds the program, which has no storage of its own
The sections are optional, so a header with nothing under it declares nothing and is not an error.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev