COBOL, one tutorial at a time.
Every lesson here is a complete program written for GnuCOBOL, desk-checked against its guide and, for 14 of the 23 so far, compiled and run with GnuCOBOL 3.2, explained line by line, with the standard it follows named at the top. The language is sixty-plus years old and still runs the payroll, the ledger, and the claims system; this site teaches it the way those systems use it.
- Lessons
- 23, in five parts
- Listings written for
- GnuCOBOL 3.x
- Run status
- Compiled and run with GnuCOBOL 3.2: 14 of 23 lessons; the other 9 desk-checked
- Contents current to
Summary of changes
New and updated lessons, newest first.
- Updated 1.3 COBOL MOVE statement and the truncation rules that surprise everyone
- Updated 1.4 COMPUTE, ROUNDED, ON SIZE ERROR: COBOL arithmetic without overflow
- Updated 2.5 COBOL INSPECT REPLACING and CONVERTING, BEFORE and AFTER
- Updated 5.3 COBOL input validation: class tests, INSPECT and TEST-NUMVAL
- Updated 4.2 Free-format COBOL in GnuCOBOL: what changed from columns 8 and 12
Start here: what we checked ourselves
Six lessons that go furthest past restating the manual: findings from the GnuCOBOL 3.2 source and dialect files, comparisons with IBM’s dialect, and our own companion scripts. The full contents follow below.
4.4
COBOL JSON GENERATE and JSON PARSE: what GnuCOBOL 3.2 can do
A GnuCOBOL program that turns fixed-width records into JSON Lines with JSON GENERATE, the exact output it should write, and why JSON PARSE does nothing in 3.2.
15 min
4.5
GnuCOBOL -std=ibm and other dialects: what each one changes
What cobc -std=ibm actually switches in GnuCOBOL 3.2, read from its dialect files, and one program whose output changes when the dialect does.
11 min
5.3
COBOL input validation: class tests, INSPECT and TEST-NUMVAL
A GnuCOBOL edit program that checks every field of a fixed-width feed with CLASS, INSPECT, TEST-NUMVAL and TEST-DATE-YYYYMMDD, and logs every error with a code.
17 min
2.5
COBOL INSPECT REPLACING and CONVERTING, BEFORE and AFTER
A GnuCOBOL program that cleans an order export in place with INSPECT REPLACING, CONVERTING and BEFORE/AFTER, and the size and order rules behind them.
14 min
4.2
Free-format COBOL in GnuCOBOL: what changed from columns 8 and 12
The same COBOL program written twice, in fixed format and in free format, with the column rules that shaped it and where GnuCOBOL and IBM disagree.
12 min
3.4
COBOL copybooks: COPY, REPLACING, and one layout in three programs
One 39-byte copybook in three GnuCOBOL programs: plain COPY, REPLACING LEADING, and REPLACE, with where GnuCOBOL and IBM differ on search paths and nesting.
13 min
Contents
Lessons are numbered by part. Times are reading times.
1 Fundamentals
Divisions, PICTURE clauses, arithmetic, control flow: the language from the ground up.
5 lessons
- 1.1Your first COBOL program: all four divisions, DISPLAY, STOP RUN7 min
- 1.2COBOL PICTURE clause: 9, X, A, V, S and how a number is stored6 min
- 1.3COBOL MOVE statement and the truncation rules that surprise everyone11 min
- 1.4COMPUTE, ROUNDED, ON SIZE ERROR: COBOL arithmetic without overflow9 min
- 1.5PERFORM VARYING, PERFORM UNTIL, and inline PERFORM in COBOL6 min
2 Data and Tables
Group items, OCCURS, REDEFINES, editing, and the data description entries that make COBOL what it is.
5 lessons
- 2.1COBOL group items and elementary items: the record layout as a struct7 min
- 2.2COBOL edited pictures: money with Z, comma, period, CR and minus6 min
- 2.3COBOL OCCURS and subscripts: a table of monthly totals5 min
- 2.4COBOL REDEFINES example: one memory area, two views of a date5 min
- 2.5COBOL INSPECT REPLACING and CONVERTING, BEFORE and AFTER14 min
3 Files and Records
Sequential, indexed, and relative files; copybooks; the record-layout discipline mainframe shops live by.
4 lessons
4 Modern COBOL
GnuCOBOL, free format, calling C and Python, JSON, testing, and moving COBOL data in and out of everything else.
5 lessons
- 4.1Generating fixed-width test data with Python and reading it in COBOL7 min
- 4.2Free-format COBOL in GnuCOBOL: what changed from columns 8 and 1212 min
- 4.3Calling a C function from COBOL, and being called back7 min
- 4.4COBOL JSON GENERATE and JSON PARSE: what GnuCOBOL 3.2 can do15 min
- 4.5GnuCOBOL -std=ibm and other dialects: what each one changes11 min
5 Practice
Worked programs that solve a real task end to end: reports, batch jobs, validation, reconciliation.
4 lessons
About this textbook
Each lesson is one complete program. Not a fragment, not pseudocode: a listing with all four divisions, written for GnuCOBOL, the free compiler that implements the ISO standard plus most of the IBM and Micro Focus extensions people meet at work. The listing is explained line by line, the dialect and format (free or fixed) are stated at the top, and anything that is IBM-only or GnuCOBOL-only is called out where it appears rather than smoothed over.
Where a program reads or writes a file, the sample file and the copybook ship with the lesson, so nothing the program needs is missing. Where a lesson touches the modern world, Python does the other half: generating fixed-width test data, parsing a copybook, checking a report, calling a COBOL module from something newer.
Note: Lessons are published under the owner’s direction and desk-checked against the GnuCOBOL Programmer’s Guide; each one says what was run and what was not. How this site is made.