Variable Example


Scenario - Explaining about different types of variables declaration in COBOL program.

Code -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
      * Individual variable
       01 LEVEL-1       PIC 9(03) VALUE 256.
      * Group variable
       01 LEVEL-GROUP.
      * Elementary variables
          05 LEVEL-21       PIC 9(03) VALUE 256.
          05 LEVEL-22       PIC 9(03) VALUE 128.

       PROCEDURE DIVISION.

           DISPLAY 'INDIVIDUAL VARIABLE   : ' LEVEL-1. 
           DISPLAY 'GROUP VARIABLE        : ' LEVEL-GROUP.
           DISPLAY 'ELEMENTARY VARIABLE-1 : ' LEVEL-21. 
           DISPLAY 'ELEMENTARY VARIABLE-2 : ' LEVEL-22.
		   
           STOP RUN.

Output -

INDIVIDUAL VARIABLE   : 256
GROUP VARIABLE        : 256128
ELEMENTARY VARIABLE-1 : 256
ELEMENTARY VARIABLE-2 : 128

Explaining Example -

In the above example:

  • The LEVEL-1 individual variable is declared and initialized with the value 256. LEVEL-GROUP is declared as a group variable with two elementary variables - LEVEL-21 and LEVEL-22.
  • LEVEL-GROUP displays the data of both elementary variables LEVEL-21 and LEVEL-22.