Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL COMPUTE Statement

Intermediate 🕑 10 min read 👁 0 views

COBOL COMPUTE Statement

The COMPUTE statement performs arithmetic operations using a formula-like syntax.

Syntax

COMPUTE identifier = arithmetic-expression [ROUNDED]
    [ON SIZE ERROR imperative-statement]
    [NOT ON SIZE ERROR imperative-statement]
[END-COMPUTE]

Arithmetic Operators

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation

Example Program

       IDENTIFICATION DIVISION.
       PROGRAM-ID. COMPUTE-DEMO.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-PRINCIPAL   PIC 9(6)V99 VALUE 10000.00.
       01 WS-RATE        PIC V9(4) VALUE 0.0750.
       01 WS-TIME        PIC 9(2) VALUE 5.
       01 WS-INTEREST    PIC 9(6)V99 VALUE 0.
       01 WS-TOTAL       PIC 9(7)V99 VALUE 0.
       01 WS-SQUARE      PIC 9(10) VALUE 0.

       PROCEDURE DIVISION.
      * Simple interest formula: I = P * R * T
           COMPUTE WS-INTEREST =
               WS-PRINCIPAL * WS-RATE * WS-TIME
           DISPLAY 'Simple Interest: ' WS-INTEREST

      * Total amount
           COMPUTE WS-TOTAL = WS-PRINCIPAL + WS-INTEREST
           DISPLAY 'Total Amount: ' WS-TOTAL

      * Exponentiation
           COMPUTE WS-SQUARE = 5 ** 4
           DISPLAY '5 to the power 4: ' WS-SQUARE

      * Complex expression
           COMPUTE WS-TOTAL =
               (WS-PRINCIPAL * (1 + WS-RATE) ** WS-TIME)
               ROUNDED
           DISPLAY 'Compound Amount: ' WS-TOTAL

           STOP RUN.

Expected Output

Simple Interest: 003750.00
Total Amount: 0013750.00
5 to the power 4: 0000000625
Compound Amount: 0014356.29

Key Points

  • COMPUTE replaces multiple arithmetic statements
  • Supports standard mathematical operators
  • ** is the exponentiation operator
  • Parentheses control order of operations