COBOL GnuCOBOL 3.2.0 Β·
β verified by execution on 2026-08-16
A COBOL program is structured exactly like a book. Where a book has chapters, COBOL has Divisions. Every single line of code you write belongs inside one of these divisions, and they must always appear in the exact same order.
There are exactly four divisions:
IDENTIFICATION DIVISION: Who wrote this program and what is it called?
ENVIRONMENT DIVISION: What hardware and files does it connect to?
DATA DIVISION: What variables and memory does it use?
PROCEDURE DIVISION: What actions does it perform?
Here is what a program looks like with all four divisions properly structured:
The very first line of any COBOL program must declare the IDENTIFICATION DIVISION. Its primary job is to provide a name for the program using the PROGRAM-ID paragraph.
What happens if we forget to provide the PROGRAM-ID? The compiler literally does not know how to name the compiled binary and will immediately throw a compile error.
cobolβ verified output
PROCEDURE DIVISION. DISPLAY 'Where am I now?'. STOP RUN.
This example raises an error (on purpose)
PROGRAM-ID header missing
Your output
PROGRAM-ID header missing
But what if we skip the ENVIRONMENT and DATA divisions? See if you can predict what the compiler will do:
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
In the mainframe era, COBOL programs were deeply tied to the physical hardware they ran on. The ENVIRONMENT DIVISION was used to specify whether the code was compiled on an IBM System/360 or an NCR machine, and it mapped physical tape drives to logical files.
Today, on modern systems, this division is mostly used for file routing.
Unlike languages such as Python or JavaScript where you can create a variable right before you use it, COBOL is completely rigid. All variables must be declared upfront inside the DATA DIVISION.
The memory is typically laid out inside a specific section called the WORKING-STORAGE SECTION.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. DATA-DIV. DATA DIVISION. WORKING-STORAGE SECTION. 01 MY-VAR PIC X(5) VALUE 'Data!'. PROCEDURE DIVISION. DISPLAY MY-VAR. STOP RUN.
Output
Data!
Your output
Order Matters
You cannot shuffle the chapters of a book and expect it to make sense, and the same goes for COBOL. The compiler expects the divisions in a strict, unyielding order: IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE.
Try compiling a program where we placed the DATA DIVISION after the PROCEDURE DIVISION:
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. WRONG-ORDER. PROCEDURE DIVISION. DISPLAY 'This will fail.' STOP RUN. DATA DIVISION. WORKING-STORAGE SECTION. 01 MSG PIC X(10) VALUE 'Hi'.
This example raises an error (on purpose)
syntax error
Your output
syntax error
The compiler hits the DATA DIVISION header while it is currently parsing instructions and throws a syntax error.
Optional Divisions
While the structure is rigid, you do not have to write divisions you do not need. If your program doesnβt map any physical files and doesnβt use any variables, you can simply omit the ENVIRONMENT and DATA divisions completely.
cobolβ verified output
PROGRAM-ID. MISSING-IDENT. PROCEDURE DIVISION. DISPLAY 'Where am I?'. STOP RUN.
Output
Where am I?
Your output
Only the PROGRAM-ID and the PROCEDURE DIVISION are strictly mandatory for a minimal program to compile and run.
Check yourself
What is the absolute minimum requirement to create a valid, runnable COBOL program?
Reveal answer
Only the IDENTIFICATION DIVISION and the PROCEDURE DIVISION are required. β Only IDENTIFICATION (to provide the PROGRAM-ID) and PROCEDURE (to execute code) are strictly required for a minimal program. The ENVIRONMENT and DATA divisions are optional.
Where must you declare all variables in a COBOL program?
Reveal answer
In the DATA DIVISION under the WORKING-STORAGE SECTION. β All variables must be declared in the DATA DIVISION. You cannot declare them inline inside the PROCEDURE DIVISION.
What happens if you place the DATA DIVISION before the ENVIRONMENT DIVISION?
Reveal answer
The program will fail to compile with a syntax error. β Divisions must appear in strict order: IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE. Changing this order causes a compile error.
Challenges
Challenge 1 +20 XP
This COBOL program has its divisions out of order and will not compile. Fix the order so it runs successfully.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "Correct order! \n"
Need a hint? (β25% XP)
The order must be IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE.
Find the bug! This program is trying to use a variable, but something is missing.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it β the tests below decide when you are right.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "Hello COBOL \n"
Need a hint? (β25% XP)
Where must variables be declared? You need a specific division for data.