Page 1 of 1

Default/Initial Value of Index Variable in Cobol

PostPosted: Fri Jan 06, 2012 1:59 pm
by Nik22Dec
Hi All,

My question is if I am not initializing/setting any value of the index variable then, what value will the index hold. To rephrase my question, let's assume that the table definition is -

01 WS-PERSON-INFO                   SYNC.     
    03 WS-NAME-INFO OCCURS 10 TIMES           
                    INDEXED BY WS-NM-IDX.     
       05 WS-NAME                    PIC X(10).


& I am executing the statement -
MOVE 'A' TO WS-NAME(WS-NM-IDX)


then, 'A' would be moved to which position/index location in the 1-D table if I have not pre-set the index to any value.

Re: Default/Initial Value of Index Variable in Cobol

PostPosted: Fri Jan 06, 2012 2:30 pm
by Nik22Dec
To add to my previous post, I have already tried executing the above mentioned code. The value 'A' is moved to the first location of the table. What I am trying to understand is if the index will always point to the first occurrence of the table even if it is not initialized. Can that be taken as a concept.

Re: Default/Initial Value of Index Variable in Cobol

PostPosted: Fri Jan 06, 2012 3:25 pm
by BillyBoyo
Consult the Cobol manual (Language Reference).

Even if it is allowed, I'd always set it to a value before using it anyway, and (at least almost) everyone later looking at your program, for instance looking for a fault, would find this and think it "wrong" and waste time tracking it down.

The values in unitialiased storage are either random ("unpredicatable results") or binary zeros, depending on whether or not you have asked the storage to be initiallised. I have never seen a program that works using this. Either it is a program that works written by a good programmer, so you don't see it, or a program that is written by a bad programmer and has enough dumbness in, including of the time you show, to just not work, except in exceptional circumstances (= the paltry amount of test-data the author pushed into it).

Re: Default/Initial Value of Index Variable in Cobol

PostPosted: Fri Jan 06, 2012 4:19 pm
by Robert Sample
Section 1.8.1.9.2 of the COBOL Language Reference manual explains it to you:
The initial value of an index at run time is undefined, and the index must be initialized before it is used as a subscript. An initial value is assigned to an index with one of the following:

The PERFORM statement with the VARYING phrase

The SEARCH statement with the ALL phrase

The SET statement
In other words, you were lucky the uninitialized index was pointing to the first entry. It could have been pointing anywhere, including into your program code.

When IBM manuals use the word "must", wise programmers pay heed.

Re: Default/Initial Value of Index Variable in Cobol

PostPosted: Fri Jan 06, 2012 5:52 pm
by Nik22Dec
Thanks so much BillyBoyo & Robert Sample for your replies. Now, I understand.Really appreciate!!