Computation Example


Scenario - Declaring, initializing, their usage, and display of COMP variables.

Code -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-CVAR          PIC 9(06) COMP.

       PROCEDURE DIVISION.

           MOVE 128255     TO WS-CVAR.
           DISPLAY "COMP VARIABLE VALUE: " WS-CVAR.
           DISPLAY "LENGTH OF COMP VARIABLE IS: " 
                           LENGTH OF WS-CVAR.

           STOP RUN.

Output -

COMP VARIABLE VALUE: 128255
LENGTH OF COMP VARIABLE IS: 000000004

Explaining Example -

In the above example:

  • WS-CVAR occupies 4 bytes (5 through 9 bytes).
  • COMP stores the numbers in a signed decimal numbers format.