Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 38 ยท Getting Started

Program Structure: Divisions, Sections and Paragraphs

See how a COBOL program is organised: four divisions, sections, paragraphs, sentences and statements, scope terminators, and a complete annotated example.

Every COBOL program has the same skeleton. Once you know it, any program, however large, becomes a map you can navigate. The structure is a strict hierarchy: divisions contain sections, sections contain paragraphs, paragraphs contain sentences, and sentences contain statements.

The four divisions

flowchart TD
    P["COBOL program"] --> I["IDENTIFICATION DIVISION<br/>who am I?"]
    P --> E["ENVIRONMENT DIVISION<br/>what machine and files?"]
    P --> D["DATA DIVISION<br/>what data do I use?"]
    P --> R["PROCEDURE DIVISION<br/>what do I do?"]

The four divisions always appear in this order; only the identification division is required.

Division Purpose
IDENTIFICATION Names the program (PROGRAM-ID).
ENVIRONMENT Describes the computer and connects program file names to real files.
DATA Declares every variable, record and file layout.
PROCEDURE Holds the logic: the statements that run.

Splitting "what" from "how" is deliberate. Data layouts live in one place and logic in another, which is why you can read a program's data model without reading a line of logic.

A complete annotated program

This program is read-only. It calculates a price with tax and prints a receipt line.

COBOL
       IDENTIFICATION DIVISION.
       PROGRAM-ID. RECEIPT.
      *
       ENVIRONMENT DIVISION.
      *
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 ITEM-NAME     PIC X(12) VALUE "NOTEBOOK".
       01 UNIT-PRICE    PIC 9(3)V99 VALUE 4.50.
       01 QUANTITY      PIC 9(2) VALUE 6.
       01 TAX-RATE      PIC V99 VALUE .08.
       01 SUBTOTAL      PIC 9(4)V99.
       01 TAX-AMOUNT    PIC 9(4)V99.
       01 GRAND-TOTAL   PIC 9(4)V99.
      *
       PROCEDURE DIVISION.
       MAIN-LOGIC.
           PERFORM CALCULATE-TOTALS
           PERFORM PRINT-RECEIPT
           STOP RUN.
      *
       CALCULATE-TOTALS.
           MULTIPLY UNIT-PRICE BY QUANTITY GIVING SUBTOTAL
           MULTIPLY SUBTOTAL BY TAX-RATE GIVING TAX-AMOUNT
           ADD SUBTOTAL TAX-AMOUNT GIVING GRAND-TOTAL.
      *
       PRINT-RECEIPT.
           DISPLAY ITEM-NAME " x " QUANTITY
           DISPLAY "Total: " GRAND-TOTAL.

Here MAIN-LOGIC, CALCULATE-TOTALS and PRINT-RECEIPT are paragraphs: named blocks in the procedure division. PERFORM runs a paragraph and then returns, much like calling a function with no arguments (see perform and paragraphs). Subtotal is 27.00, tax 2.16 and the grand total 29.16, which DISPLAY shows as 002916 (see picture clauses to understand that output).

Sections

A section is a named group of paragraphs. The data division has up to four sections: FILE SECTION, WORKING-STORAGE SECTION, LOCAL-STORAGE SECTION and LINKAGE SECTION appear in that order when used (see data division and levels). The environment division has CONFIGURATION SECTION and INPUT-OUTPUT SECTION. In the procedure division sections are optional and used to group paragraphs:

COBOL
       PROCEDURE DIVISION.
       INITIALISATION SECTION.
       OPEN-FILES.
           ...
       PROCESSING SECTION.
       READ-LOOP.
           ...

Sentences, statements and periods

A statement is one verb with its operands, such as ADD 1 TO COUNTER. A sentence is one or more statements ended by a period. The period is the biggest trap in the language, because it ends every open nested statement:

COBOL
           IF BALANCE < 0
               DISPLAY "Overdrawn"
               MOVE 0 TO BALANCE.
           DISPLAY "Next".

Modern style uses scope terminators (END-IF, END-PERFORM, END-READ, END-EVALUATE, ...) and puts a single period at the end of the paragraph:

COBOL
           IF BALANCE < 0
               DISPLAY "Overdrawn"
               MOVE 0 TO BALANCE
           END-IF
           DISPLAY "Next".

The second version makes the extent of the IF obvious and is safe to edit later. Use it throughout this course.

Reading a program quickly (Python stand-in)

This runnable block is a Python program, not COBOL. It does what you do with your eyes on a new listing: scan for division, section and paragraph headers to get a table of contents.

Python Outline a COBOL listing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trick used here is real: paragraph and section names start in area A, and statements start in area B.

A second stand-in: PERFORM as a call

Python Paragraphs as named blocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the correct order of the four divisions?

    The order is always Identification, Environment, Data, Procedure.
  2. 2. Where do paragraph names start in fixed format?

    Division, section and paragraph names start in area A; statements go in area B.
  3. 3. What is the main benefit of END-IF over ending an IF with a period?

    A period ends all open nested statements. A scope terminator ends only its own.
  4. 4. Which division is the only one every program must have?

    Only the program name is mandatory; the identification division (or its PROGRAM-ID) must be present.

Summary

  • A program is divisions, then sections, paragraphs, sentences and statements.
  • The four divisions are Identification, Environment, Data and Procedure, always in that order.
  • Paragraph and section names start in area A, statements in area B.
  • A period ends a sentence and all statements open inside it; prefer END-IF and friends.
  • PERFORM paragraph runs a named block and comes back.

Next: Identification and Environment Divisions