I'm trying to userstand how this works:
WORKING-STORAGE SECTION.
01 PAY-TABLE.
05 PAY-VALUES OCCURS 25 TIMES PIC 9(3)V99.
01 WORKING-VALUES.
05 SUB PIC 9(2) VALUE ZERO.
PROCEDURE DIVISION.
INITIALIZE-ROUTINE.
INITIALIZE PAY-TABLE
MAINLINE-ROUTINE.
PERFORM LOAD-TABLE
VARYING SUB FROM 1
BY 1
UNTIL SUB>10.
DISPLAY-ROUTINE.
DISPLAY PAY-VALUE (SUB)
LOAD-TABLE.
MOVE SUB TO PAY-VALUE (SUB).
I have this code as a review code in the book. There are several questions according to this code, with answers, however, I don't really understand why should it be exactly this answer.
The DISPLAY statement will display the value _____ on the monitor.
Answer: unknownWhen the value of SUB in the PERFORM statement is equal to 9, the value in PAY-VALUE (SUB), before the PERFORM executes, will be equal to:
Answer:10When the LOAD-TABLE routine has executed for the last time, the value in PAY-VALUE (25) will be:
Answer: 26
I've tried to read tutorials about tables but still don't understand how this example works.
Note that much of this depends on the environment -- some COBOL compilers behave differently and there are also variances between platforms. (I've had a lot of fun with this porting code from HP3000 COBOL to Linux NetCOBOL.) But generally speaking...
1) The book may say the answer is "unknown" because compilers may initialize things differently.
INITIALIZE PAY-TABLE
may set each instance of PAY-VALUE to zero or it may set PAY-TABLE (the 125 byte character field that is comprised of the 25 PAY-VALUEs) to spaces. What it should be, however, (as in the former case,) is zero.2) This is incorrect. Again, there is the possibility of variance (due to the reason mentioned in #1) but it should be zero. Because nothing has happened to PAY-VALUE yet (before the PERFORM for SUB=9), it would still be the value to which it had been initialized.
3) As in #2, with the same caveats, the answer should be zero. Because the
LOAD-TABLE
paragraph is only performed until SUB becomes greater than 10, PAY-VALUE (25) will never change from its initialized value.Note also that the program is poorly written -- without a
STOP RUN.
at the end of theMAINLINE-ROUTINE
, the program will continue on and execute theDISPLAY-ROUTINE
andLOAD-TABLE
paragraphs one last time.There are also, a number of typos, most notably the definition of PAY-VALUES (with an S) and the usage of PAY-VALUE (no S).
Here is a sample run (I added displays before and after the
MOVE
statement inLOAD-TABLE
and after thePERFORM LOAD-TABLE
statement has completed. Note the extra displays.)Bear in mind that the PAY-VALUE values show as hundreds (e.g., 00100 instead of 1) because the field is defined as 9(03)V99 meaning there is an implied decimal place and two digits to the right of it.
Hope this helps!