Simple Compute Example


Scenario - Let's assume we have three variables, WS-INP1, WS-INP2, and WS-INP3. We want to perform the expression (WS-INP1 + WS-INP2) * WS-INP3 and store it in WS-OP.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COMPUTEP.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-INP1        PIC 9(3) VALUE 240.
          05 WS-INP2        PIC 9(2) VALUE 10.
          05 WS-INP3        PIC 9(2) VALUE 20.
          05 WS-OP          PIC 9(5).

       PROCEDURE DIVISION.

           COMPUTE WS-OP = ( WS-INP1 + WS-INP2 ) * WS-INP3.
           DISPLAY "RESULT: " WS-OP.

           STOP RUN.

Output -

RESULT: 05000

Explaining Example -

In the above example:

  • It calculates a result by adding WS-INP1 and WS-INP2, then multiplying the sum by WS-INP3.
  • The result is stored in WS-OP, and finally, the program displays the result and terminates.