COBOL Cheat Sheet
Last updated
Program structure
A program is made of divisions, always in the same order. Each header ends with a period.
| Part | Code |
|---|---|
| Hello World in COBOL, a complete program | IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello World!".
STOP RUN. |
| All four divisions, in order | IDENTIFICATION DIVISION.
PROGRAM-ID. GREET.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(10) VALUE "Ada".
PROCEDURE DIVISION.
DISPLAY "Hello, " WS-NAME.
STOP RUN. |
| Name the program (required) | IDENTIFICATION DIVISION. → PROGRAM-ID. HELLO. |
| Describe the files the program uses | ENVIRONMENT DIVISION. |
| Describe the data | DATA DIVISION. → WORKING-STORAGE SECTION. |
| The statements that run, top to bottom | PROCEDURE DIVISION. |
| End the program | STOP RUN. |
Fixed-format columns and comments
In fixed format each column range has a job, and the compiler reads nothing after column 72.
| Columns | Code |
|---|---|
| Where each part starts (the first line counts the columns) | 123456789012345
* A comment line
MAIN-PARA.
DISPLAY "Hi". |
| Columns 1 to 6: the sequence area, empty or a line number | 000100 DISPLAY "Hi". |
Column 7: the indicator, where * makes the whole line a comment | * This line is a comment |
| Columns 8 to 11: Area A, for divisions, sections, paragraph names, 01 and 77 | PROCEDURE DIVISION. |
| Columns 12 to 72: Area B, for statements and levels 05 and deeper | DISPLAY "Hi". |
| Columns 73 to 80: ignored | * Keep every line within column 72 |
| A comment after the code, anywhere on the line | DISPLAY "Hi". *> greet the user |
PIC clauses and VALUE
Fields are declared in the WORKING-STORAGE SECTION: a level number, a name, a PIC clause and an optional VALUE. The last column is what DISPLAY prints; brackets mark where a text field starts and ends.
| Field | Declaration | DISPLAY prints |
|---|---|---|
| Text of 10 characters (X) | 01 WS-NAME PIC X(10) VALUE "Ada". | [Ada ] |
| Whole number of 3 digits (9) | 01 WS-QTY PIC 9(3) VALUE 42. | 042 |
| Implied decimal point (V), never printed | 01 WS-PRICE PIC 9(3)V99 VALUE 12.5. | 01250 |
| Signed number (S), sign printed last | 01 WS-BAL PIC S9(3) VALUE -42. | 042- |
| Counter that starts at zero | 01 WS-COUNT PIC 9(4) VALUE ZERO. | 0000 |
| Text that starts blank | 01 WS-LINE PIC X(5) VALUE SPACES. | [ ] |
| Standalone item (level 77) | 77 WS-TOTAL PIC 9(5) VALUE 0. | 00000 |
| Packed decimal, two digits a byte | 01 WS-AMT PIC S9(7)V99 COMP-3 VALUE 1234.5. | +000123450 |
| Binary number | 01 WS-N PIC S9(4) COMP VALUE 7. | +00007 |
MOVE rules: truncation and padding
With WS-CODE PIC X(5), WS-NUM PIC 9(3) and WS-AMT PIC 9(3)V99. COBOL never warns when a MOVE cuts a value.
| Rule | Code | Field after |
|---|---|---|
| Text starts on the left, padded with spaces | MOVE "AB" TO WS-CODE. | [AB ] |
| Text that is too long loses characters on the right | MOVE "ABCDEFG" TO WS-CODE. | [ABCDE] |
| A number lines up on the right, padded with zeros | MOVE 7 TO WS-NUM. | 007 |
| A number that is too big loses digits on the left | MOVE 12345 TO WS-NUM. | 345 |
| Decimals line up on the point | MOVE 4.5 TO WS-AMT. | 00450 |
| Extra decimals are dropped, not rounded | MOVE 1.999 TO WS-AMT. | 00199 |
| A number moved into text keeps its zeros | MOVE 7 TO WS-NUM. → MOVE WS-NUM TO WS-CODE. | [007 ] |
| Clear a field | MOVE SPACES TO WS-CODE. / MOVE ZERO TO WS-NUM. | [ ] / 000 |
DISPLAY and ACCEPT
DISPLAY prints its items side by side, then ends the line. ACCEPT reads the next input line into a field.
| Operation | Code | Result |
|---|---|---|
| Print text | DISPLAY "Hello World!". | Hello World! |
| Print text and fields together | DISPLAY "Qty: " WS-QTY. | Qty: 042 |
| Print a text field without its trailing spaces | DISPLAY "[" FUNCTION TRIM(WS-NAME) "]". | [Ada] |
| Read a line into a text field | ACCEPT WS-NAME. | Grace → [Grace ] |
| Read a number | ACCEPT WS-QTY. | 7 → 007 |
| A typed decimal point is kept | ACCEPT WS-PRICE. | 12.50 → 01250 |
| A line that is not a number gives zero | ACCEPT WS-QTY. | abc → 000 |
Level numbers and group items
01 starts a record. Higher levels belong to the nearest lower level above them. An item with no PIC is a group: it stands for all the bytes under it and is treated as text.
| Element | Code |
|---|---|
| A record with a group inside | 01 WS-CUSTOMER.
05 WS-NAME.
10 WS-FIRST PIC X(10).
10 WS-LAST PIC X(10).
05 WS-CITY PIC X(12). |
| Bytes with no name, such as fixed text in a line | 05 FILLER PIC X(3) VALUE " | ". |
| Name a field by its record | MOVE "Ada" TO WS-FIRST OF WS-CUSTOMER. |
| Copy a whole group, byte for byte | MOVE WS-CUSTOMER TO WS-BACKUP. |
| Text items to spaces, numeric items to zeros | INITIALIZE WS-CUSTOMER. |
| The same bytes with another layout | 01 WS-DATE PIC 9(8) VALUE 20261125.
01 WS-DATE-PARTS REDEFINES WS-DATE.
05 WS-YEAR PIC 9(4).
05 WS-MONTH PIC 99.
05 WS-DAY PIC 99. |
Edited pictures for printing numbers
MOVE a number into an edited field, then DISPLAY that field. Negative values need a signed source field (PIC S9).
| Use | Picture | Value | Prints |
|---|---|---|---|
| Leading zeros as spaces | ZZ9 | 7 | [ 7] |
| With a decimal point | ZZ9.99 | 5.5 | [ 5.50] |
| Thousands comma | ZZZ,ZZ9 | 1234 | [ 1,234] |
| Floating dollar sign | $$$,$$9.99 | 1234.5 | [ $1,234.50] |
| Fixed dollar sign | $ZZ9.99 | 5 | [$ 5.00] |
| Asterisks instead of leading zeros | ***9.99 | 42.5 | [**42.50] |
| Minus sign next to the digits | ---9 | -42 | [ -42] |
| CR when the value is negative | ZZ9CR | -42 | [ 42CR] |
| Slashes in a date | 99/99/9999 | 23112026 | [23/11/2026] |
Arithmetic: ADD, SUBTRACT, MULTIPLY, DIVIDE, COMPUTE
Each row starts with WS-A = 10 and WS-B = 3, all fields PIC 9(3) and WS-TAX PIC 9(3)V99. A result that does not fit loses digits on the left, with no warning.
| Operation | Code | Result |
|---|---|---|
| Add to a field | ADD 5 TO WS-A. | WS-A = 015 |
| Subtract from a field | SUBTRACT 4 FROM WS-A. | WS-A = 006 |
| Keep the operands, store the result (GIVING) | ADD WS-A WS-B GIVING WS-C. | WS-C = 013 |
| Subtract with GIVING | SUBTRACT WS-B FROM WS-A GIVING WS-C. | WS-C = 007 |
| Multiply | MULTIPLY WS-A BY WS-B GIVING WS-C. | WS-C = 030 |
| Divide with a remainder | DIVIDE WS-A BY WS-B GIVING WS-C REMAINDER WS-R. | WS-C = 003 / WS-R = 001 |
| Divide INTO a field | DIVIDE 2 INTO WS-A. | WS-A = 005 |
| A formula | COMPUTE WS-C = (WS-A + WS-B) * 2. | WS-C = 026 |
| A power | COMPUTE WS-C = WS-B ** 2. | WS-C = 009 |
| Extra decimals are dropped | COMPUTE WS-TAX = 19.99 * 0.07. | WS-TAX = 00139 |
| Round half away from zero (ROUNDED) | COMPUTE WS-TAX ROUNDED = 19.99 * 0.07. | WS-TAX = 00140 |
| Catch a result that does not fit | ADD 995 TO WS-A
ON SIZE ERROR DISPLAY "Too big"
NOT ON SIZE ERROR DISPLAY "Added"
END-ADD. | Too big / WS-A = 010 |
| Catch a division by zero | DIVIDE WS-A BY WS-ZERO GIVING WS-C
ON SIZE ERROR DISPLAY "Cannot divide by zero"
END-DIVIDE. | Cannot divide by zero |
IF and conditions
A period ends every open IF, so write no periods inside an IF and close it with END-IF.
| Operation | Code |
|---|---|
| IF and ELSE | IF WS-QTY > 10
DISPLAY "Large order"
ELSE
DISPLAY "Small order"
END-IF. |
| Compare | = / NOT = / < / > / <= / >= |
| Compare in words | IF WS-QTY IS GREATER THAN 10 |
| Both, either, the opposite | IF WS-AGE >= 18 AND WS-AGE < 65 / OR / NOT |
| Several values for one subject | IF WS-NUM = 1 OR 2 OR 3 |
| Only digits? | IF WS-INPUT IS NUMERIC |
| Only letters and spaces? | IF WS-NAME IS ALPHABETIC |
| An IF inside an IF needs its own END-IF | IF WS-A > 0
IF WS-B > 0
DISPLAY "Both positive"
END-IF
END-IF. |
Condition names (88 levels) and EVALUATE
An 88 level names a value of the field above it. EVALUATE runs the first WHEN that matches.
| Operation | Code |
|---|---|
| Name values of a field | 01 WS-STATUS PIC X VALUE "A".
88 IS-ACTIVE VALUE "A".
88 IS-CLOSED VALUE "C" "X". |
| Name a range | 88 IS-TEEN VALUE 13 THRU 19. |
| Test a condition name | IF IS-ACTIVE DISPLAY "Active" END-IF. |
| Make it true (moves its first value) | SET IS-CLOSED TO TRUE. |
| Choose by value | EVALUATE WS-DAY-NUM
WHEN 1 THRU 5
DISPLAY "Weekday"
WHEN 6
WHEN 7
DISPLAY "Weekend"
WHEN OTHER
DISPLAY "Not a day"
END-EVALUATE. |
| Choose by condition | EVALUATE TRUE
WHEN WS-SCORE >= 90
DISPLAY "A"
WHEN WS-SCORE >= 80
DISPLAY "B"
WHEN OTHER
DISPLAY "C"
END-EVALUATE. |
| Two subjects at once (ANY matches every value) | EVALUATE WS-TYPE ALSO WS-VIP
WHEN "D" ALSO "Y"
DISPLAY "VIP deposit"
WHEN "D" ALSO ANY
DISPLAY "Deposit"
WHEN OTHER
DISPLAY "Other"
END-EVALUATE. |
PERFORM and loops
A paragraph is a name in Area A and the statements under it. PERFORM runs it and comes back.
| Loop | Code |
|---|---|
| Run a paragraph | MAIN-PARA.
PERFORM PRINT-LINE.
STOP RUN.
PRINT-LINE.
DISPLAY "A line". |
| A fixed number of times | PERFORM PRINT-LINE 3 TIMES. |
| Until a condition is true (tested first) | PERFORM READ-NEXT UNTIL END-OF-FILE. |
| Run at least once (tested after) | PERFORM ASK-AGAIN WITH TEST AFTER UNTIL WS-ANSWER = "Y". |
| Count with a counter | PERFORM SHOW-ROW VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5. |
| Inline loop, no paragraph | PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 3
DISPLAY "Row " WS-I
END-PERFORM. |
| Inline, a fixed number of times | PERFORM 3 TIMES
DISPLAY "Hi"
END-PERFORM. |
Tables: OCCURS and SEARCH
Subscripts count from 1. A loop counter must have room for the value that ends the loop.
| Operation | Code |
|---|---|
| A table of 12 elements | 01 WS-MONTHS.
05 WS-MONTH PIC X(3) OCCURS 12 TIMES. |
| Use one element | MOVE "JAN" TO WS-MONTH(1). |
| Visit every element | PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 12
DISPLAY WS-MONTH(WS-I)
END-PERFORM. |
| Rows and columns | 01 WS-HALL.
05 WS-ROW OCCURS 3 TIMES.
10 WS-SEAT PIC X OCCURS 4 TIMES. → MOVE "X" TO WS-SEAT(2, 3). |
| A table filled from values | 01 WS-DAY-NAMES.
05 FILLER PIC X(3) VALUE "MON".
05 FILLER PIC X(3) VALUE "TUE".
05 FILLER PIC X(3) VALUE "WED".
01 WS-DAYS REDEFINES WS-DAY-NAMES.
05 WS-DAY-NAME PIC X(3) OCCURS 3 TIMES. |
| A table with an index | 01 WS-FRUITS.
05 WS-FRUIT PIC X(6) OCCURS 5 TIMES
INDEXED BY WS-IX. |
| Move the index | SET WS-IX TO 1. / SET WS-IX UP BY 1. |
| Search from the start (serial) | SET WS-IX TO 1.
SEARCH WS-FRUIT
AT END DISPLAY "Not found"
WHEN WS-FRUIT(WS-IX) = "PEAR"
DISPLAY "Found"
END-SEARCH. |
| A table sorted by a key | 01 WS-CODES.
05 WS-ENTRY OCCURS 3 TIMES
ASCENDING KEY IS WS-KEY
INDEXED BY WS-CX.
10 WS-KEY PIC XX.
10 WS-DESC PIC X(10). |
| Binary search of a sorted table | SEARCH ALL WS-ENTRY
AT END DISPLAY "Unknown"
WHEN WS-KEY(WS-CX) = "DE"
DISPLAY WS-DESC(WS-CX)
END-SEARCH. |
Strings: reference modification, STRING, UNSTRING, INSPECT
With WS-NAME holding "Ada Lovelace".
| Operation | Code | Result |
|---|---|---|
| Part of a field (start:length) | DISPLAY WS-NAME(1:3). | Ada |
| From a position to the end | DISPLAY WS-NAME(5:). | Lovelace |
| Change one character | MOVE "a" TO WS-NAME(1:1). | ada Lovelace |
| Join pieces | MOVE SPACES TO WS-FULL.
STRING WS-FIRST DELIMITED BY SPACE
" " DELIMITED BY SIZE
WS-LAST DELIMITED BY SPACE
INTO WS-FULL
END-STRING. | Ada Lovelace |
| Split a line | UNSTRING WS-CSV DELIMITED BY ","
INTO WS-F1 WS-F2 WS-F3
TALLYING IN WS-PARTS
END-UNSTRING. | "Oslo,Rome,Lima" → WS-PARTS = 3 |
| Count characters | MOVE 0 TO WS-COUNT.
INSPECT WS-TEXT TALLYING WS-COUNT FOR ALL "A". | "BANANA" → WS-COUNT = 3 |
| Replace characters | INSPECT WS-DATE-TXT REPLACING ALL "." BY "/". | "25.11.2026" → "25/11/2026" |
| Map one set of characters to another | INSPECT WS-TEXT CONVERTING "abc" TO "ABC". | "cab" → "CAB" |
Intrinsic functions
FUNCTION NAME(arguments) works where a value is expected. COMPUTE a numeric result into a field before you print it.
| Function | Code | Result |
|---|---|---|
| Upper case | DISPLAY FUNCTION UPPER-CASE("cobol"). | COBOL |
| Lower case | DISPLAY FUNCTION LOWER-CASE("COBOL"). | cobol |
| Remove leading and trailing spaces | DISPLAY "[" FUNCTION TRIM(WS-NAME) "]". | [Ada] |
| Length of a field | COMPUTE WS-N = FUNCTION LENGTH(WS-NAME). | WS-N = 010 |
| Text to a number | COMPUTE WS-PRICE = FUNCTION NUMVAL("12.50"). | WS-PRICE = 01250 |
| Is it a valid number? (0 when it is) | COMPUTE WS-N = FUNCTION TEST-NUMVAL("12a"). | WS-N = 003 |
| Largest value | COMPUTE WS-N = FUNCTION MAX(3, 9, 4). | WS-N = 009 |
| Remainder of a division | COMPUTE WS-N = FUNCTION MOD(17, 5). | WS-N = 002 |
| Reverse text | DISPLAY FUNCTION REVERSE("ABC"). | CBA |
| Today's date | DISPLAY FUNCTION CURRENT-DATE(1:8). | YYYYMMDD |
Sequential files
SELECT ties a file to a data set, FD describes its record. ASSIGN TO CUSTIN reads the data set named CUSTIN.
| Step | Code |
|---|---|
| Name the file and its data set (SELECT) | ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUST-FILE ASSIGN TO CUSTIN
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-FS. |
| Describe its record (FD) | DATA DIVISION.
FILE SECTION.
FD CUST-FILE.
01 CUST-REC.
05 CUST-NAME PIC X(10).
05 CUST-BAL PIC 9(5). |
| A status field and an end-of-file flag | 01 WS-FS PIC XX.
01 WS-EOF PIC X VALUE "N".
88 END-OF-FILE VALUE "Y". |
| Open for reading, writing or adding at the end | OPEN INPUT CUST-FILE. / OPEN OUTPUT RPT-FILE. / OPEN EXTEND RPT-FILE. |
| The read loop | OPEN INPUT CUST-FILE.
PERFORM UNTIL END-OF-FILE
READ CUST-FILE
AT END SET END-OF-FILE TO TRUE
NOT AT END DISPLAY CUST-NAME CUST-BAL
END-READ
END-PERFORM.
CLOSE CUST-FILE. |
| Write a record (a WRITE names the record, a READ the file) | WRITE RPT-LINE. |
| Write from a working-storage line | WRITE RPT-LINE FROM WS-HEADING AFTER ADVANCING 2 LINES. |
| Read into a working-storage record | READ CUST-FILE INTO WS-CUST
AT END SET END-OF-FILE TO TRUE
END-READ. |
| Close files | CLOSE CUST-FILE RPT-FILE. |
FILE STATUS codes
With FILE STATUS IS WS-FS, the field gets a code after every OPEN, READ, WRITE and CLOSE.
| Code | Meaning |
|---|---|
00 | The statement worked |
10 | End of file: no record was left to READ |
35 | OPEN INPUT found no data set |
47 | READ of a file that is not open for input |
JCL job skeleton
On a mainframe a batch job runs programs. Every JCL line starts with //, and names are 1 to 8 characters.
| Statement | Code |
|---|---|
| A job with one step | //PAYJOB JOB (ACCT),'CODDY',CLASS=A,MSGCLASS=X
//PAY EXEC PGM=PAYROLL
//CUSTIN DD DSN=CODDY.CUST.MASTER,DISP=SHR
//RPTOUT DD SYSOUT=*
//SYSOUT DD SYSOUT=* |
| Start the job (JOB) | //PAYJOB JOB (ACCT),'CODDY',CLASS=A,MSGCLASS=X |
| A step runs one program (EXEC) | //PAY EXEC PGM=PAYROLL |
| Read an existing data set (DD) | //CUSTIN DD DSN=CODDY.CUST.MASTER,DISP=SHR |
| Create a data set, delete it if the step fails | //BIGOUT DD DSN=CODDY.CUST.BIG,DISP=(NEW,CATLG,DELETE) |
| Print the output with the job (DISPLAY goes to SYSOUT) | //SYSOUT DD SYSOUT=* |
| Instream data that ACCEPT reads | //SYSIN DD *
00125
00300
/* |
| Run a step only if every earlier return code is 4 or less | //POST EXEC PGM=POSTTXN,COND=(4,LT) |
| Run steps when a test is true | // IF (CHECK.RC <= 4) THEN
//POST EXEC PGM=POSTTXN
//SYSOUT DD SYSOUT=*
// ENDIF |
| A comment line | //* Nightly batch |
Compile and run with GnuCOBOL
GnuCOBOL's compiler is cobc. Coddy's COBOL playground runs these for you.
| Task | Command |
|---|---|
| Compile an executable in IBM's dialect, fixed format | cobc -x -std=ibm -fixed hello.cob |
| Run it | ./hello |
| Run it with input for ACCEPT | ./hello < input.txt |
| Stop on bad numeric data and bad subscripts | cobc -x -std=ibm -fixed -debug hello.cob |
| Check the syntax only | cobc -fsyntax-only -std=ibm -fixed hello.cob |
| Free format, no column rules | cobc -x -free hello.cob |
The COBOL syntax you look up most, on one page, with a code example in every row: the four divisions, fixed-format columns, PIC clauses, MOVE, arithmetic, IF and EVALUATE, PERFORM loops, tables, text, sequential files and a JCL job. The tables follow the order of Coddy's COBOL course, from a first program to batch jobs.
Every COBOL example was compiled and run with GnuCOBOL 3.2 as cobc -x -std=ibm -fixed, which reads IBM's COBOL dialect in fixed format, the compiler of Coddy's COBOL playground and lessons. Whole programs keep their real columns. Shorter examples drop their leading spaces: in a program, levels 01 and 77 and paragraph names start in Area A (column 8), and statements start in Area B (column 12).
COBOL cheat sheet FAQ
What is the basic syntax of a COBOL program?
DISPLAY "Hello World!". and MOVE 7 TO WS-NUM. Only the IDENTIFICATION DIVISION is required.Why do COBOL lines start in column 8 or 12?
*, Area A (columns 8 to 11) holds divisions, sections, paragraph names and levels 01 and 77, and Area B (columns 12 to 72) holds statements. The compiler ignores columns 73 to 80.What does PIC 9(3)V99 mean?
01250. To print 12.50, MOVE it into an edited picture such as ZZ9.99.Why should a counter have VALUE ZERO?
-debug, as on Coddy) and gives a wrong result without them. On a mainframe this is the S0C7 ABEND. Give every counter and total VALUE ZERO.What happens when COBOL divides by zero?
With ON SIZE ERROR, the error branch runs and the result field keeps its old value. Without it, GnuCOBOL goes on and the field also keeps its old value, while an IBM mainframe ends the program with an S0CB ABEND. Write ON SIZE ERROR on every DIVIDE whose divisor can be zero.
Which COBOL does this cheat sheet cover?
-std=ibm. The JCL table is the shape of a z/OS batch job; JCL runs on a mainframe and in Coddy's COBOL course, not with cobc.Can I run these COBOL examples online?
Yes. The COBOL playground on Coddy compiles your program with GnuCOBOL and runs it, with a Stdin box for ACCEPT and rulers at columns 7, 8, 12 and 72. Paste a row from this cheat sheet into a program and press Run, with nothing to install.
Is this COBOL cheat sheet free?
Yes. It is free to read, copy and print, with no sign-up.