Skip to main content
eLearner.app

COBOL Course

Cheatsheet

A quick reference — the essential syntax of COBOL on a single page. Press Ctrl/Cmd + P to print it.

COBOL · Cheatsheet — eLearner.app

Structure and Divisions

  • Minimal Program

           IDENTIFICATION DIVISION.
           PROGRAM-ID. MINIMAL-PROGRAM.
           PROCEDURE DIVISION.
               DISPLAY "Hello from COBOL".
               STOP RUN.

    IDENTIFICATION DIVISION, PROGRAM-ID, and PROCEDURE DIVISION are mandatory in every program.

  • Fixed Format Rules

    Column 1-6   : Sequence number (optional)
    Column 7     : Comments (*), Continuations (-)
    Column 8-11  : Area A (Divisions, Sections, Paragraphs, level 01)
    Column 12-72 : Area B (Statements, data clauses, levels > 01)

    Traditional COBOL requires rigid column alignment. A * character in column 7 marks a comment.

Variables and Types (DATA DIVISION)

  • Variables Declaration

           DATA DIVISION.
           WORKING-STORAGE SECTION.
           01 WS-USER-NAME PIC X(20) VALUE "Alice".
           01 WS-USER-AGE  PIC 9(3)  VALUE 25.
           01 WS-SALARY    PIC 9(4)V99 VALUE 1500.50.

    Variables are declared in WORKING-STORAGE SECTION. Level 01 indicates main records, PIC defines the type (X for alphanumeric, 9 for numeric, V for implicit decimals).

  • Structured Variables (Records)

           01 WS-USER-RECORD.
              05 WS-FIRST-NAME PIC X(15) VALUE "John".
              05 WS-LAST-NAME  PIC X(15) VALUE "Doe".
              05 WS-DETAILS.
                 10 WS-ROLE    PIC X(10) VALUE "Coder".

    Data hierarchy uses level numbers (01, 05, 10) to group sub-fields into complex records.

COBOL Arithmetic

  • Formal Mathematical Statements

    ADD 5 TO WS-COUNTER.
    ADD WS-VAL-A TO WS-VAL-B GIVING WS-SUM.
    SUBTRACT 10 FROM WS-TOTAL.
    MULTIPLY WS-QTY BY WS-PRICE GIVING WS-SUBTOTAL.
    DIVIDE WS-TOTAL BY 2 GIVING WS-HALF.

    Use GIVING to store the result in a new variable without modifying the initial operand.

  • COMPUTE Statement

    COMPUTE WS-FINAL-AMOUNT = (WS-SUBTOTAL * 1.22) - WS-DISCOUNT.

    COMPUTE allows complex algebraic calculations using standard math operators (+, -, *, /, **).

Control Flow

  • IF-ELSE Conditionals

    IF WS-USER-AGE >= 18 THEN
        DISPLAY "Access granted"
    ELSE
        DISPLAY "Access denied"
    END-IF.

    Conditional blocks must always terminate with END-IF (or a period "." in older dialects).

  • EVALUATE (Switch-Case)

    EVALUATE WS-USER-AGE
        WHEN 0 THRU 17
            DISPLAY "Minor"
        WHEN 18 THRU 64
            DISPLAY "Adult"
        WHEN OTHER
            DISPLAY "Senior"
    END-EVALUATE.

    EVALUATE checks multiple conditions and is equivalent to switch-case or nested IF-ELSE structures.

Paragraphs and Loops (Iterations)

  • Paragraphs and PERFORM

           PROCEDURE DIVISION.
           MAIN-LOGIC.
               PERFORM DO-INITIALIZE.
               DISPLAY "Running...".
               STOP RUN.
    
           DO-INITIALIZE.
               DISPLAY "Initializing".

    Paragraphs partition PROCEDURE DIVISION code into logical reusable modules called with PERFORM.

  • Loops (PERFORM TIMES / UNTIL)

    * Fixed numerical loop
    PERFORM 5 TIMES
        DISPLAY "Hello"
    END-PERFORM.
    
    * Conditional loop (while/until)
    PERFORM UNTIL WS-COUNTER > 10
        DISPLAY "Count: " WS-COUNTER
        ADD 1 TO WS-COUNTER
    END-PERFORM.

    PERFORM UNTIL executes the block UNTIL the condition becomes true (evaluated before each iteration).

eLearner.app · COBOL Course · cheatsheet generated from lesson content.