The four divisions
Every COBOL program has the same four sections in the same order. Once you know what belongs where, you can navigate any program in seconds.
ENVIRONMENT: the bridge to JCL
SELECT PAY-FILE ASSIGN TO INFILE says: the file this program calls PAY-FILE is whatever the JCL supplies on the DD statement named INFILE. That single line is the entire connection between a program and its data, and it is why you can point the same program at test data by changing one DD.
DATA DIVISION: three sections you will meet
| Section | Holds |
|---|---|
| FILE SECTION | Record layouts for files, one FD per file |
| WORKING-STORAGE SECTION | Variables the program owns; they persist for the whole run |
| LOCAL-STORAGE SECTION | Variables re-initialised on every call — used in recursive or CICS work |
| LINKAGE SECTION | Data passed in from outside: a PARM, or arguments from a calling program |
Level numbers build a hierarchy
01 CUSTOMER-RECORD. 05 CUST-ID PIC X(08). 05 CUST-NAME. 10 CUST-FIRST PIC X(15). 10 CUST-LAST PIC X(20). 05 CUST-STATUS PIC X(01). 88 CUST-ACTIVE VALUE 'A'. 88 CUST-CLOSED VALUE 'C'. 88 CUST-VALID VALUE 'A' 'C' 'S'. 05 CUST-BALANCE PIC S9(9)V99 COMP-3. * then in the PROCEDURE DIVISION: IF CUST-ACTIVE PERFORM 3000-PROCESS-ACTIVE END-IF
- 01 starts a record. 05, 10, 15 nest inside it. The numbers themselves are arbitrary; only their relative size matters.
- A level with no PIC is a group item — it is the concatenation of everything under it, and is always treated as character data.
- 88 levels are condition names: a readable name for a value test. Use them; they make logic far easier to read and to change.
- FILLER names space you do not care about. It is not a keyword you can reference.
PROCEDURE DIVISION: paragraphs and PERFORM
Logic is organised into paragraphs, usually numbered so the numbers imply call order. PERFORM 2000-PROCESS runs that paragraph and returns. PERFORM … UNTIL is the loop. PERFORM … VARYING is the counted loop. Modern style uses scope terminators — END-IF, END-READ, END-PERFORM — rather than full stops, because a misplaced full stop is one of the classic COBOL bugs.
Common mistakes
It terminates the conditional silently. The code compiles and does the wrong thing.
It persists for the run. In a called subprogram, values from the previous call are still there unless you initialise them or use LOCAL-STORAGE.
A group with no PIC is always character data, whatever its children are. Moving it to a numeric field is how S0C7s begin.
What you will see at work
- Paragraph numbering conventions differ by site but always encode structure. Learn yours and follow it.
- Copybooks are included with COPY statements and expanded at compile time; the expanded version appears in the compile listing.
- The LINKAGE SECTION plus PROCEDURE DIVISION USING is how a program receives a PARM from JCL — remember the two-byte length prefix.
Key terms
Check your understanding.
Take this lesson's quiz and save your progress. Free.