. COBOL . COBOL Initialize

COBOL Initialize

Working-Storage for Example 1 and 2

000340 01  CAT-AND-DOG.
000350     05  CAT    PIC 9.
000360     05  DOG    PIC X.   

Initialize Example Number 1 - Initialize a PIC 9 and a PIC X field

004310
004320     INITIALIZE CAT.
004430     INITIALIZE DOG.
004440
This will move a 0 to CAT and a space to DOG.

Initialize Example Number 2 - Initialize a PIC 9 and a PIC X field

004310
004320     INITIALIZE CAT-AND-DOG.
004440
This will also move a 0 to CAT and a space to DOG.

Working-Storage for Example 3

000340 01  CATS-AND-DOGS-GROUP.
000350     05  CATS-AND-DOGS   OCCURS 10.
000350         10  CATS        PIC 9.
000360         10  DOGS        PIC X.   

Initialize Example Number 3 - Initialize a table

004310
004320     INITIALIZE CATS-AND-DOGS.
004440
This will move a 0 to all 10 CATS and a space to all 10 DOGS. Certainly a very handy way to initialize a table. NOTE: This verb can sometimes use a lot of CPU.
Comments Comments are left by visitors to FluffyCat.com, are not endorsed by FluffyCat.com, and may or may not be accurate.
Comment by TheMadProfessor Rate this Comment

Actually, INITIALIZE has an optional REPLACING clause (albeit one I've seldom seen used):

INITIALIZE identifier-1 REPLACING class-identifier BY identifier-2|literal-1

where class-identifier can be ALPHABETIC, APLHANUMERIC, NUMERIC, ALPHANUMERIC-EDITED, NUMERIC-EDITED, DBCS or EGCS. (The latter two are IBM-extensions, not standard.)

INITIALIZE field-name REPLACING ALPHANUMERIC BY '&' would work but not for NUMERIC. Not sure whether it would work for NUMERIC-EDITED, but I would suspect not. INITIALIZE field-name REPLACING NUMERIC BY 7 should work just fine...BY '7' might or might not, probably depends on the compiler.

Note that INITIALIZE does not look at REDEFINES fields (which makes sense, as there could easily be multiple ways to initialize a given memory location if it did); only the primary definition controls how a given area is initialized. Also, it will only alter named variables unless the WITH FILLER optional clause in included, IWC all FILLER fields will also be initialized as per their PICTURE clauses.

The comment about example 3 being wrong I believe is correct, but for a reason other than that stated. Since CATS-AND-DOGS occurs 10 times, I believe most compilers will toss an error as the example is coded (YMMV). A method that should work on any compiler:

PERFORM VARYING SUB1 FROM 1 BY 1 UNTIL SUB1 > 10
....INITIALIZE CATS-AND-DOGS(SUB1)
END-PERFORM

What I'm unsure of is what would happen if one did INITIALIZE CATS-AND-DOGS-GROUP. It may be compiler-dependent as to whether the elementary items within the OCCURS would be treated as a redefinition of CATS-AND-DOGS or not.

Comment by archive Rate this Comment

Just wanted to write ;)

i think OCCURS can't be used in level 01

Comment by archive Rate this Comment

Thanks for the site. It is very helpful & straightforward.

I have a question... How would an INITIALIZE statement look if I wanted to override the default value (zero or space) with a '&' ???

Comment by dsiebel Rate this Comment

The Example 3 code is invalid: the OCCURS clause cannot be on the 01 level.

Comment by Larry Rate this Comment

You can use initialize on group or non group items. Try it out, it's always possible that whatever compiler you are using doesn't accept it for some reason. I've only used initialize on IBM mainframe and compliant systems.

Initialize does make more sense on group items, on non group items it is probably more efficient and clear to move zero or spaces.

Comment by archive Rate this Comment

The INITIALIZE statement may only be used on group items is this TRUE or FALSE? why?

Comment by archive Rate this Comment

You might want to mention that use of the INITIALIZE statement on a variable that uses a VALUES clause will cast the type and not the content.

I see guys at my shop struggle with this daily.

Comment by archive Rate this Comment

I was told to stop using this instruction because it was placing binary zeroes in some fields.

I this true? I've never heard it before...

Sign In
to add your own comment