compute

Command:

COMPUTE

Syntax:

COMPUTE <compute-variable> [ROUNDED] = <expression>.

Description:

The COMPUTE statement is used to evaluate a normal mathematical expression, and place the result in compute-variable. Refer to the Expressions and Conditions section of Chapter 3, CobolScript Language Constructs, for details on the various forms that expressions are permitted to take.

COMPUTE also supports the use of functions; see Appendix B, Function Reference, for complete details on the functions supported.

The use of alphanumeric variables or string literals in a COMPUTE statement is illegal. Also, only one variable can be acted upon at a time in a CobolScript COMPUTE statement. This means that multiple assignment statements must be used to assign multiple variables.

To identify size errors (encountered when a COMPUTE result is larger than the target variable’s picture clause permits) first check the expression result in a condition, since size errors do not cause direct program errors. For instance, the following three statements will place a value of 11 in num_variable without causing a direct program error:

1 num_var PIC 99 VALUE 0.

1 increment_var PIC 999 VALUE 111.

COMPUTE num_var = num_var + increment_var.

This type of overflow can be trapped by first checking the expression with a conditional statement, as in the following:

IF (num_var + increment_var) >= 100

DISPLAY `Limit bypassed`

ELSE

COMPUTE num_var = num_var + increment_var

END-IF.

Example Usage:

COMPUTE var = var + 5.

COMPUTE depreciation =

DDBAMT(cost, life, period, salvage-value).

COMPUTE delta = (((x+y)/z)%3)^1.86 – SQRT(x).

See Also:

ADD

SUBTRACT

MULTIPLY

DIVIDE

Sample Program:

COMPUTE.CBL