The four divisions
Every COBOL program is organised into up to four divisions, always in this order:
| Division | Answers the question | Required? |
|---|---|---|
IDENTIFICATION |
What is this program called? | Yes |
ENVIRONMENT |
What machine and files does it use? | No |
DATA |
What data does it work on? | No |
PROCEDURE |
What does it do? | In practice, yes |
COBOL was designed in 1959 so that business managers and auditors could read a program, not just programmers. Separating what data exists from what the program does is part of that idea, and it is why a COBOL program reads a bit like a formal document.
Here is a program that uses all four. The lines with just a * in
column 7 are blank comment lines, used here as spacers (more on that in the
next lesson):
IDENTIFICATION DIVISION.
PROGRAM-ID. BRANCHRP.
AUTHOR. J MURPHY.
*
ENVIRONMENT DIVISION.
*
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BRANCH-NAME PIC X(10) VALUE "CORK".
*
PROCEDURE DIVISION.
DISPLAY "BRANCH: " WS-BRANCH-NAME.
STOP RUN.
It prints:
BRANCH: CORK
(WS-BRANCH-NAME holds ten characters, so six trailing spaces follow
CORK on that line. You will see why in the next module.)
Divisions, sections, paragraphs, sentences
COBOL has a hierarchy, a bit like a book:
- Divisions are split into sections (for example
WORKING-STORAGE SECTION.inside theDATA DIVISION). - Sections contain paragraphs, which are named blocks of code.
- Paragraphs contain sentences, which end with a period.
- Sentences contain statements such as
DISPLAYandSTOP RUN.
What goes where
- IDENTIFICATION:
PROGRAM-IDis the only required entry.AUTHORandDATE-WRITTENare old optional entries you will still see in production code. They do nothing; GnuCOBOL accepts them but prints an "obsolete" warning, which you can ignore. - ENVIRONMENT: in batch programs this is where
FILE-CONTROLlinks each file in the program to a DD name in the JCL. It can be empty, as above. - DATA:
WORKING-STORAGE SECTIONholds the program's variables. Each one is declared with a level number (01), a name and aPIC(picture) clause saying what it can hold.VALUEgives it a starting value. Later you will add aFILE SECTIONhere for record layouts. - PROCEDURE: the instructions, executed from the top.
Naming data
Data names can be up to 30 characters of letters, digits and hyphens.
The WS- prefix is not required by the language, but almost every shop
uses it for working-storage items so you can tell at a glance where a
field lives.
On the job
When you open an unfamiliar program, read the DATA DIVISION first.
Knowing the record layouts and working fields makes the
PROCEDURE DIVISION far easier to follow.
Your task
Complete the program so it has all four divisions in the right order.
- Add an empty
ENVIRONMENT DIVISION. - Add a
DATA DIVISION.with aWORKING-STORAGE SECTION.that declaresWS-SYSTEM-NAMEasPIC X(7)withVALUE "PAYROLL". - Change the
DISPLAYso it prints the literalSYSTEM:followed by the contents ofWS-SYSTEM-NAME.
Expected output:
SYSTEM: PAYROLL