COBOL Cheat Sheet
Structural COBOL reference covering divisions, data definitions, procedure verbs, and basic file handling for legacy systems.
Program Structure
The four divisions of a COBOL program.
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-GREETING PIC X(20) VALUE "Hello, World!". PROCEDURE DIVISION. DISPLAY WS-GREETING. STOP RUN.
Data Division
Declaring records and fields with PICTURE clauses.
DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-EMPLOYEE. 05 WS-NAME PIC X(30). 05 WS-AGE PIC 9(3). 05 WS-SALARY PIC 9(7)V99. 01 WS-COUNTER PIC 9(4) VALUE ZERO. 01 WS-FLAG PIC X VALUE "N". 88 WS-DONE VALUE "Y".
Procedure Division Verbs
Common statements used in the Procedure Division.
- MOVE a TO b- Assigns/copies the value of a into b
- ADD a TO b- b = b + a
- COMPUTE x = a + b * c- Arithmetic expression assignment
- IF cond THEN ... ELSE ... END-IF- Conditional branching
- PERFORM para-name- Executes a named paragraph (like calling a routine)
- PERFORM UNTIL cond ... END-PERFORM- Loops until a condition becomes true
Common Clauses & Concepts
Frequently used declaration clauses.
- PIC 9(5)- Numeric picture clause, 5 digits
- PIC X(20)- Alphanumeric picture clause, 20 characters
- VALUE- Sets an initial literal value for a data item
- OCCURS n TIMES- Declares an array/table of n elements
- 01, 05, 88- Level numbers defining record hierarchy and condition names
- COPY member.- Includes a copybook of shared data or code
File Handling
Opening, reading, and closing a sequential file.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUST.DAT" ORGANIZATION IS LINE SEQUENTIAL. PROCEDURE DIVISION. OPEN INPUT CUSTOMER-FILE. READ CUSTOMER-FILE AT END DISPLAY "EOF reached". CLOSE CUSTOMER-FILE.
PERFORM Loops
Iteration with PERFORM UNTIL and VARYING.
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 10 ADD WS-I TO WS-TOTAL END-PERFORM. * inline vs out-of-line PERFORM PROCESS-RECORD UNTIL WS-EOF = 'Y'. PERFORM 100-INIT THRU 100-EXIT. PERFORM 5 TIMES DISPLAY 'Hello' END-PERFORM.
Tables & OCCURS
Arrays with OCCURS and indexed access.
01 WS-MONTHS. 05 WS-MONTH OCCURS 12 TIMES INDEXED BY WS-IDX PIC X(9). 01 WS-MATRIX. 05 WS-ROW OCCURS 3 TIMES. 10 WS-COL OCCURS 3 TIMES PIC 9(2). SET WS-IDX TO 1. MOVE 'JANUARY' TO WS-MONTH(WS-IDX). SEARCH WS-MONTH WHEN WS-MONTH(WS-IDX) = 'MARCH' DISPLAY 'Found' END-SEARCH.
STRING & UNSTRING
Concatenating and splitting text fields.
STRING WS-FIRST DELIMITED BY SPACE ' ' DELIMITED BY SIZE WS-LAST DELIMITED BY SPACE INTO WS-FULL-NAME END-STRING. UNSTRING WS-CSV DELIMITED BY ',' INTO WS-F1 WS-F2 WS-F3 END-UNSTRING. INSPECT WS-TEXT REPLACING ALL 'A' BY 'B'. INSPECT WS-TEXT TALLYING WS-CNT FOR ALL 'X'.
PICTURE Clauses
Common PIC symbols and editing characters.
- PIC 9(5)- unsigned numeric, 5 digits
- PIC S9(5)V99- signed number with implied decimal point (V)
- PIC X(20)- alphanumeric field, 20 characters
- PIC $$,$$9.99- numeric edited with floating currency and commas
- PIC 9(4) COMP- binary (COMP) storage for efficiency
- PIC S9(7) COMP-3- packed-decimal (COMP-3), 4 bytes for 7 digits
Conditions & 88-Levels
Condition names and evaluation constructs.
- 88 IS-VALID VALUE 'Y'.- condition-name that is true when the field equals 'Y'
- SET IS-VALID TO TRUE- assign the 88-level's VALUE to its parent field
- EVALUATE / WHEN / END-EVALUATE- case/switch-style multi-branch selection
- EVALUATE TRUE- decision-table form testing arbitrary conditions
- IF ... ELSE ... END-IF- scope-terminated conditional (avoid period pitfalls)
- NUMERIC / ALPHABETIC class test- IF WS-FLD IS NUMERIC validates content class
COBOL source columns matter in fixed format: Area A starts at column 8 (division/section/paragraph headers), Area B at column 12 (statements) — misaligned code fails to compile.