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.
Interactive COBOL Execution Sandbox
Select a program, edit the working-storage input parameters, and execute the runtime simulator.
View underlying PROCEDURE DIVISION logic
READY FOR EXECUTION. PRESS [EXECUTE PROGRAM] TO RUN.
Most Common COBOL Verbs Cheat Sheet
The essential verbs used in the PROCEDURE DIVISION for day-to-day transaction processing.
Copies data from a source field or literal into target variable, applying automatic padding/formatting.
MOVE 100 TO WS-TOTAL.
Evaluates algebraic expressions with parentheses, exponents, and precise decimal precision.
COMPUTE NET = GROSS - DEDUCT.
Executes another paragraph or loops a code block multiple times or until a condition is met.
PERFORM 100-CALC 5 TIMES.
Prints literals and variable values out to standard output (SYSOUT console or terminal screen).
DISPLAY "BALANCE: " WS-BAL.
Natural-language arithmetic verbs with optional GIVING clause to target a third variable.
ADD BONUS TO SALARY GIVING NET.
Returns control back to the host operating system, closing open units and halting execution.
STOP RUN.
Returns to whoever called this program. In a main program it behaves like STOP RUN, so many shops use it everywhere.
GOBACK.
Invokes a separately compiled program, passing fields by reference. The callee declares them in its LINKAGE SECTION.
CALL 'TAXCALC' USING WS-GROSS WS-TAX.
Sets numeric fields in a group to zero and alphanumeric fields to spaces. Your best defense against S0C7.
INITIALIZE WS-CUSTOMER-REC.
Counts or replaces characters inside a field. COBOL's closest thing to a find-and-replace.
INSPECT WS-PHONE REPLACING ALL '-' BY SPACE.
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.
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.
-
1Install the compiler
# macOS brew install gnucobol # Ubuntu / Debian (and Windows via WSL) sudo apt install gnucobol # confirm it worked cobc --version
-
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
-
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
- Hello world, then read your name with ACCEPT.
- A loan calculator using COMPUTE ROUNDED.
- Read a CSV with UNSTRING and total a column.
- A report with page headers and edited PICs like
$ZZ,ZZ9.99. - An indexed-file customer lookup by key.
Check your understanding
Four questions on control flow, flags, and arithmetic.