Default/Initial Value of Index Variable in Cobol



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Default/Initial Value of Index Variable in Cobol

Postby Nik22Dec » Fri Jan 06, 2012 1:59 pm

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.
Thanks,
Nik
User avatar
Nik22Dec
 
Posts: 68
Joined: Mon Dec 26, 2011 6:38 pm
Has thanked: 2 times
Been thanked: 0 time

Re: Default/Initial Value of Index Variable in Cobol

Postby Nik22Dec » Fri Jan 06, 2012 2:30 pm

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.
Thanks,
Nik
User avatar
Nik22Dec
 
Posts: 68
Joined: Mon Dec 26, 2011 6:38 pm
Has thanked: 2 times
Been thanked: 0 time

Re: Default/Initial Value of Index Variable in Cobol

Postby BillyBoyo » Fri Jan 06, 2012 3:25 pm

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).
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Default/Initial Value of Index Variable in Cobol

Postby Robert Sample » Fri Jan 06, 2012 4:19 pm

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.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Default/Initial Value of Index Variable in Cobol

Postby Nik22Dec » Fri Jan 06, 2012 5:52 pm

Thanks so much BillyBoyo & Robert Sample for your replies. Now, I understand.Really appreciate!!
Thanks,
Nik
User avatar
Nik22Dec
 
Posts: 68
Joined: Mon Dec 26, 2011 6:38 pm
Has thanked: 2 times
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post