COBOL table structure and output

260 views Asked by At

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.

  1. The DISPLAY statement will display the value _____ on the monitor.
    Answer: unknown

  2. When 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:10

  3. When 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.

1

There are 1 answers

1
Roger Sinasohn On BEST ANSWER

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 the MAINLINE-ROUTINE, the program will continue on and execute the DISPLAY-ROUTINE and LOAD-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 in LOAD-TABLE and after the PERFORM LOAD-TABLE statement has completed. Note the extra displays.)

Before move -- Sub: 01   PV: 00000
After move  --           PV: 00100

Before move -- Sub: 02   PV: 00000
After move  --           PV: 00200

Before move -- Sub: 03   PV: 00000
After move  --           PV: 00300

Before move -- Sub: 04   PV: 00000
After move  --           PV: 00400

Before move -- Sub: 05   PV: 00000
After move  --           PV: 00500

Before move -- Sub: 06   PV: 00000
After move  --           PV: 00600

Before move -- Sub: 07   PV: 00000
After move  --           PV: 00700

Before move -- Sub: 08   PV: 00000
After move  --           PV: 00800

Before move -- Sub: 09   PV: 00000
After move  --           PV: 00900

Before move -- Sub: 10   PV: 00000
After move  --           PV: 01000

Completed Perform of LOAD-TABLE.
PAY-VALUE(25): 00000
00000

Before move -- Sub: 11   PV: 00000
After move  --           PV: 01100

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!