MODULE 1 · FIRST STEPS · 3/8

The four divisions

10 min30 XPExercise

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 the DATA DIVISION).
  • Sections contain paragraphs, which are named blocks of code.
  • Paragraphs contain sentences, which end with a period.
  • Sentences contain statements such as DISPLAY and STOP RUN.

What goes where

  • IDENTIFICATION: PROGRAM-ID is the only required entry. AUTHOR and DATE-WRITTEN are 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-CONTROL links each file in the program to a DD name in the JCL. It can be empty, as above.
  • DATA: WORKING-STORAGE SECTION holds the program's variables. Each one is declared with a level number (01), a name and a PIC (picture) clause saying what it can hold. VALUE gives it a starting value. Later you will add a FILE SECTION here 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.

  1. Add an empty ENVIRONMENT DIVISION.
  2. Add a DATA DIVISION. with a WORKING-STORAGE SECTION. that declares WS-SYSTEM-NAME as PIC X(7) with VALUE "PAYROLL".
  3. Change the DISPLAY so it prints the literal SYSTEM: followed by the contents of WS-SYSTEM-NAME.

Expected output:

SYSTEM: PAYROLL
fixed format
Run your program to see its output here. The first visible test's input and datasets are used.
Submit to grade your program against every test.