Initialize Statement Example


Scenario - Initialize usage in COBOL program.

Code -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-NAME            PIC X(20). 
          05 FILLER             PIC X(05). 
          05 WS-GENDER          PIC X(01). 
          05 FILLER             PIC X(05). 
          05 WS-TODAY           PIC 9(08). 
          05 FILLER             PIC X(01). 

       PROCEDURE DIVISION.

           MOVE ALL '-'         TO WS-VAR. 
           DISPLAY 'BEFORE INIT:  ' WS-VAR.

           INITIALIZE WS-VAR.
           DISPLAY 'AFTER INIT:   ' WS-VAR.

           STOP RUN.

JCL -

//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//**
//STEP01  EXEC PGM=INITGRP
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR
//SYSOUT   DD  SYSOUT=*

Output -

BEFORE INIT:  ----------------------------------------
AFTER INIT:                       ----- -----00000000-

Explaining Example -

In the above example:

  • First, it moves the character '-' to every position within WS-VAR using the MOVE statement.
  • Then, it displays the content of WS-VAR before the initialization.
  • After that, it initializes WS-VAR, setting all its elements to their default initial values except FILLER.
  • Finally, it displays the content of WS-VAR after the initialization.
Note! FILLER positions are not initialized with the INITIALIZE statement, and the remaining variables are initialized according to their data type.