Using COMPUTE and other arithmetic statements

Use the COMPUTE statement for most arithmetic evaluations rather than ADD, SUBTRACT, MULTIPLY, and DIVIDE statements. Often you can code only one COMPUTE statement instead of several individual arithmetic statements.

About this task

The COMPUTE statement assigns the result of an arithmetic expression to one or more data items:


Compute z     = a + b / c ** d - e
Compute x y z = a + b / c ** d - e

Some arithmetic calculations might be more intuitive using arithmetic statements other than COMPUTE. For example:

COMPUTE Equivalent arithmetic statements

Compute Increment = Increment + 1

Add 1 to Increment

Compute Balance =
    Balance - Overdraft

Subtract Overdraft from Balance

Compute IncrementOne =
    IncrementOne + 1
Compute IncrementTwo =
    IncrementTwo + 1
Compute IncrementThree =
    IncrementThree + 1

Add 1 to IncrementOne,
    IncrementTwo,
    IncrementThree

You might also prefer to use the DIVIDE statement (with its REMAINDER phrase) for division in which you want to process a remainder. The REM intrinsic function also provides the ability to process a remainder.

When you perform arithmetic calculations, you can use national decimal data items as operands just as you use zoned decimal data items. You can also use national floating-point data items as operands just as you use display floating-point operands.

Related tasks  
Defining numeric data