Compute statement in COBOL

Compute statement

  • For writing arithmetic expressions in COBOL, compute statements are used.
  • This statement is the replacement of ADD, subtract, Multiply and divide.

Example : Demonstration of compute statement

IDENTIFICATION DIVISION.
PROGRAM-ID. CS.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-N1 PIC 9(3) VALUE 5.
   01 WS-N2 PIC 9(4) VALUE 7.
   01 WS-N3 PIC 9(4) VALUE 8.
   01 WS-NA PIC 9(3) VALUE 80.
   01 WS-NB PIC 9(3) VALUE 20.
   01 WS-NC PIC 9(3).

PROCEDURE DIVISION.
   COMPUTE WS-NC= (WS-N1 * WS-N2) - (WS-NA / WS-NB) + WS-N3.
   DISPLAY "WS-NUM1     : " WS-N1
   DISPLAY "WS-NUM2     : " WS-N2
   DISPLAY "WS-NUM3     : " WS-N3
   DISPLAY "WS-NUMA     : " WS-NA
   DISPLAY "WS-NUMB     : " WS-NB
DISPLAY "Result of compute is     : " WS-NC

STOP RUN.


Output:
WS-NUM1     : 005
WS-NUM2     : 0007
WS-NUM3     : 0008
WS-NUMA     : 080
WS-NUMB     : 020
Result of compute is     : 039