COBOLPRIMER
Run COBOL

Part 2 of 3

Writing COBOL

How COBOL expresses decisions, loops, and subroutines, compared line by line with JavaScript. Then run programs in the lab and set up a real compiler.

Control flow, translated

If you already write JavaScript, most COBOL logic maps one-to-one. The trouble is in the few places where it doesn't.

COBOL

        
JavaScript equivalent

        

Interactive COBOL Execution Sandbox

Select a program, edit the working-storage input parameters, and execute the runtime simulator.

PROGRAM INPUT PARAMETERS PAYROLL.cbl
View underlying PROCEDURE DIVISION logic

              
GnuCOBOL / IBM z/OS Runtime
SYSOUT - IBM 3270 Terminal
IBM z/OS ENTERPRISE COBOL COMPILER V6.4
READY FOR EXECUTION. PRESS [EXECUTE PROGRAM] TO RUN.
> System idle. Awaiting job trigger...
RC=0000 (NORMAL) LINES: 1

Most Common COBOL Verbs Cheat Sheet

The essential verbs used in the PROCEDURE DIVISION for day-to-day transaction processing.

MOVE Assignment

Copies data from a source field or literal into target variable, applying automatic padding/formatting.

MOVE 100 TO WS-TOTAL.
COMPUTE Math Expression

Evaluates algebraic expressions with parentheses, exponents, and precise decimal precision.

COMPUTE NET = GROSS - DEDUCT.
PERFORM Subroutines & Loops

Executes another paragraph or loops a code block multiple times or until a condition is met.

PERFORM 100-CALC 5 TIMES.
DISPLAY Console Output

Prints literals and variable values out to standard output (SYSOUT console or terminal screen).

DISPLAY "BALANCE: " WS-BAL.
ADD / SUBTRACT Arithmetic

Natural-language arithmetic verbs with optional GIVING clause to target a third variable.

ADD BONUS TO SALARY GIVING NET.
STOP RUN Termination

Returns control back to the host operating system, closing open units and halting execution.

STOP RUN.
GOBACK Return

Returns to whoever called this program. In a main program it behaves like STOP RUN, so many shops use it everywhere.

GOBACK.
CALL Subprogram

Invokes a separately compiled program, passing fields by reference. The callee declares them in its LINKAGE SECTION.

CALL 'TAXCALC' USING WS-GROSS WS-TAX.
INITIALIZE Reset

Sets numeric fields in a group to zero and alphanumeric fields to spaces. Your best defense against S0C7.

INITIALIZE WS-CUSTOMER-REC.
INSPECT Text scan

Counts or replaces characters inside a field. COBOL's closest thing to a find-and-replace.

INSPECT WS-PHONE REPLACING ALL '-' BY SPACE.
UNSTRING Split

The reverse of STRING: splits a delimited field into several targets, like parsing a CSV line.

UNSTRING WS-LINE DELIMITED BY ','
  INTO WS-ID WS-NAME WS-AMT.
READ / WRITE File I/O

You READ a file but WRITE a record. That asymmetry trips up nearly everyone the first time.

READ CUST-FILE AT END SET WS-EOF TO TRUE.
WRITE RPT-LINE.

Write real COBOL on your own machine

The sandbox above is a simulation. GnuCOBOL is a free, real compiler that runs on Linux, macOS, and Windows, and it's enough to learn everything on this page.

  1. 1Install the compiler

    # macOS
    brew install gnucobol
    
    # Ubuntu / Debian (and Windows via WSL)
    sudo apt install gnucobol
    
    # confirm it worked
    cobc --version
  2. 2Compile and run

    # -x builds an executable from fixed-format source
    cobc -x HELLO.cbl
    ./HELLO
    
    # free-format source (no column rules)
    cobc -x -free hello.cob
  3. 3Graduate to a real mainframe

    IBM Z Xplore gives free, hands-on access to an actual z/OS system with guided challenges covering JCL, datasets, and COBOL. The Open Mainframe Project also publishes a free COBOL programming course on GitHub.

Editor setup

  • VS Code + IBM Z Open Editor: syntax checking, copybook resolution, and column guides.
  • Zowe Explorer: browse datasets and submit JCL on a mainframe straight from VS Code.
  • Set a ruler at column 72: text past it is silently ignored in fixed format, which causes baffling compile errors.

A practice ladder

  1. Hello world, then read your name with ACCEPT.
  2. A loan calculator using COMPUTE ROUNDED.
  3. Read a CSV with UNSTRING and total a column.
  4. A report with page headers and edited PICs like $ZZ,ZZ9.99.
  5. An indexed-file customer lookup by key.

Check your understanding

Four questions on control flow, flags, and arithmetic.

Question 1 of 4 Score: 0