Mainframe Path Start learning free
Core6 min readLesson 1 of 5

Why COBOL still, and what it is good at

COBOL was designed so that business people could read it. That makes it wordy, and it also makes forty-year-old code comprehensible to someone who has never seen it before — which turns out to be worth a great deal.

The design decisions that still matter

What it is not good at

COBOL has no rich standard library, limited string handling by modern standards, and a type system that will happily let you move a character field into a numeric one and only complain at run time — which is exactly how S0C7 abends happen. Modern COBOL has objects, JSON parsing and calls into other languages, but most production code does not use them.

A complete, small program

Count records and total an amount
       IDENTIFICATION DIVISION.
       PROGRAM-ID. PAYSUM.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT PAY-FILE ASSIGN TO INFILE
               ORGANIZATION IS SEQUENTIAL
               FILE STATUS IS WS-STATUS.
       DATA DIVISION.
       FILE SECTION.
       FD  PAY-FILE.
       01  PAY-RECORD.
           05  PAY-EMP-ID        PIC X(08).
           05  PAY-NAME          PIC X(30).
           05  PAY-AMOUNT        PIC S9(7)V99 COMP-3.
           05  FILLER            PIC X(20).
       WORKING-STORAGE SECTION.
       01  WS-STATUS             PIC XX     VALUE SPACES.
       01  WS-EOF-FLAG           PIC X      VALUE 'N'.
           88  END-OF-FILE                  VALUE 'Y'.
       01  WS-COUNT              PIC 9(07)  VALUE ZERO.
       01  WS-TOTAL              PIC S9(11)V99 COMP-3 VALUE ZERO.
       01  WS-TOTAL-ED           PIC ZZZ,ZZZ,ZZ9.99-.
       PROCEDURE DIVISION.
       0000-MAIN.
           PERFORM 1000-OPEN
           PERFORM 2000-PROCESS UNTIL END-OF-FILE
           PERFORM 3000-CLOSE
           GOBACK.
       1000-OPEN.
           OPEN INPUT PAY-FILE
           IF WS-STATUS NOT = '00'
               DISPLAY 'OPEN FAILED, STATUS ' WS-STATUS
               MOVE 12 TO RETURN-CODE
               GOBACK
           END-IF
           PERFORM 2100-READ.
       2000-PROCESS.
           ADD 1 TO WS-COUNT
           ADD PAY-AMOUNT TO WS-TOTAL
           PERFORM 2100-READ.
       2100-READ.
           READ PAY-FILE
               AT END SET END-OF-FILE TO TRUE
           END-READ.
       3000-CLOSE.
           CLOSE PAY-FILE
           MOVE WS-TOTAL TO WS-TOTAL-ED
           DISPLAY 'RECORDS READ : ' WS-COUNT
           DISPLAY 'TOTAL AMOUNT : ' WS-TOTAL-ED.

Common mistakes

Assuming COBOL is a toy language

It handles decimal arithmetic, file I/O and record structure better than most modern languages, because that is what it was built for.

Writing modern-style COBOL nobody else at the site uses

Consistency with the existing codebase matters more than using every feature the compiler supports.

What you will see at work

Key terms

Check your understanding.
Take this lesson's quiz and save your progress. Free.

Take the lesson quiz
The four divisions →