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 TypeReal 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
Why This Genuinely Surprises Newcomers Moving "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:

ADD A TO B. *> B = B + A ADD A B GIVING C. *> C = A + B (A, B unchanged) SUBTRACT A FROM B. *> B = B - A SUBTRACT A FROM B GIVING C. *> C = B - A MULTIPLY A BY B. *> B = A * B MULTIPLY A BY B GIVING C. *> C = A * B DIVIDE A INTO B. *> B = B / A DIVIDE A BY B GIVING C. *> C = A / B DIVIDE A BY B GIVING C REMAINDER D. *> C = quotient, D = remainder

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.

COMPUTE WS-TOTAL = (WS-PRICE * WS-QTY) + WS-SHIPPING. COMPUTE WS-BALANCE ROUNDED = WS-BALANCE + WS-INTEREST ON SIZE ERROR DISPLAY "BALANCE OVERFLOW" END-COMPUTE.

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/DIVIDECOMPUTE
Best forA single, simple real operationAny real, multi-step expression
ReadabilityReads close to plain EnglishReads like ordinary math notation
Real, common legacy usageStill extremely common in older codePreferred for anything beyond one operation

Hands-On Exercises

Exercise 1

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.

📄 View solution
Exercise 2

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 solution
Exercise 3

Rewrite 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.

📄 View solution

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