Working with Variables: MOVE, COMPUTE & Arithmetic Verbs
COBOL Fundamentals
Chapter 4 · Working with Variables: MOVE, COMPUTE & Arithmetic Verbs
Chapter 3 built the WS-CUSTOMER-RECORD structure but never actually changed any of its values. This chapter covers COBOL's own real verbs for doing that — copying data with MOVE, and every real way COBOL performs arithmetic.
MOVE: Real Alignment Rules Worth Knowing
MOVE copies a value from one field into another — but exactly how it lines the data up depends on the field's own real type, and getting this wrong is a genuinely common source of legacy-code confusion.
| Field Type | Real Alignment Rule |
|---|---|
| Alphanumeric (PIC X) | Left-justified — extra space on the right is padded with blanks; if the destination is shorter, extra characters on the right are truncated |
| Numeric (PIC 9) | Aligned on the implied decimal point — digits are padded with zeros or truncated at whichever end is needed, never simply left- or right-justified the way text is |
"HI" into a PIC X(5) field gives "HI " — padded on the right. But moving a value from a PIC 9(3)V99 field into a shorter PIC 9(2)V9 field aligns on the decimal point first, then truncates the leftmost integer digit and the rightmost decimal digit — never simply "the first few characters." Text and numbers genuinely follow different real rules.
The Real Arithmetic Verbs
COBOL provides four dedicated real verbs, each with its own genuine syntax variations:
Every one of these real forms can add ROUNDED and ON SIZE ERROR — two genuine safety clauses worth knowing from the start.
ROUNDED
Rounds the result to fit the receiving field's own real PICTURE clause, instead of silently truncating extra decimal places.
ON SIZE ERROR
Runs a real block of statements if the result genuinely doesn't fit the receiving field (overflow, or division by zero) — without it, an overflow can silently produce a wrong, truncated value instead.
COMPUTE: A Real, More Flexible Alternative
Rather than chaining several separate arithmetic verbs together, COMPUTE accepts a genuine arithmetic expression directly — real operators +, -, *, /, and ** (exponentiation), combined using standard real operator precedence, with parentheses to override it exactly as in ordinary mathematical notation.
Both ROUNDED and ON SIZE ERROR work identically here, applying to the entire computed expression's own final result.
Arithmetic Verbs vs. COMPUTE: When to Use Which
| ADD/SUBTRACT/MULTIPLY/DIVIDE | COMPUTE | |
|---|---|---|
| Best for | A single, simple real operation | Any real, multi-step expression |
| Readability | Reads close to plain English | Reads like ordinary math notation |
| Real, common legacy usage | Still extremely common in older code | Preferred for anything beyond one operation |
Hands-On Exercises
Using this chapter's own real alignment rules, explain in your own words what value would result from moving the alphanumeric value "COBOL" into a field defined as PIC X(3), and why the result differs from what happens with a numeric MOVE of the same length mismatch.
A colleague says, "ON SIZE ERROR and ROUNDED do basically the same job — they both handle a result that doesn't fit." Using this chapter's own real material, explain in your own words why this isn't accurate.
📄 View solutionRewrite this chapter's own real COMPUTE WS-TOTAL = (WS-PRICE * WS-QTY) + WS-SHIPPING. statement using only the dedicated ADD and MULTIPLY verbs instead. Explain, in your own words, why the COMPUTE version is genuinely easier to read for this specific calculation.
Chapter 4 Quick Reference
- MOVE — alphanumeric: left-justified, right-padded/truncated; numeric: decimal-point-aligned, padded/truncated at either end
- Real arithmetic verbs: ADD/SUBTRACT/MULTIPLY/DIVIDE, each with a GIVING form (and DIVIDE's own real REMAINDER)
- ROUNDED — rounds instead of silently truncating; ON SIZE ERROR — catches genuine overflow/division-by-zero
- COMPUTE — real +, -, *, /, ** operators with standard precedence and parentheses, for any multi-step expression