The COMPUTE statement assigns to one or more data items the value of an arithmetic expression.
General Format for Format 1 (arithmetic compute)
Format 2 (boolean compute)
Syntax Rules for Format 2
- Identifier-2 must reference an elementary boolean data item.
- Boolean-expression must not consist solely of the figurative constant ALL literal.
- "Equal", "Equals" and "=" are synonymous.
General Rules
- See the topics
Arithmetic Expressions,
The ROUNDED Phrase,
The ON SIZE ERROR Phrase,
Arithmetic Statements,
Overlapping Operands and
Multiple Results in Arithmetic Statements in the chapter
Procedure Division; the section
Explicit and Implicit Scope Terminators in the chapter
Concepts of the COBOL Language, and the section
Delimited Scope Statements in the chapter
Language Fundamentals.
- An arithmetic expression consisting of a single identifier or literal provides a method of setting the values of identifier-1
equal to the value of the single identifier or literal.
- The COMPUTE statement allows you to combine arithmetic operations without the need to explicitly store intermediate results
in temporary data items, as would be necessary using the arithmetic statements ADD, SUBTRACT, MULTIPLY and DIVIDE. The expression
is evaluated to as many digits as possible, and truncated or rounded to fit into identifier-1.
The intermediate results obtained during the expression evaluation are truncated as if they were moved to data items whose
PICTURE is determined by the COBOL system. This behavior is selected using the ARITHMETIC Compiler directive.
The number of boolean positions in the value resulting from the evaluation of boolean-expression is the number of boolean
positions in the largest boolean item referenced in the expression. The resulting value is moved to the data item referenced
by identifier-2 according to the rules of the MOVE statement.
- If more than one identifier is specified for the result of the operation, that is preceding "=", the value of the arithmetic
expression is computed, and then this value is stored as the new value of each occurrence of identifier-1 in turn.