INITIALIZE Statement


INITIALIZE sets the variables with system-defined initial values based on their data types. It's functionally equivalent to one or more MOVE statements. It is a convenient way to reset the full copybook, and group variable, ensuring that they start with predictable values.

Syntax -

INITIALIZE group-variable-1 [group-variable-2 ...]
    [REPLACING ALPHABETIC | 
	           ALPHANUMERIC | 
			   ALPHANUMERIC-EDITED | 
			   NUMERIC | 
			   NUMERIC-EDITED
					DATA BY {literal-1} ALL {literal-2}]
Note! All statements coded in [ ] are optional.

Parameters -

  • group-variable-1, group-variable-2, ...- The variable names we wish to initialize.
  • REPLACING - Optional. It is used to replace default initializing values with new ones.
    • NUMERIC DATA - Specifies that numeric variable initalized with literal-1 value instead of ZEROES. Similarly, ALPHABETIC DATA, ALPHANUMERIC DATA, ...
    • BY literal-1 - Specifies the new literal-1 value to initialize the variables.
    • ALL literal-2 - Specifies the new literal-2 value to initialize non-numeric variables.

How INITIALIZE Works?

  • For NUMERIC fields (like PIC 9), they are set to zeros.
  • For ALPHANUMERIC fields (like PIC X), they are set to spaces.
  • For ALPHABETIC fields (like PIC A), they are set to spaces.
  • For ALPHA-NUMERIC EDITED and NUMERIC EDITED fields, they retain their specific edit symbols, and other positions are set to zeros or spaces as appropriate.
  • FILLER items, redefined items, and items with USAGE are not initialized by the INITIALIZE statement.

Practical 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.

Output -

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

Explaining Example -

FILLER positions are not initalized with INITIALIZE statement and remaining variables initialized according to their data type.