Menu
CoddyTech

COBOL Cheat Sheet

Last updated

Program structure

A program is made of divisions, always in the same order. Each header ends with a period.

PartCode
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 usesENVIRONMENT DIVISION.
Describe the dataDATA DIVISION. → WORKING-STORAGE SECTION.
The statements that run, top to bottomPROCEDURE DIVISION.
End the programSTOP RUN.

Fixed-format columns and comments

In fixed format each column range has a job, and the compiler reads nothing after column 72.

ColumnsCode
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 number000100 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 lineDISPLAY "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.

FieldDeclarationDISPLAY 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 printed01 WS-PRICE PIC 9(3)V99 VALUE 12.5.01250
Signed number (S), sign printed last01 WS-BAL PIC S9(3) VALUE -42.042-
Counter that starts at zero01 WS-COUNT PIC 9(4) VALUE ZERO.0000
Text that starts blank01 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 byte01 WS-AMT PIC S9(7)V99 COMP-3 VALUE 1234.5.+000123450
Binary number01 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.

RuleCodeField after
Text starts on the left, padded with spacesMOVE "AB" TO WS-CODE.[AB ]
Text that is too long loses characters on the rightMOVE "ABCDEFG" TO WS-CODE.[ABCDE]
A number lines up on the right, padded with zerosMOVE 7 TO WS-NUM.007
A number that is too big loses digits on the leftMOVE 12345 TO WS-NUM.345
Decimals line up on the pointMOVE 4.5 TO WS-AMT.00450
Extra decimals are dropped, not roundedMOVE 1.999 TO WS-AMT.00199
A number moved into text keeps its zerosMOVE 7 TO WS-NUM. → MOVE WS-NUM TO WS-CODE.[007 ]
Clear a fieldMOVE 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.

OperationCodeResult
Print textDISPLAY "Hello World!".Hello World!
Print text and fields togetherDISPLAY "Qty: " WS-QTY.Qty: 042
Print a text field without its trailing spacesDISPLAY "[" FUNCTION TRIM(WS-NAME) "]".[Ada]
Read a line into a text fieldACCEPT WS-NAME.Grace → [Grace ]
Read a numberACCEPT WS-QTY.7 → 007
A typed decimal point is keptACCEPT WS-PRICE.12.50 → 01250
A line that is not a number gives zeroACCEPT 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.

ElementCode
A record with a group inside01 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 line05 FILLER PIC X(3) VALUE " | ".
Name a field by its recordMOVE "Ada" TO WS-FIRST OF WS-CUSTOMER.
Copy a whole group, byte for byteMOVE WS-CUSTOMER TO WS-BACKUP.
Text items to spaces, numeric items to zerosINITIALIZE WS-CUSTOMER.
The same bytes with another layout01 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).

UsePictureValuePrints
Leading zeros as spacesZZ97[ 7]
With a decimal pointZZ9.995.5[ 5.50]
Thousands commaZZZ,ZZ91234[ 1,234]
Floating dollar sign$$$,$$9.991234.5[ $1,234.50]
Fixed dollar sign$ZZ9.995[$ 5.00]
Asterisks instead of leading zeros***9.9942.5[**42.50]
Minus sign next to the digits---9-42[ -42]
CR when the value is negativeZZ9CR-42[ 42CR]
Slashes in a date99/99/999923112026[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.

OperationCodeResult
Add to a fieldADD 5 TO WS-A.WS-A = 015
Subtract from a fieldSUBTRACT 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 GIVINGSUBTRACT WS-B FROM WS-A GIVING WS-C.WS-C = 007
MultiplyMULTIPLY WS-A BY WS-B GIVING WS-C.WS-C = 030
Divide with a remainderDIVIDE WS-A BY WS-B GIVING WS-C REMAINDER WS-R.WS-C = 003 / WS-R = 001
Divide INTO a fieldDIVIDE 2 INTO WS-A.WS-A = 005
A formulaCOMPUTE WS-C = (WS-A + WS-B) * 2.WS-C = 026
A powerCOMPUTE WS-C = WS-B ** 2.WS-C = 009
Extra decimals are droppedCOMPUTE 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 fitADD 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 zeroDIVIDE 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.

OperationCode
IF and ELSEIF WS-QTY > 10 DISPLAY "Large order" ELSE DISPLAY "Small order" END-IF.
Compare= / NOT = / < / > / <= / >=
Compare in wordsIF WS-QTY IS GREATER THAN 10
Both, either, the oppositeIF WS-AGE >= 18 AND WS-AGE < 65 / OR / NOT
Several values for one subjectIF 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-IFIF 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.

OperationCode
Name values of a field01 WS-STATUS PIC X VALUE "A". 88 IS-ACTIVE VALUE "A". 88 IS-CLOSED VALUE "C" "X".
Name a range88 IS-TEEN VALUE 13 THRU 19.
Test a condition nameIF IS-ACTIVE DISPLAY "Active" END-IF.
Make it true (moves its first value)SET IS-CLOSED TO TRUE.
Choose by valueEVALUATE 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 conditionEVALUATE 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.

LoopCode
Run a paragraphMAIN-PARA. PERFORM PRINT-LINE. STOP RUN. PRINT-LINE. DISPLAY "A line".
A fixed number of timesPERFORM 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 counterPERFORM SHOW-ROW VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5.
Inline loop, no paragraphPERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 3 DISPLAY "Row " WS-I END-PERFORM.
Inline, a fixed number of timesPERFORM 3 TIMES DISPLAY "Hi" END-PERFORM.

Subscripts count from 1. A loop counter must have room for the value that ends the loop.

OperationCode
A table of 12 elements01 WS-MONTHS. 05 WS-MONTH PIC X(3) OCCURS 12 TIMES.
Use one elementMOVE "JAN" TO WS-MONTH(1).
Visit every elementPERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 12 DISPLAY WS-MONTH(WS-I) END-PERFORM.
Rows and columns01 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 values01 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 index01 WS-FRUITS. 05 WS-FRUIT PIC X(6) OCCURS 5 TIMES INDEXED BY WS-IX.
Move the indexSET 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 key01 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 tableSEARCH 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".

OperationCodeResult
Part of a field (start:length)DISPLAY WS-NAME(1:3).Ada
From a position to the endDISPLAY WS-NAME(5:).Lovelace
Change one characterMOVE "a" TO WS-NAME(1:1).ada Lovelace
Join piecesMOVE 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 lineUNSTRING WS-CSV DELIMITED BY "," INTO WS-F1 WS-F2 WS-F3 TALLYING IN WS-PARTS END-UNSTRING."Oslo,Rome,Lima" → WS-PARTS = 3
Count charactersMOVE 0 TO WS-COUNT. INSPECT WS-TEXT TALLYING WS-COUNT FOR ALL "A"."BANANA" → WS-COUNT = 3
Replace charactersINSPECT WS-DATE-TXT REPLACING ALL "." BY "/"."25.11.2026" → "25/11/2026"
Map one set of characters to anotherINSPECT 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.

FunctionCodeResult
Upper caseDISPLAY FUNCTION UPPER-CASE("cobol").COBOL
Lower caseDISPLAY FUNCTION LOWER-CASE("COBOL").cobol
Remove leading and trailing spacesDISPLAY "[" FUNCTION TRIM(WS-NAME) "]".[Ada]
Length of a fieldCOMPUTE WS-N = FUNCTION LENGTH(WS-NAME).WS-N = 010
Text to a numberCOMPUTE 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 valueCOMPUTE WS-N = FUNCTION MAX(3, 9, 4).WS-N = 009
Remainder of a divisionCOMPUTE WS-N = FUNCTION MOD(17, 5).WS-N = 002
Reverse textDISPLAY FUNCTION REVERSE("ABC").CBA
Today's dateDISPLAY 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.

StepCode
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 flag01 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 endOPEN INPUT CUST-FILE. / OPEN OUTPUT RPT-FILE. / OPEN EXTEND RPT-FILE.
The read loopOPEN 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 lineWRITE RPT-LINE FROM WS-HEADING AFTER ADVANCING 2 LINES.
Read into a working-storage recordREAD CUST-FILE INTO WS-CUST AT END SET END-OF-FILE TO TRUE END-READ.
Close filesCLOSE 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.

CodeMeaning
00The statement worked
10End of file: no record was left to READ
35OPEN INPUT found no data set
47READ 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.

StatementCode
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.

TaskCommand
Compile an executable in IBM's dialect, fixed formatcobc -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 subscriptscobc -x -std=ibm -fixed -debug hello.cob
Check the syntax onlycobc -fsyntax-only -std=ibm -fixed hello.cob
Free format, no column rulescobc -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?
A program is four divisions in a fixed order: IDENTIFICATION names it, ENVIRONMENT describes its files, DATA declares its fields and PROCEDURE holds the statements that run. Statements are English words that end with a period, such as 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?
Fixed-format COBOL comes from punched cards. Columns 1 to 6 are for line numbers, column 7 marks a comment with *, 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?
A number of five digits: three before an implied decimal point and two after it. The V marks where the point is, but no point is stored or printed, so 12.5 displays as 01250. To print 12.50, MOVE it into an edited picture such as ZZ9.99.
Why should a counter have VALUE ZERO?
A numeric field with no VALUE starts as binary zeros, which are not digits. Adding to it stops the program with a "not numeric" error when runtime checks are on (-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?
IBM's COBOL dialect in fixed format, the COBOL that most banks and insurers run, as GnuCOBOL 3.2 compiles it with -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.

Coddy programming languages illustration

Learn COBOL with Coddy

Get started