AI Generate COBOL docs instantly

COBOL Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

4 snippets

Basic COBOL program structure

Hello World

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
    DISPLAY 'Hello, World!'.
    STOP RUN.

Four Divisions

IDENTIFICATION DIVISION.
PROGRAM-ID. MYPROGRAM.

ENVIRONMENT DIVISION.

DATA DIVISION.

PROCEDURE DIVISION.
    STOP RUN.

Comments

      * This is a comment (column 7)
      *> Modern comment style
      DISPLAY 'Code'.  *> Inline comment

Line Format

      * Columns 1-6: Sequence number
      * Column 7: Indicator (*, /, -, D)
      * Columns 8-11: Area A (divisions, sections)
      * Columns 12-72: Area B (statements)
      * Columns 73-80: Program name

Data Division

4 snippets

Variable declarations and data structures

Working-Storage Section

DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-NAME           PIC X(30).
01  WS-AGE            PIC 99.
01  WS-SALARY         PIC 9(7)V99.
01  WS-COUNTER        PIC 999 VALUE ZERO.

Picture Clauses

01  WS-NUM            PIC 9(5).      *> 5 digits
01  WS-DECIMAL        PIC 9(5)V99.   *> 5 digits, 2 decimal
01  WS-ALPHA          PIC A(10).     *> Alphabetic
01  WS-ALPHANUM       PIC X(20).     *> Alphanumeric
01  WS-SIGNED         PIC S9(5).     *> Signed number

Group Items

01  WS-EMPLOYEE.
    05  WS-EMP-ID         PIC 9(5).
    05  WS-EMP-NAME.
        10  WS-FIRST-NAME PIC X(15).
        10  WS-LAST-NAME  PIC X(20).
    05  WS-EMP-SALARY     PIC 9(7)V99.

Redefines

01  WS-DATE-NUMERIC   PIC 9(8).
01  WS-DATE-FORMATTED REDEFINES WS-DATE-NUMERIC.
    05  WS-YEAR           PIC 9(4).
    05  WS-MONTH          PIC 99.
    05  WS-DAY            PIC 99.

Procedure Division

3 snippets

Program logic and flow control

Paragraphs

PROCEDURE DIVISION.
MAIN-LOGIC.
    PERFORM INITIALIZE-PROGRAM.
    PERFORM PROCESS-DATA.
    PERFORM CLEANUP-PROGRAM.
    STOP RUN.

INITIALIZE-PROGRAM.
    DISPLAY 'Starting program'.

PROCESS-DATA.
    DISPLAY 'Processing'.

CLEANUP-PROGRAM.
    DISPLAY 'Cleanup'.

Perform Until

PERFORM PROCESS-RECORD
    UNTIL WS-COUNTER > 10.

PROCESS-RECORD.
    ADD 1 TO WS-COUNTER.
    DISPLAY 'Counter: ' WS-COUNTER.

Perform Varying

PERFORM VARYING WS-COUNTER FROM 1 BY 1
    UNTIL WS-COUNTER > 100
    DISPLAY 'Count: ' WS-COUNTER
END-PERFORM.

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

File Handling

3 snippets

Sequential and indexed file operations

File Definition

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT EMPLOYEE-FILE
        ASSIGN TO 'EMPLOYEE.DAT'
        ORGANIZATION IS SEQUENTIAL
        ACCESS MODE IS SEQUENTIAL
        FILE STATUS IS WS-FILE-STATUS.

File Operations

OPEN INPUT EMPLOYEE-FILE.
READ EMPLOYEE-FILE
    AT END MOVE 'Y' TO WS-EOF
END-READ.
CLOSE EMPLOYEE-FILE.

Indexed File

FILE-CONTROL.
    SELECT INDEXED-FILE
        ASSIGN TO 'EMPIDX.DAT'
        ORGANIZATION IS INDEXED
        ACCESS MODE IS DYNAMIC
        RECORD KEY IS EMP-ID
        FILE STATUS IS WS-STATUS.

Arithmetic Operations

3 snippets

Mathematical computations

Basic Operations

ADD 10 TO WS-COUNTER.
SUBTRACT 5 FROM WS-BALANCE.
MULTIPLY WS-PRICE BY WS-QUANTITY GIVING WS-TOTAL.
DIVIDE WS-TOTAL BY WS-COUNT GIVING WS-AVERAGE
    REMAINDER WS-REMAIN.

Compute Statement

COMPUTE WS-RESULT = (WS-A + WS-B) * WS-C / WS-D.
COMPUTE WS-TOTAL = WS-PRICE * WS-QTY * (1 + WS-TAX-RATE).

Rounding

COMPUTE WS-ROUNDED = WS-VALUE
    ON SIZE ERROR
        DISPLAY 'Overflow error'
END-COMPUTE.

Conditional Statements

3 snippets

Decision-making logic

If-Else

IF WS-AGE >= 18
    DISPLAY 'Adult'
ELSE
    DISPLAY 'Minor'
END-IF.

Evaluate (Switch)

EVALUATE WS-GRADE
    WHEN 'A'
        DISPLAY 'Excellent'
    WHEN 'B'
        DISPLAY 'Good'
    WHEN 'C'
        DISPLAY 'Average'
    WHEN OTHER
        DISPLAY 'Failed'
END-EVALUATE.

Complex Conditions

IF WS-SALARY > 50000 AND WS-YEARS > 5
    OR WS-LEVEL = 'SENIOR'
    MOVE 'ELIGIBLE' TO WS-STATUS
END-IF.

String Handling

3 snippets

String manipulation operations

String Concatenation

STRING WS-FIRST-NAME DELIMITED BY SPACE
       ' ' DELIMITED BY SIZE
       WS-LAST-NAME DELIMITED BY SPACE
    INTO WS-FULL-NAME
END-STRING.

Unstring

UNSTRING WS-FULL-NAME
    DELIMITED BY SPACE
    INTO WS-FIRST-NAME
         WS-LAST-NAME
END-UNSTRING.

Inspect

INSPECT WS-STRING TALLYING WS-COUNT FOR ALL 'A'.
INSPECT WS-STRING REPLACING ALL 'OLD' BY 'NEW'.

Called Programs

2 snippets

Modular programming with CALL

Call Statement

CALL 'SUBPROG' USING WS-PARAM1 WS-PARAM2.

CALL 'CALCPROG' USING BY REFERENCE WS-INPUT
                      BY CONTENT WS-CONSTANT.

Subprogram Linkage

IDENTIFICATION DIVISION.
PROGRAM-ID. SUBPROG.
DATA DIVISION.
LINKAGE SECTION.
01  LS-PARAM1         PIC X(10).
01  LS-PARAM2         PIC 9(5).
PROCEDURE DIVISION USING LS-PARAM1 LS-PARAM2.
    DISPLAY LS-PARAM1.
    EXIT PROGRAM.

More Cheat Sheets

FAQ

Frequently asked questions

What is a COBOL cheat sheet?

A COBOL cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in COBOL. It helps developers quickly look up syntax without searching through documentation.

How do I learn COBOL quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important COBOL concepts?

Key COBOL concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my COBOL code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your COBOL code using AI.

Code Conversion Tools

Convert COBOL to Other Languages

Easily translate your COBOL code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate COBOL Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week