In this topic, we described about the below sections -
Subscript is a number of occurrences of array element. Literal or a data-name can use as a subscript. It is a very straight forward value and each occurrence can be accessed by using subscript. Subscript maximum value limitation is equal to the number of OCCURS.
In the above diagram, the OCCURANCES is subscript. Incrementing subscript is equal to incrementing the total occurrence. Let us use the below declaration, to understand better about subscript -
01 BTECH-1ST-YEAR. 03 SUBJECT-MARKS PIC 9(03) OCCURES 6 TIMES.
In the above example, SUBJECT-MARKS have 6 OCCURRENCES. i.e. Subscript have 6 occurrences from 1 to 6. Subscript always starts from 1 and increase by one to refer the next occurrence of element.
Total 6 subject marks can be accessed like - SUBJECT-MARKS (1), SUBJECT-MARKS (2), SUBJECT-MARKS (3), SUBJECT-MARKS (4), SUBJECT-MARKS (5) and SUBJECT-MARKS (6).
How the Subscript is declared?
Subscript should be defined in a working-storage section. Mostly, subscript is declared with S9(04) COMP. If number of occurrences is more, change the subscript declaration to accommodate the occurrences max value. The subscript declaration for the above example is -
01 MARKS-SUB PIC S9(04).
How the Subscript is initialized?
Subscript is initialized by using MOVE statement. Without initializing, the results are unpredictable. The lowest possible subscript value is 1. The subscript initialization for the above example is -
MOVE 1 TO MARKS-SUB.
How the subscript is incremented/decremented?
Subscript can be incremented by ADD operator and can be decremented by SUBTRACT operator. Incrementing or Decrementing subscript moves the subscript by one occurrence. Incrementing subscript for the above example is -
ADD 1 TO MARKS-SUB.
Decrementing subscript for the above example is -
SUBTRACT 1 FROM MARKS-SUB.
How the table accessed using subscript in program?
Array element can be accessed by using the subscript like below.
The student marks can be accessed using subscript for the above example is -
Possible Errors: -
If the subscript increased beyond the number of occurrences and tries to access the table item with the subscript, that causes the S0C4 Protection Exception. The same error occurred if the subscript is not initialized and trying to access the table element with uninitialized subscript.
Debug the program to verify the subscript value at the time of program abend.