The COMPUTE Statement

The COMPUTE statement assigns to one or more data items the value of an arithmetic expression.

General Format
Syntax Rules
  1. Identifiers that appear only to the left of "=" must refer to either an elementary numeric item or an elementary numeric edited item

    OSVSVSC2MFor a floating-point item.

  2. OSVSVSC2MF"Equal" and "=" are synonymous.
General Rules
  1. 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.
  2. 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.
  3. 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.

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

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