Skip to content
LumenaryAcademy
Foundations 50 min

First steps in COBOL

Where COBOL came from, why banks and governments still run it, and a first program in four divisions.

Code tested · 3AI-draftedAwaiting review by a computer scientist
MotivationH

Why learn COBOL in 2026?

When you are paid a salary, when a card payment is approved at a shop, when an insurance premium is calculated or a pension arrives, there is a good chance that somewhere along the way a COBOL program did the arithmetic. COBOL is not fashionable and it is not where most new software is written. It is something else: a language that was designed for business records and money in 1959, that did that job well enough to be trusted with the core of banks, insurers and governments, and that has been running those systems, maintained and extended, ever since.

People sometimes quote dramatic figures about how many lines of COBOL exist or what fraction of transactions it handles. Those estimates vary wildly and are hard to check, so this course will not repeat them. What is easy to check is that the major mainframe vendor, IBM, still ships and updates its Enterprise COBOL compiler; that the language has an international standard revised as recently as 2023; and that organisations which depend on it struggle to find people who can read it. In April 2020, when unemployment claims surged at the start of the COVID-19 pandemic, several US state agencies publicly asked for volunteers and retirees who knew COBOL to help keep their benefit systems running. That was a reminder that the code is still there, and that the people who understand it are scarce.

So there are good reasons to learn it:

  • Work that matters. Payroll, payments, tax and benefits touch everyone. Keeping them correct is honourable, careful work.
  • Modernisation needs readers. Every project that moves a COBOL system to Java, to the cloud or to a newer COBOL starts with people who can read the old code and say exactly what it does.
  • A different way of thinking about data. COBOL describes data as fixed-width records with exact decimal fields. Learning it teaches you why money should never be stored in a binary floating-point number, and what a file format really is.
  • History you can run. Many ideas you meet in other languages (records, readable keywords, portability across machines, a standard maintained by a committee) were tried here first.

There are honest costs too. COBOL is verbose. Its tooling outside the mainframe world is thinner than for younger languages. Its best-paid work usually requires learning a whole platform (z/OS, JCL, CICS, Db2) on top of the language. This course teaches the language thoroughly with a free compiler on your own computer, and describes the platform so that you know what you are walking into.

History & global context

A committee, a navy officer and a common language

In the 1950s every computer maker had its own languages, and a program written for one machine had to be rewritten for the next. Businesses and governments buying computers found this expensive. In May 1959 a meeting at the US Department of Defense brought together computer manufacturers, large users and government agencies to discuss a common business language. The group became known as CODASYL, the Conference on Data Systems Languages, and a short-range committee was given a few months to produce a specification.

The committee did not start from nothing. The strongest influence was FLOW-MATIC, a language for the UNIVAC developed from 1955 by a team led by Grace Hopper at Remington Rand. Hopper had written one of the first compilers and was convinced that business programs should be written in English-like words rather than mathematical symbols, so that managers and auditors could read them. FLOW-MATIC programs said things like "compare", "transfer" and "if greater go to". IBM's COMTRAN design, led by Bob Bemer, contributed ideas too, and committee members such as Jean Sammet of Sylvania shaped the result. Hopper served as a technical adviser to the committee, and she later did much to spread the language; she is often, fondly and a little loosely, called the mother of COBOL.

The specification was published in 1960 as COBOL-60. In December 1960 the same COBOL program, with only minor changes, was compiled and run on computers from two different makers, RCA and Remington Rand, which was a striking demonstration of portability at the time. The Department of Defense, then the largest buyer of computers, made clear that it wanted COBOL on the machines it bought, so manufacturers had every reason to supply compilers. Within a decade COBOL was the most widely used business language in the world.

The language kept evolving through a series of standards:

StandardWhat it brought
ANSI COBOL 1968, 1974The first official standards; file handling and the core verbs settled
ANSI COBOL 1985Structured programming: END-IF, inline PERFORM, EVALUATE, scope terminators
ISO COBOL 2002Free-format source, object orientation, user-defined functions, recursion
ISO COBOL 2014, 2023Cleanups, more intrinsic functions, longer names, new operators and messaging

The year 2000 problem made COBOL briefly famous again: many programs stored years as two digits to save precious storage, and teams around the world spent the late 1990s finding and fixing date logic before 1 January 2000. Most of those systems are still running today.

IntuitionH

A program as a structured document

A COBOL program reads less like a script and more like a formal document with fixed chapters. Every program is divided into up to four divisions, always in this order:

DivisionWhat it saysRequired?
IDENTIFICATION DIVISIONThe program's name (PROGRAM-ID)Yes
ENVIRONMENT DIVISIONHow the program connects to the outside world: files, devices, local conventionsOnly when needed
DATA DIVISIONEvery piece of data the program uses, with its exact size and formatOnly when needed
PROCEDURE DIVISIONThe statements that do the workYes, in practice

Inside the divisions there is a hierarchy: a division contains sections, a section contains paragraphs, a paragraph contains sentences, and a sentence is one or more statements ending in a full stop (a period). You will meet sections in the data division right away (WORKING-STORAGE SECTION) and paragraphs in the procedure division in lesson 4.

Analogy, not a formal construction

Think of a government form. The top box says what the form is (identification). A box of instructions says where to send it and which conventions apply, such as whether dates are written day first (environment). Then there is a grid of labelled fields, each with a fixed number of character boxes (data). Only at the end come the steps to follow (procedure). Nothing in the procedure may refer to a field that the form did not lay out in advance.

That last point is the heart of COBOL. You do not create variables as you go. Every item is declared in the data division with a picture that says exactly how many characters or digits it holds. PIC X(20) is twenty characters of text; PIC 9(4)V99 is a number with four digits before an implied decimal point and two after. The procedure division then works on those fixed boxes. This is why COBOL is so good at business records: a record in a file is a row of fixed boxes, and the program describes it the same way.

ComputationC

Installing GnuCOBOL and a first program

Getting a compiler

Professional COBOL is mostly compiled with IBM's Enterprise COBOL on z/OS mainframes, and some with commercial compilers on Linux and Windows. To learn, you need none of that. GnuCOBOL is a free, standard-conforming compiler from the GNU project. It translates COBOL into C and then uses your system's C compiler, so the result is an ordinary native program.

  • Linux: install the package from your distribution, usually named gnucobol (for example sudo apt install gnucobol on Debian and Ubuntu).
  • macOS: brew install gnucobol with Homebrew.
  • Windows: the GnuCOBOL site links ready-made builds; installing it inside WSL (Windows Subsystem for Linux) also works well.

Check it with cobc --version. This course was tested with GnuCOBOL 3.2, released in 2023 and the current stable release as of late 2026.

Hello, COBOL

Save this as main.cob:

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. hello.
PROCEDURE DIVISION.
    DISPLAY "Hello from COBOL!"
    STOP RUN.

Compile and run it:

bash
cobc -x -free main.cob -o main
./main
text
Hello from COBOL!

The flags matter. -x asks for an executable program (without it, cobc builds a module meant to be called by another program). -free says the source is in free format, which we explain below. -o main names the output. Add -Wall while learning to see every warning.

Read the program line by line. IDENTIFICATION DIVISION. opens the first division; PROGRAM-ID. hello. names the program. There is no environment or data division, because this program has no files and no data. PROCEDURE DIVISION. starts the work. DISPLAY writes its operands to the screen followed by a newline. STOP RUN ends the program and returns control to the operating system. The full stop after STOP RUN ends the sentence.

Keywords are not case-sensitive: display, Display and DISPLAY are the same. Older code is written entirely in capitals because early card punches and printers had no lower case; modern shops vary. This course writes keywords in capitals and its own names in lower case, so you can always tell which is which.

All four divisions

Here is a program that uses all four divisions. It prints one line of an invoice for a customer in a country that writes a decimal comma, as much of Europe, Latin America and Africa does:

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. invoice-line.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
    DECIMAL-POINT IS COMMA.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 customer-name    PIC X(20)    VALUE "Ngozi Okafor".
01 quantity         PIC 9(3)     VALUE 340.
01 unit-price       PIC 9(4)V99  VALUE 12,85.
01 line-total       PIC 9(6)V99.
01 line-total-out   PIC ZZZ.ZZ9,99.
PROCEDURE DIVISION.
    MULTIPLY quantity BY unit-price GIVING line-total
    MOVE line-total TO line-total-out
    DISPLAY "Customer: " customer-name
    DISPLAY "Quantity: " quantity
    DISPLAY "Total:    " line-total-out " EUR"
    STOP RUN.
text
Customer: Ngozi Okafor        
Quantity: 340
Total:      4.369,00 EUR

A great deal is happening in a few lines:

  • The environment division tells the compiler that in this program the comma is the decimal point. That changes how the source is read (the literal 12,85 means twelve and eighty-five hundredths) and how numbers are printed.
  • The data division has a WORKING-STORAGE SECTION, where a program keeps the data it works on. Each 01 line declares one item: a name, a PIC (picture) clause giving its size and kind, and optionally a starting VALUE.
  • customer-name is twenty characters, so the name is padded with spaces to fill the box. You can see the trailing spaces in the output: the line Customer: Ngozi Okafor is followed by eight blanks.
  • unit-price is 9(4)V99: four digits, an implied decimal point (V), two digits. The point is not stored; the compiler simply remembers where it is.
  • line-total-out is an edited picture. Moving a number into it inserts the thousands separator and the decimal mark and replaces leading zeros with spaces (Z). That is how COBOL formats output, and lesson 2 covers it properly.
  • MULTIPLY ... GIVING computes 340 × 12.85 exactly, in decimal, and stores 4369.00 in line-total.

Notice too that statements inside a paragraph need no full stop: the compiler knows where each one begins from its keyword. Modern style puts one full stop at the end of each paragraph and nowhere else, for reasons lesson 4 explains.

A taste of a loop

COBOL's loop is PERFORM. This program prints part of the seven times table:

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. times-table.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 n        PIC 99  VALUE 7.
01 i        PIC 99.
01 product  PIC 999.
PROCEDURE DIVISION.
    PERFORM VARYING i FROM 1 BY 1 UNTIL i > 5
        MULTIPLY n BY i GIVING product
        DISPLAY n " x " i " = " product
    END-PERFORM
    STOP RUN.
text
07 x 01 = 007
07 x 02 = 014
07 x 03 = 021
07 x 04 = 028
07 x 05 = 035

The leading zeros are not a bug. PIC 99 means "exactly two decimal digits", and DISPLAY shows all of them. In a real report you would move each number into an edited picture such as Z9 to suppress them. PERFORM VARYING ... UNTIL is a counting loop: it sets i to 1, tests the condition before each pass, runs the body, and adds 1. END-PERFORM closes it.

Fixed format and free format

Open almost any COBOL program written before about 2010 and it will look like this:

text
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. HELLO.
000300 PROCEDURE DIVISION.
000400*    THIS LINE IS A COMMENT
000500     DISPLAY 'HELLO FROM COBOL!'
000600     STOP RUN.

This is fixed format, inherited from 80-column punched cards. Each column range has a job:

ColumnsNameUse
1 to 6Sequence areaLine numbers, so a dropped deck of cards could be put back in order
7Indicator area* for a comment line, - to continue a literal, D for a debugging line
8 to 11Area ADivision, section and paragraph names, FD, and level numbers 01 and 77
12 to 72Area BStatements and the rest of the data description
73 to 80Identification areaIgnored by the compiler; once used to label the card deck

COBOL 2002 introduced free format, where code may start in any column and lines may be long. This course uses free format because it is easier to type and read; cobc -free selects it, and so does the directive >>SOURCE FORMAT IS FREE on the first line. Comments in free format start with *> and run to the end of the line; current compilers accept *> in fixed-format code as well. When you work on an existing system you will follow its format, which is very often fixed, and your editor will show the column boundaries for you.

Reading an error

COBOL has more than a thousand reserved words (cobc --list-reserved prints GnuCOBOL's list), and sooner or later you will use one by accident. This program names a data item full:

cobol
IDENTIFICATION DIVISION.
PROGRAM-ID. countdown.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 full PIC 9 VALUE 3.
PROCEDURE DIVISION.
    DISPLAY full
    STOP RUN.

GnuCOBOL 3.2 rejects it:

text
main.cob:7: error: syntax error, unexpected FULL, expecting (
    5 | 01 full PIC 9 VALUE 3.
    6 | PROCEDURE DIVISION.
    7 >     DISPLAY full
    8 |     STOP RUN.
    9 | <EOF>

The message points at line 7, where full is used, not at line 5, where it was declared, and it says only that FULL was unexpected. When a COBOL error makes no sense, ask first whether one of your names is a reserved word. The professional habit that avoids the problem is to give names a prefix or a hyphenated form, such as ws-full or full-name, which can never be reserved.

Common misconceptionsH

Common misconceptions

Misconception

"COBOL is dead." It is not growing quickly, but it is maintained, standardised and running a great deal of critical work. IBM's Enterprise COBOL compiler receives regular releases, and the standard was revised in 2023. Being old and being dead are different things.

Misconception

"COBOL must be written in capital letters." Keywords and names are case-insensitive. Old code is in capitals because of old equipment and old habits. Text inside quotes is the exception: "Hello" and "HELLO" are different literals.

Misconception

"The full stop is just punctuation." In the procedure division a full stop ends a sentence, and it also closes every open IF and other conditional statement. A stray period in the middle of an IF silently changes what the program does. That is why modern style uses scope terminators such as END-IF and puts one period at the end of each paragraph.

Misconception

"COBOL code is unreadable because it is old." Much old COBOL is hard to read because it was changed by many hands over decades, often with GO TO statements and cryptic names. Well-written COBOL, with descriptive names and structured paragraphs, is among the most readable code there is. The language was designed so that people other than the author could check it.

Exercises

Exercises

Try each question before opening the hints. Every answer about program output was checked by compiling and running the program with GnuCOBOL 3.2.

1. First steps in COBOL

Warm-upHuman

In which division do you declare a data item such as 01 total PIC 9(5)V99.?

Choose one

2. First steps in COBOL

Warm-upHuman

Name the four divisions a COBOL program can have, one word each (for example identification).

Write elements separated by commas. Order and repeats don’t matter — just like in a set!

3. First steps in COBOL

Warm-upHuman

Which statement about COBOL's origins is accurate?

Choose one

4. First steps in COBOL

CoreComputational

What is the last line this program prints?

IDENTIFICATION DIVISION.
PROGRAM-ID. nines.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 n        PIC 99  VALUE 9.
01 i        PIC 99.
01 product  PIC 999.
PROCEDURE DIVISION.
    PERFORM VARYING i FROM 2 BY 2 UNTIL i > 6
        MULTIPLY n BY i GIVING product
        DISPLAY n " x " i " = " product
    END-PERFORM
    STOP RUN.

5. First steps in COBOL

CoreComputational

How many lines does this program print?

IDENTIFICATION DIVISION.
PROGRAM-ID. q.
PROCEDURE DIVISION.
    DISPLAY "Kia ora" " " "COBOL"
    DISPLAY "Line two"
    STOP RUN.
    DISPLAY "Line three".

6. First steps in COBOL

CoreHuman

In fixed-format COBOL, in which column do you put * to make a whole line a comment?

7. First steps in COBOL

CoreComputational

cobc reports syntax error, unexpected FULL on a line that says DISPLAY full, although 01 full PIC 9 VALUE 3. is declared. What is the best fix?

Choose one

Further reading and resources