Initialization in Cobol

Initialization in COBOL is used to set the declared variable values to an initial value. It sets the
Alphanumeric type(alphabetic, alphanumeric, alphanumeric-edited, DBCS, national, or national-edited) field values to Spaces and Numeric type(numeric or numeric-edited) field values to Zeroes. It is equivalent of moving eirther
zeroes are spaces to the variables according to their data type but when a group variable has many sub
fields with different data types then moving eirther zeroes or spaces to each one of them is not a good
coding standard. Instead a Single Initialize performs the same function as above.FIller is the one exception
which does not get effected with the Initialize verb.

Examples:
1. Elementary data itesm or Single Dat Items.

01 WW-ELEM-FIELD-ALPHA PIC X(10).
01 WW-ELEM-FIELD-NUM PIC 9(04).

INITIALIZE WW-ELEM-FIELD-ALPHA
WW-ELEM-FIELD-ALPHA.

Now th Alphanumeric field woul dbe initialzed to Spaces and the nueric one to Zeroes.

2. Group data items:

ex2:
01 WW-GROUP-VAR
05 WW-VAR-1 PIC X(05) VALUE ‘ALPHA’.
05 WW-VAR-2 PIC 9(04) VALUES 1234 .
05 FILLER PIC X(10) VALUE ‘END OF VAR’.

INITIALIZE WW-GROUP-VAR

With the above statement data from Var-1 and Var-2 gets removed and replaced with spaces and Zeroes. Here
the FILLER is intact and its values remains same. Move statement can be used to replace the Filler values.

3. Arrays

Initialize can be used to set an inittial values to all the occurences of the array or to a single
occurence.

01 WW-ARRAY.
05 WW-NAME-ARRAY OCCURS 10 TIMES.
06 WW-FIRST-NAME PIC X(10).
06 WW-LAST-NAME PIC X(10).
06 ww-age pic 999.

a)->To replace all the occurences of the above table, Initialize can be used on the elementary data item.
INITIALIZE WW-ARRAY.
b)–> to replaces only specific ocurence of the table
INITIALIZE WW-NAME-ARRAY(1) –>here instead if 1 any specific occurence can be given.

Replacing Option:
It has a unique option of specifying the values to be set as initial ones instead of the default values
(spaces and Zeroes).
Taking the example ex2 for repalcing.
INITIALIZE WW-GROUP-VAR REPLACING ALL NUERIC DATA BY 3421 and ALPHANUERIC BY ‘REPLC’.

Exceptions of Using Initialize:
– Filler
– Data Items with USAGE IS POINTER, USAGE IS FUNCTION-POINTER, or USAGE IS PROCEDURE-POINTER

Real Time uses where generally used:
File structure generally gets initialized after each record is written to the output file otherwise the
previuos values may exist.
Can be used where ever the structure or data item needs to be set to an initial value before processing further steps.

Add a Comment

Your email address will not be published. Required fields are marked *