COBOL GnuCOBOL 3.2.0 Β·
β verified by execution on 2026-08-16
In modern languages, you assign values using an equals sign (name = "Bob"). In COBOL, assignment is an action performed by the MOVE statement.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. MOVE-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 SRC PIC X(5) VALUE 'HELLO'. 01 DEST PIC X(5). PROCEDURE DIVISION. MOVE SRC TO DEST. DISPLAY DEST. STOP RUN.
Output
HELLO
Your output
MOVE copies data from the sending area to the receiving area. It does not alter the sending variable.
The Danger of Truncation
Because memory is perfectly rigid, moving data between variables of different sizes forces COBOL to adapt.
If you move data into a variable that is too big, COBOL pads it (with spaces for text, zeros for numbers). If you move data into a variable that is too small, COBOL silently chops off the excess data (truncation).
Crucially, text is truncated on the right, but numbers are truncated on the left!
The Silent Left-Truncation
A modern programmer writing in Python or Java expects one of two things to happen when a number overflows: either the variable dynamically resizes to fit it (like Pythonβs integers), or the runtime throws a loud OverflowException and crashes the program.
That intuition fails in COBOL because there are no dynamic objects to manage memory or throw exceptions. Memory is rigidly fixed at compile time.
Why did the language designers build it to fail silently on the left? In the 1960s, COBOL was built for batch processing millions of banking and insurance records overnight. Halting an entire batch job because one variable overflowed was considered a catastrophic failure. Instead, the runtime was designed to align the decimal points, chop off whatever high-order digits didnβt fit, and keep running. It was expected that the programmer strictly verified data sizes upstream.
This means COBOL will happily turn $1,000,050 into $50 without throwing a single error!
The Boundary: ON SIZE ERROR
This silent truncation rule stops applying if you explicitly tell COBOL to watch for it. By adding the ON SIZE ERROR phrase to a COMPUTE or math statement, you can define an imperative action to take when an overflow occurs, preventing the corrupted data from being saved.
Verbose Math vs COMPUTE
Early COBOL forced programmers to write math out like spoken English sentences.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. ADD-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 COUNTER PIC 9(2) VALUE 05. PROCEDURE DIVISION. ADD 10 TO COUNTER. DISPLAY COUNTER. STOP RUN.
Output
15
Your output
While ADD, SUBTRACT, MULTIPLY, and DIVIDE exist, they quickly become a nightmare to read when performing complex formulas. Modern COBOL uses the COMPUTE statement, which lets you write standard algebraic expressions using +, -, *, /, and ** (exponentiation).
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
IDENTIFICATION DIVISION. PROGRAM-ID. COMPUTE-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 RESULT PIC 9(3). PROCEDURE DIVISION. COMPUTE RESULT = (10 + 5) * 2. DISPLAY RESULT. STOP RUN.
Output
030
You predicted
COMPUTE is just as efficient as the verbose verbs and is highly recommended for all modern COBOL development!
Check yourself
When moving a large number (e.g., '1234') into a shorter numeric variable (`PIC 9(2)`), which digits are kept?
Reveal answer
The rightmost digits ('34'). β For numeric variables, COBOL truncates on the left (high-order digits). It aligns the decimal point and drops any overflow on the left, keeping the rightmost digits '34'.
How does the MOVE verb change the data type of the sending variable?
Reveal answer
It doesn't. The sending variable is completely untouched. β MOVE only transfers bytes. It formats the data to fit the receiving variable's PIC clause, but the original sending variable is left exactly as it was.
Why is COMPUTE generally preferred over verbs like ADD or MULTIPLY for complex math?
Reveal answer
It allows you to evaluate standard algebraic expressions (e.g., A = (B + C) * D) instead of writing multiple verbose statements. β COMPUTE allows standard arithmetic expressions, making calculations far more readable than chaining multiple ADD and MULTIPLY verbs.
Challenges
Challenge 1 +20 XP
This program expects to display the full year '2026', but it prints '026'. Fix the receiving variable's PIC clause so it doesn't truncate the left digit.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Rewrite the verbose ADD and MULTIPLY statements into a single COMPUTE statement: TOTAL = (BASE + TAX) * QTY.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "0036\n"
Need a hint? (β25% XP)
Use COMPUTE TOTAL = (BASE + TAX) * QTY.
Show solution (0 XP)
IDENTIFICATION DIVISION.PROGRAM-ID. USE-COMPUTE.DATA DIVISION.WORKING-STORAGE SECTION.01 BASE PIC 9(2) VALUE 10.01 TAX PIC 9(2) VALUE 2.01 QTY PIC 9(2) VALUE 3.01 TOTAL PIC 9(4).PROCEDURE DIVISION. COMPUTE TOTAL = (BASE + TAX) * QTY. DISPLAY TOTAL. STOP RUN.