COBOLMainframe

COBOL Program Structure: 4 Divisions with Annotated Code Examples

TT
TopicTrick
COBOL Program Structure: 4 Divisions with Annotated Code Examples

Every COBOL program ever written follows the same skeleton: four divisions in a fixed order, each serving a distinct architectural purpose. Understanding this structure is the foundation of all COBOL development, whether you are writing your first "Hello World" or maintaining a 500,000-line banking system. This guide walks through each division in detail with working examples.

What you'll learn in this guide:

  • How COBOL's four divisions (IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE) are structured and why they exist
  • The syntax of PROGRAM-ID, AUTHOR, and the IDENTIFICATION DIVISION header
  • How sections and paragraphs organise executable logic in the PROCEDURE DIVISION
  • Best practices for program naming, layout, and avoiding FALL-THROUGH bugs

Next lesson: COBOL Data Division: PIC Clauses, COMP Types, and Level Numbers

The Overall Shape of a COBOL Program

Before diving into each division, here is a complete minimal COBOL program so you can see how all the pieces connect:

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO001.
AUTHOR.     TOPICTRICK.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-3090.
OBJECT-COMPUTER. IBM-3090.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-MESSAGE         PIC X(30) VALUE 'HELLO FROM COBOL'.

PROCEDURE DIVISION.
    DISPLAY WS-MESSAGE
    STOP RUN.

This compiles and runs. Every element here has a specific place in the division hierarchy. Let us examine each division in depth.

IDENTIFICATION DIVISION

The IDENTIFICATION DIVISION is always first. It provides metadata about the program — information the compiler and operating system need to identify and manage the compiled load module.

PROGRAM-ID — Required

PROGRAM-ID is the only required paragraph in the entire IDENTIFICATION DIVISION. It gives the program its name:

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. CUSTMGR.

On z/OS, this name appears in system dumps, SMF records, and CICS resource definitions. Choose names that are meaningful and 8 characters or fewer to match PDS member name limitations.

Optional Paragraphs

These paragraphs are syntactically valid but ignored by modern compilers — they serve as human-readable documentation:

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. CUSTMGR.
AUTHOR.      J SMITH.
DATE-WRITTEN. 2025-01-10.
DATE-COMPILED. 2026-01-20.
REMARKS.
    CUSTOMER ACCOUNT MANAGEMENT MODULE.
    MAINTAINS THE CUSTOMER MASTER FILE.
    CALLED BY: ORDERMGR, RPTMONTH.

The REMARKS paragraph is especially useful for capturing the program's purpose, calling programs, and modification history in a standard location.

ENVIRONMENT DIVISION

The ENVIRONMENT DIVISION handles platform-specific configuration. It bridges the gap between the logical COBOL program and the physical operating environment.

CONFIGURATION SECTION

The CONFIGURATION SECTION identifies the source and object computer. On modern z/OS this is largely ceremonial but still commonly included:

cobol
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-3090.
OBJECT-COMPUTER. IBM-3090.
SPECIAL-NAMES.
    DECIMAL-POINT IS COMMA.

SPECIAL-NAMES is the most useful entry here. Common uses include:

  • DECIMAL-POINT IS COMMA — European numeric format (1.234,56 instead of 1,234.56)
  • UPSI-0 ON STATUS IS SW-ON — program switches for conditional compilation
  • C01 IS TOP-OF-PAGE — printer channel control for report programs

INPUT-OUTPUT SECTION and FILE-CONTROL

This is where files are assigned. Every file the program uses must appear here with a SELECT statement:

cobol
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT CUSTOMER-FILE
        ASSIGN TO CUSTMST
        ORGANIZATION IS INDEXED
        ACCESS MODE IS RANDOM
        RECORD KEY IS CF-CUSTOMER-ID
        FILE STATUS IS WS-CUST-STATUS.

    SELECT REPORT-FILE
        ASSIGN TO RPTOUT
        ORGANIZATION IS SEQUENTIAL
        ACCESS MODE IS SEQUENTIAL
        FILE STATUS IS WS-RPT-STATUS.

The ASSIGN TO value (CUSTMST, RPTOUT) is the ddname — the name in the JCL DD statement or CICS file definition that maps the logical name to a physical dataset. This is the critical link between the COBOL program and the operating system.

FILE STATUS assigns a two-character working-storage field that receives the I/O return code after every file operation. 00 means success; other codes indicate errors.

DATA DIVISION

The DATA DIVISION defines every piece of data the program uses. It is structured into sections, each with a specific purpose.

FILE SECTION

The FILE SECTION defines the record layout for each file declared in FILE-CONTROL:

cobol
DATA DIVISION.
FILE SECTION.
FD  CUSTOMER-FILE
    RECORDING MODE IS F
    BLOCK CONTAINS 0 RECORDS
    RECORD CONTAINS 200 CHARACTERS.
01  CUSTOMER-RECORD.
    05 CF-CUSTOMER-ID    PIC X(10).
    05 CF-CUSTOMER-NAME  PIC X(40).
    05 CF-BALANCE        PIC S9(11)V99 COMP-3.
    05 CF-STATUS-CODE    PIC X(2).
    05 FILLER            PIC X(147).

The FD (File Description) entry names the file and describes its physical attributes. BLOCK CONTAINS 0 tells the compiler to use the system-determined block size — the standard for z/OS QSAM files.

The 01 record that follows is the I/O buffer. Data is moved here for WRITE operations or moved from here after READ.

WORKING-STORAGE SECTION

WORKING-STORAGE holds all the variables your program manipulates. This is the workhorse section — counters, flags, work areas, output lines, and sub-program communication areas all live here:

cobol
WORKING-STORAGE SECTION.
01 WS-FLAGS.
   05 WS-EOF-FLAG         PIC X     VALUE 'N'.
      88 WS-EOF                     VALUE 'Y'.
      88 WS-NOT-EOF                 VALUE 'N'.
   05 WS-ERROR-FLAG        PIC X     VALUE 'N'.

01 WS-COUNTERS.
   05 WS-RECORDS-READ      PIC 9(9) COMP VALUE ZERO.
   05 WS-RECORDS-WRITTEN   PIC 9(9) COMP VALUE ZERO.
   05 WS-ERROR-COUNT       PIC 9(5) COMP VALUE ZERO.

01 WS-WORK-AREA.
   05 WS-FORMATTED-DATE    PIC X(10).
   05 WS-AMOUNT-DISPLAY    PIC ZZZ,ZZZ,ZZ9.99-.

88-level condition names (WS-EOF, WS-NOT-EOF) give Boolean semantics to character flags. You test IF WS-EOF instead of IF WS-EOF-FLAG = 'Y' — cleaner and less error-prone.

LINKAGE SECTION

The LINKAGE SECTION defines data passed from a calling program. It does not allocate storage; it maps COBOL names to memory addresses passed at runtime via the USING phrase:

cobol
LINKAGE SECTION.
01 LS-COMMAREA.
   05 LS-CUSTOMER-ID    PIC X(10).
   05 LS-REQUEST-TYPE   PIC X(1).
   05 LS-RETURN-CODE    PIC S9(4) COMP.

In CICS programs, the DFHCOMMAREA is defined in the LINKAGE SECTION. For called subprograms, any parameters passed BY REFERENCE are defined here.

LOCAL-STORAGE SECTION

The LOCAL-STORAGE SECTION (available in IBM Enterprise COBOL 4.2+) allocates storage on each CALL and releases it on RETURN — equivalent to stack variables in C. This is essential for recursive programs and thread-safe subprograms:

cobol
LOCAL-STORAGE SECTION.
01 LS-WORK-BUFFER       PIC X(1000).
01 LS-LOOP-COUNTER      PIC 9(5) COMP.

PROCEDURE DIVISION

The PROCEDURE DIVISION contains the executable code. It is organized into sections and paragraphs.

PROCEDURE DIVISION Header

If the program receives parameters, declare them in the USING clause:

cobol
PROCEDURE DIVISION USING LS-COMMAREA.

For programs with no parameters, the header is simply:

cobol
PROCEDURE DIVISION.

Sections and Paragraphs

Sections group related paragraphs. A section begins with a name followed by SECTION.:

cobol
PROCEDURE DIVISION.

MAIN-LOGIC SECTION.
    PERFORM INITIALIZE-PROGRAM
    PERFORM PROCESS-RECORDS UNTIL WS-EOF
    PERFORM FINALIZE-PROGRAM
    STOP RUN.

INITIALIZE-PROGRAM SECTION.
    OPEN INPUT CUSTOMER-FILE
    OPEN OUTPUT REPORT-FILE
    PERFORM READ-CUSTOMER.

PROCESS-RECORDS SECTION.
    PERFORM VALIDATE-RECORD
    IF WS-RECORD-VALID
        PERFORM WRITE-REPORT-LINE
        ADD 1 TO WS-RECORDS-WRITTEN
    END-IF
    PERFORM READ-CUSTOMER.

READ-CUSTOMER SECTION.
    READ CUSTOMER-FILE INTO WS-WORK-AREA
        AT END SET WS-EOF TO TRUE
    END-READ.

The main logic paragraph (MAIN-LOGIC SECTION) acts as a table of contents for the program. This "top-down" structure makes large COBOL programs readable: you see the high-level flow immediately, then drill into each section for detail.

STOP RUN vs EXIT PROGRAM vs GOBACK

These three statements end program execution differently:

  • STOP RUN — terminates the entire run unit. Use in main programs.
  • EXIT PROGRAM — returns to the calling program. If there is no caller, behaves like STOP RUN. Use in subprograms.
  • GOBACK — equivalent to EXIT PROGRAM in subprograms, and STOP RUN in main programs. The preferred choice in IBM Enterprise COBOL because it works correctly in both contexts.

Column Layout: The Legacy Constraint

Traditional COBOL uses a fixed column layout inherited from 80-column punched cards:

ColumnsPurpose
1–6Sequence numbers (ignored by modern compilers)
7Indicator area: * for comment, - for continuation
8–11Area A — division, section, paragraph names; FD, SD, 01 levels
12–72Area B — all other code
73–80Identification area (ignored)

IBM Enterprise COBOL supports free format compilation mode where these column restrictions do not apply. Specify PROCESS FREE or compile with COBOL(FREE) to use modern free-form source. Most new z/OS development uses fixed format for compatibility with existing SCLM and Endevor configurations, but free format is increasingly common.

Putting It Together: A Complete Example

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. BALCHK.
AUTHOR.     TOPICTRICK.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT ACCOUNT-FILE
        ASSIGN TO ACCTMST
        ORGANIZATION IS INDEXED
        ACCESS MODE IS RANDOM
        RECORD KEY IS AF-ACCOUNT-ID
        FILE STATUS IS WS-FILE-STATUS.

DATA DIVISION.
FILE SECTION.
FD  ACCOUNT-FILE
    RECORD CONTAINS 100 CHARACTERS.
01  ACCOUNT-RECORD.
    05 AF-ACCOUNT-ID     PIC X(10).
    05 AF-BALANCE        PIC S9(11)V99 COMP-3.
    05 AF-STATUS         PIC X(2).
    05 FILLER            PIC X(75).

WORKING-STORAGE SECTION.
01 WS-FILE-STATUS        PIC XX VALUE SPACES.
   88 WS-FILE-OK                  VALUE '00'.
   88 WS-RECORD-NOT-FOUND         VALUE '23'.

01 WS-SEARCH-ID          PIC X(10).

LINKAGE SECTION.
01 LS-PARMS.
   05 LS-ACCOUNT-ID      PIC X(10).
   05 LS-BALANCE         PIC S9(11)V99 COMP-3.
   05 LS-RETURN-CODE     PIC S9(4) COMP.

PROCEDURE DIVISION USING LS-PARMS.
MAIN-LOGIC.
    MOVE LS-ACCOUNT-ID TO AF-ACCOUNT-ID
    READ ACCOUNT-FILE
    IF WS-FILE-OK
        MOVE AF-BALANCE TO LS-BALANCE
        MOVE 0 TO LS-RETURN-CODE
    ELSE
        MOVE -1 TO LS-RETURN-CODE
    END-IF
    GOBACK.

This subprogram illustrates all four divisions working together: IDENTIFICATION names it, ENVIRONMENT assigns the file, DATA defines the record and parameters, and PROCEDURE reads the record and returns.

Next Steps

With program structure understood, the natural next topic is the DATA DIVISION in depth — specifically PIC clauses, COMP storage types, and level numbers. The COBOL Data Division guide covers everything you need to define data correctly. For the broader curriculum, visit the COBOL Mastery course.

Frequently Asked Questions

Q: What are the four divisions of a COBOL program and what does each contain?

Every COBOL program has four divisions in fixed order. The IDENTIFICATION DIVISION names the program (PROGRAM-ID is mandatory; AUTHOR, DATE-WRITTEN, and REMARKS are optional documentation). The ENVIRONMENT DIVISION describes the computing environment and maps file names to physical files (CONFIGURATION SECTION and INPUT-OUTPUT SECTION). The DATA DIVISION defines all data structures — files, working storage, and linkage areas. The PROCEDURE DIVISION contains the executable logic. Only IDENTIFICATION DIVISION and PROCEDURE DIVISION are strictly mandatory; a minimal valid COBOL program needs just those two.

Q: Why does COBOL use SECTIONS and PARAGRAPHS rather than functions?

COBOL was designed for business data processing in the late 1950s, when structured programming concepts were not yet established. Sections and paragraphs evolved as the primary units of code organisation before subroutines became standard. In modern COBOL, paragraphs function like methods — they are called with PERFORM and return when complete. Sections group related paragraphs and can be performed as a unit. For new code, most shops standardise on one paragraph per logical function and avoid FALL-THROUGH between paragraphs (where execution continues into the next paragraph without an explicit PERFORM).

Q: What is the purpose of the STOP RUN statement?

STOP RUN terminates the COBOL program and returns control to the operating system or the calling program. It closes all open files automatically. In a main program (not called by another COBOL program), STOP RUN ends the job step. In a subprogram called via CALL, you should use GOBACK instead of STOP RUN — GOBACK returns to the caller, while STOP RUN terminates the entire run unit including the calling program. Mixing up STOP RUN and GOBACK in subprograms is a classic COBOL bug that ends an entire batch job prematurely.


Ready to Master COBOL?

This lesson is part of the COBOL Mastery Course — the complete reference from first program to production mainframe. 20 modules covering COBOL syntax, file handling, DB2, CICS, JCL, and modern features. Free, fresher to senior.

→ View the full COBOL Mastery Course