100% found this document useful (1 vote)
2K views

COBOL From Micro To Mainframe 3rd Edition PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

COBOL From Micro To Mainframe 3rd Edition PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 908

COBOL

From Micro to Mainframe

RT:elEntT T. [3Ft.AUEnl
AR{f L VnzcpuEz VtLL,A,Fl
ATTTHUR R. EIUgs
THIRD EDITIOil
ISBN O-I3_?1DBI7-A

lffifiilflillilililllililtlflll
Chapter 1 - Introduction

re 1.6 (continued)

23 FD PRINT-FILE
24 RECORD CONTAINS 132 CHARACTERS
25 DATA RECORD IS PRINT-LINE.
26 01 PRINT-LINE PIC X(132).
27
28 WORKING-STORAGE SECTION.
29 01 DATA-REMAINS-SWITCH PIC X(2) VALUE SPACES.
30
31 01 HEADING-LINE.
~~~Da(a Division
32 05 FILLER PIC X(10) VALUE SPACES.
33 05 FILLER PIC X(12) VALUE 'STUDENT NAME'
34 05 FILLER PIC X(110) VALUE SPACES.

36 01 DETAIL-LINE.
37 05 FILLER PIC X(8) VALUE SPACES.
38 05 PRINT-NAME PIC 1(25).
39 05 FILLER PIC X(99) VALUE SPACES. I
40
41 PROCEDURE DIVISION.
42 PREPARE-SENIOR-REPORT.
43 OPEN INPUT STUDENT-FILE
44 OUTPUT PRINT-FILE.
45 READ STUDENT-FILE
46 AT END MOVE 'NO' TO DATA-REMAINS-SWITCH
47 END-READ.
48 PERFORM WRITE-HEADING-LINE.
49 PERFORM PROCESS-RECORDS
50 UNTIL DATA-REMAINS-SWITCH = 'NO'.
51 CLOSE STUDENT-FILE
52 PRINT-FILE.
53 STOP RUN.
54
55 WRITE-HEADING-LINE. Procedure Division
56 MOVE HEADING-LINE TO PRINT-LINE.
57 WRITE PRINT-LINE.
58
59 PROCESS-RECORDS.
60 IF STU-CREDITS > 110 AND STU-MAJOR = 'ENGINEERING'
61 MOVE STU-NAME TO PRINT-NAME
62 MOVE DETAIL-LINE TO PRINT-LINE
63 WRITE PRINT-LINE
64 END-IF.
65 READ STUDENT-FILE
66 AT END MOVE 'NO' TO DATA-REMAINS-SWITCH
67 END-READ.
Flowchart Blocks for Problem 7

May be used more


than once

C O B O L Listing for Problem 8

IDENTIFICATION DIVISION.
PROGRAM-ID. FIRSTTRY.
GRAUER.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPLOYEE-FILE ASSIGN TO 'A:\CHAPTR02\FIRSTTRY.DAT'
ORGANIZATION IS LINE SEQUENTIAL.
PRINT-FILE
ASSIGN TO PRINTER.

FILE SECTION.
FD EMPLOYEE-FILE
RECORD CONTAINS 44 CHARACTERS
DATA RECORD IS EMPLOYEE-RECORD.
01 EMPLOYEE-RECORD.
05 EMP-NAME PIC X(25).
05 EMP-TITLE PIC X(10).
05 EMP-AGE PIC 99.
05 FILLER PIC XX.
05 EMP-SALARY PIC 9(5).
Chapter 1 — Introduction

i g u r e 1.12 COBOL Listing for Problem 8 (continued)

25 FD 4

26 RECORD CONTAINS 132 CHARACTERS


27 DATA RECORD IS PRINT-LINE.
28 01 PRINT-LINE.
29 05 FILLER PIC X.
30 05 PRINT-NAME 5
31 05 FILLER PIC X(2).
32 05 PRINT-AGE PIC 99.
33 05 FILLER PIC X(3).
34 05 PRINT-SALARY PIC 9(5).
35 05 FILLER PIC X(94).
36
37 6

38 01 END-OF-DATA-FLAG PIC X(3) 7


39 PROCEDURE DIVISION.
40 MAINLINE.
41 8 INPUT EMPLOYEE-FILE
42 OUTPUT PRINT-FILE.
43 MOVE SPACES TO PRINT-LINE.
44 MOVE 'SALARY REPORT FOR PROGRAMMERS UNDER 30' TO PRINT-LINE.
45 WRITE PRINT-LINE
46 AFTER ADVANCING 2 LINES.
47 READ EMPLOYEE-FILE
48 AT END MOVE 'YES' TO END-OF-DATA-FLAG
49 END-READ.
50 .9 PROCESS-EMPLOYEE-RECORDS
51 UNTIL END-OF-DATA-FLAG = 'YES'.
52 CLOSE EMPLOYEE-FILE
53 PRINT-FILE.
54 STOP RUN.
55
? 0
56 PROCESS-EMPLOYEE-RECORDS. ^'
1
57 IF EMP-TITLE = PROG RAMMER 'JMSTMP-AGE < 30
58 MOVE SPACES TO PRINTLINE
59 MOVE EMP-NAME ,10'PRINT-NAME
60 MOVE " TO PRINT-AGE
61 MOVE EMP-SALARY TO PRINT-SALARY
62 WRITE PRINT-LINE
63 END-IF.
?
64 READ EMPLOYEE-FILE '
65 AT END MOVE "TO END-OF-DATA-FLAG
66 END-READ.
C h a p t e r 4 The Identification, Environment, and Data Divisions

a n d t h u s r e d u c i n g t h e n u m b e r of t i m e s t h e i n p u t / o u t p u t d e v i c e is a c c e s s e d . In
o t h e r w o r d s , it is m o r e efficient t o a c c e s s a disk o n c e a n d r e a d a b l o c k c o n t a i n i n g 10
r e c o r d s , t h a n it is t o a c c e s s t h e disk 10 t i m e s a n d r e a d e a c h r e c o r d individually. T h e
blocking factor is d e f i n e d as t h e n u m b e r o f logical records in a physical record. T h e
c o n c e p t is illustrated i n Figure 4.2 w h e r e t h e r e c o r d s o f F i g u r e 4 . 2 a are u n b l o c k e d ,
w h e r e a s t h o s e i n F i g u r e s 4 . 2 b a n d 4 . 2 c h a v e b l o c k i n g factors o f 2 a n d 3, r e s p e c t i v e l y .
T h e h i g h e r t h e b l o c k i n g factor, t h e f e w e r t h e n u m b e r o f p h y s i c a l r e c o r d s , a n d
t h e m o r e efficient t h e p r o c e s s i n g . T h u s , t h e b l o c k i n g factor s h o u l d a l w a y s b e a s
h i g h as p o s s i b l e , w i t h i n the l i m i t a t i o n s of the p h y s i c a l d e v i c e . T h e actual
d e t e r m i n a t i o n o f t h e b l o c k i n g factor n e e d n o t c o n c e r n u s n o w ; w h a t is i m p o r t a n t is
t h e i m p l e m e n t a t i o n o f b l o c k i n g i n a COBOL p r o g r a m .
A s s u m e , for e x a m p l e , a b l o c k i n g factor o f 5, w i t h t h e a s s o c i a t e d entry, BLOCK
C O N T A I N S 5 RECORDS. T h e initial e x e c u t i o n o f t h e READ s t a t e m e n t p l a c e s a b l o c k
o f 5 logical r e c o r d s i n m e m o r y , w i t h o n l y t h e first r e c o r d a v a i l a b l e t o t h e p r o g r a m .
T h e s e c o n d (third, fourth, a n d fifth) e x e c u t i o n o f t h e READ s t a t e m e n t m a k e s a n e w
logical record available, without a c o r r e s p o n d i n g physical o p e r a t i o n taking place.
In s i m i l a r f a s h i o n t h e sixth e x e c u t i o n o f t h e READ s t a t e m e n t will b r i n g a n e w
p h y s i c a l r e c o r d i n t o t h e I / O area, w i t h n e w l o g i c a l r e c o r d s m a d e available o n t h e
s e v e n t h t h r o u g h t e n t h e x e c u t i o n s o f t h e READ s t a t e m e n t . All o f t h i s is a u t o m a t i c a l l y
d o n e for t h e p r o g r a m m e r as l o n g a s t h e BLOCK C O N T A I N S s t a t e m e n t is s p e c i f i e d in
t h e COBOL F D .
C h a p t e r 5 — The Procedure Division

Example 5.4 SUBTRACT A FROM B GIVING C

A B

B e f o r e e x e c u t i o n : [15] 100

A f t e r e x e c u t i o n : 10

T A B L E 5.8 The SUBTRACT Instruction

Value before execution 5 10 30 100

Value after execution of


S U B T R A C T A F R O M C. 5 10 25 100
SUBTRACT A B FROM C. 5 10 15 100
SUBTRACT A B FROM C GIVING D. 5 10 30 15
S U B T R A C T 10 F R O M C D . 5 10 20 90

MULTIPLY

T h e MULTIPLY statement has t w o formats:

[identi f i e r - 1 ! r ,- v,
MULTIPLY { \ BY {identifier-2 ROUNDEDU . . .
1 J
[literal-1 J - l J

[ON S I Z E E R R O R imperative-statement-l]

[END-MULTIPLY]

[identifier- l| [identifier-2|
MULTIPLY { } BY { }

[literal-1 J " [literal-2 J

GIVING |identifier-3 [RQUNDEP][ . . .

[ON S I Z E E R R O R imperative-statement-l]

[END-MULTIPLY]

If GIVING is used, then the result of the multiplication is stored in identifier-3


(and beyond). If GIVING is omitted, then the result is stored in identifier-2 (and
beyond).

Example 5.5 MULTIPLY A BY B

A B

B e f o r e e x e c u t i o n :
C h a p t e r 5 The Procedure Divisio

P R O G R A M M I N C T I P

The COMPUTE statement should always be used when multiple arithmetic operators are involved.
Consider two sets of equivalent code:

MULTIPLY B BY B GIVING B-SQUARED.


MULTIPLY 4 BY A GIVING FOUR-A.
MULTIPLY FOUR-A BY C GIVING FOUR-A-C.
SUBTRACT FOUR-A-C FROM B-SQUARED GIVING RESULT-1.
COMPUTE RESULT-2 = RESULT-1 ** .5.
SUBTRACT B FROM RESULT-2 GIVING NUMERATOR.
MULTIPLY 2 BY A GIVING DENOMINATOR.
DIVIDE NUMERATOR BY DENOMINATOR GIVING X.

COMPUTE X = (-B + (B ** 2 - (4 * A * C)) ** .5) / (2 * A).

Both sets of code apply to the quadratic formula,

•B + 4¥~-4AC

It is fairly easy to determine what is happening from the single COMPUTE statement, but next to
impossible to realize the cumulative effect of the eight arithmetic statements. Interpretation of the unacceptable
code is further clouded by the mandatory definition of data names for intermediate results, RESULT-1,
RESULT-2, etc.
Parentheses are often required in COMPUTE statements to alter the normal hierarchy of operations; for
example, parentheses are required around 2 * A in the denominator. If they had been omitted, the numerator
would have been divided by 2 and then the quotient would have been multiplied by A. Sometimes the
parentheses are optional to the compiler but should be used to clarify things for the programmer. The
parentheses around 4 * A * C do not alter the normal order of operations and hence are optional.
Individual arithmetic statements are preferable to the COMPUTE statement when only a single operation
is required. Hence, ADD 1 TO COUNTER is easier to read than COMPUTE COUNTER = COUNTER + 1.
unaffected. E x a m p l e 5.9 parallels 5.8 except that B Y replaces I N T O , resulting in a
quotient of zero a n d a remainder of 10. Table 5.10 contains additional examples of
the D I V I D E statement.
Example 5.7 DIVIDE A INTO B.

10| [501

I5I

Example 5.8 DIVIDE A INTO B GIVING C REMAINDER D.

|10| |51| } 13 j
|10! [51I 5 I

Example 5.9 DIVIDE A BY B GIVING C REMAINDER D.

,10, \Sl\

The DIVIDE Instruction

Value before execution 10 30


Value after execution of
5 30
DIVIDE 2 INTO B.
10 5
DIVIDE 2 INTO B GIVING C.
10 30
D I V I D E S B Y 5 GIVING A.
DIVIDE A INTO B C. 2 6
DIVIDE A INTO B GIVING C. 10 2
DIVIDE 3 INTO A GIVING B REMAINDER C. 1 2

Arithmetic is performed o n decimal as well as integer fields. Y o u m u s t b e aware of


the decimal point, a n d in particular, be sure to define the field holding the result with
a sufficient number of decimal places. Consider E x a m p l e 5.10, in w h i c h A a n d B have
pictures of 99 a n d 99V9, respectively.

Example 5.10 ADD A TO B.


Chapter 6 — Debugging

•"igure s„1 (continued)

93
94 01 DASH-LINE.
95 05 FILLER PIC X(31)VALUE SPACES.
96 05 FILLER PIC X(8) VALUE ALL
97 05 FILLER PIC X(2) VALUE SPACES.
98 05 FILLER PIC X(8) VALUE ALL
99 05 FILLER PIC X(2) VALUE SPACES.
100 05 FILLER PIC X(7) VALUE ALL '-'.
101 05 FILLER PIC X(6) VALUE SPACES.
102 05 FILLER PIC X(7) VALUE ALL
103 05 FILLER PIC X(5) VALUE SPACES.
104 05 FILLER PIC X(7) VALUE ALL '-'.
105 05 FILLER PIC X(49) VALUE SPACES.
106
107 01 TOTAL-LINE.
108 05 FILLER PIC X(8) VALUE SPACES.
109 05 FILLER PIC X(17)
110 VALUE 'UNIVERSITY TOTALS'.
ntf' y /Q\
111 05 FILLER Tit A\Oj VALUE SPACES.
112 05 TOT-TUITION PIC 9(6).
113 05 FILLER PIC X(6) VALUE SPACES.
114 05 TOT-UNION-FEE PIC 9(4).
115 05 riLLcn PIC X(5) VALUE SPACES.
116 05 TOT-ACTIVITY-FEE PIC 9(4).
117 05 FILLER PIC X(7) VALUE SPACES.
118 05 TOT-SCHOLARSHIP PIC 9(6).
119 05 FILLER PIC X(6) VALUE SPACES.
120 05 TOT-IND-BILL PIC 9(6).
121 05 FILLER PIC X(49) VALUE SPACES.
122
123 PROCEDURE DIVISION. Reserved •;•>vd use f
- J i
124 START. " "
125 OPEN INPUT STUDENT-FILE
126 OUTPUT PRINT-FILE.
127 PERFORM WRITE-HEADING-LINE.
128 PERFORM READ-STUDENT-FILE.
129 PERFORM PROCESS-STUDENT-RECORD
130 UNTIL DATA-REMAINS-SWITCH = 'NO'.
131 PERFORM WRITE-UNIVERSITY-TOTALS.
132 CLOSE STUDENT-FILE
133 PRINT-FILE.
n
i i
STOP fti
m i

135
136 WRITE-HEADING-LINE.
137 MOVE HEADING-LINE TO PRINT -LINE.
138 WRITE PRINT-LINE
139 AFTER ADVANCING PAGE.
140 MOVE SPACES TO PRINT-LINE.
141 WRITE PRINT-LINE.
142

i
C h a p t e r 6— Debugging

(continued)

11 ASSIGN TO PRINTER.
12
13 DATA DIVISION.
14 FILE SECTION.
15 FD STUDENT-FILE
16 RECORD CONTAINS 27 CHARACTERS.
17 01 STUDENT-RECORD.
18 05 STU-NAME.
19 10 STU-LAST-NAME PIC X(15)
20 10 STU-INITIALS PIC XX.
21 05 STU-CREDITS PIC 9(2).
22 05 STU-UNION-MEMBER PIC X.
23 05 STU-SCHOLARSHIP PIC 9(4).
24 05 STU-GPA - - — _ _

25
26 FD PRINT-FILE
27 RECORD CONTAINS 132 CHARACTERS.
28 01 PRINT-LINE PIC X(132).
on
30 WORKING-STORAGE SECTION.
31 01 DATA-REMAINS-SWITCH PIC X(2) VALUE SPACES
32
33 01 INDIVIDUAL-CALCULATIONS.
34 05 IND-TUITION PIC 9(4) VALUE ZEROS.
35 05 IND-ACTIVITY-FEE PIC 9(2) VALUE ZEROS.
36 05 IND-UNION-FEE PIC 9(2) VALUE ZEROS.
37 05 IND-SCHOLARSHIP PIC 9(4) VALUE ZEROS.
38 05 IND-BILL PIC 9(6) VALUE ZEROS.
39
40 01 UNIVERSITY-TOTALS.
41 05 UNI-TUITION PIC 9(6) VALUE ZEROS.
42 05 UNI-UNION-FEE PIC 9(4) VALUE ZEROS.
43 05 UNI-ACTIVITY-FEE PIC 9(4) VALUE ZEROS.
44 05 UNI-SCHOLARSHIP PIC 9(6) VALUE ZEROS.
45 05 UNI-IND-BILL PIC 9(6) VALUE ZEROS.
46
47 01 CONSTANTS-AND-RATES.
48 05 PRICE-PER-CREDIT PIC 9(3) VALUE 200.
49 05 UNION-FEE PIC 9(2) VALUE 25.
50 05 ACTIVITY-FEES.
51 10 1ST-ACTIVITY-FEE PIC 99 VALUE 25.
52 10 IST-CREDIT-LIMIT PIC 99 VALUE 6.
53 10 2ND-ACTIVITY-FEE PIC 99 VALUE 50.
54 10 2ND-CREDIT-LIMIT PIC 99 VALUE 12.
55 10 3RD-ACTIVITY-FEE PIC 99 VALUE 75.
C h a p t e r 6 — Debugging

lie
After c o r r e c t i n g t h e file n a m e , w e r e c o m p i l e d t h e p r o g r a m a n d r a n it a g a i n , this
t i m e w i t h t h e r e s u l t s i n Figure 6.5. F i g u r e 6.5a s h o w s a n o t h e r c o m m o n RTS error,
"Illegal Character i n N u m e r i c Field." This error a l m o s t a l w a y s c o m e s f r o m h a v i n g
s p a c e s i n a n u m e r i c field. O n e c a u s e o f t h o s e s p a c e s i s w h e n t h e p r o g r a m r e a d s a
d a t a file that a c t u a l l y h a s s p a c e s i n t h e field. I n o t h e r w o r d s , if t h e field h a s a
PICTURE o f 9(5) a n d t h e actual c o n t e n t s are " 123", t h e p r o g r a m will fail if it tries t o
u s e this field i n a c o m p u t a t i o n . T h i s p r o b l e m i s a d a t a p r o b l e m rather t h a n a
p r o g r a m p r o b l e m . W h e n y o u c r e a t e test data, b e s u r e t o t y p e i n l e a d i n g z e r o s for
a n y n u m e r i c fields. I n t h i s c a s e t h e field s h o u l d h a v e b e e n "00123".
A s e c o n d r e a s o n for s p a c e s i n a n u m e r i c field is w h e n t h e p r o g r a m a t t e m p t s t o
r e a d b e y o n d t h e e n d o f a file. T h e i n c o r r e c t p l a c e m e n t o f t h e READ i n l i n e 149 o f
F i g u r e 6 . 3 c a u s e s this c o n d i t i o n . Figure 6 . 5 b s h o w s t h e l i n e w h e r e t h e error w a s
detected. W e clicked o n e a c h data n a m e in t h e statement, a n d t h e Animator
s h o w e d u s t h e c u r r e n t c o n t e n t s o f t h e field. S T U - C R E D I T S c o n t a i n s o n l y s p a c e s
a n d c a u s e d t h e error. I n this c a s e , w h e n t h e p r o g r a m r e a d b e y o n d t h e e n d o f t h e
file, COBOL i n s e r t e d s p a c e s i n t o S T U D E N T - R E C O R D (line 17), i n c l u d i n g S T U -
CREDITS. T o c o r r e c t t h e p r o b l e m w e r e s t o r e d t h e READ t o t h e e n d o f PROCESS-
S T U D E N T - R E C O R D , r e c o m p i l e d t h e p r o g r a m , a n d reran it. T h i s a c t i o n c o r r e c t s
t h e r u n - t i m e errors, b u t t h e r e are still l o g i c errors t o d e a l w i t h .

fare 6.5 Illegal Character Run Time Error

Illegal character in numeri


c field (Error 163)

) OK j

in time error messaoe

— — 2
162 COHJ
U' II . rt>i f ! U N .
ffi3
>(,<
ICS COHP-JIL UNION F i t .
1S>6 Si' STl) UNI ON- MF
16? HO BE Z E R O 10
1&« E L SE
16} M OU f UNION-F
m EHD-IF.
J72 court) r* ficnuitv-pe j|A»piyi A(Ml*l!tii Manllorj !i:jst!
l?« mi-H SIU-CREDITS
EUALUAIE TRU E <-- 1 S T CREDIT-LIMIT J
rtOUi: iST-BCTIUIIi-FEE TO IND OCTIUI
!V4 WH
EN STU-CREDITS > 1ST CRKDIT I.IfllT
iVT.
i n
C h a p t e r 6 Debugging

3. Do you agree with the authors' suggestions for successful walkthroughs? Are there
any guidelines you wish to add to the list? To remove from the iist?
4. Identify the syntactical errors in the COBOL fragment in Figure 6.8.
5. Identify the logical errors in the COBOL fragment in Figure 6.9. (Assume there are
no other READ statements in the program.)
6. The COBOL fragment in Figure 6.10a is taken from a program that compiled
cleanly but failed to execute. The error message is in Figure 6.10b. Explain
the problem.

COBOL Fragment for Problem 4

IDENTIFICATION DIVISION.
PROGRAM ID. ERRORS.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
SELECT EMPLOYEE-FILE ASSIGN TO 'A:\CHAPTR06\EMP.DAT'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD EMPLOYEE-FILE
RECORD CONTAINS 50 CHARACTERS
DATA RECORD IS EMPLOYEE-RECORD.
EMPLOYEE-RECORD.
05 EMP-NAME PIC X(20).
05 EMP-NUMBER PIC X(9).
05 FILLER PIC X(20).
WORKING STORAGE SECTION.
10 END-OF-FILE-SWITCH PIC X(3) VALUE BLANKS.

m m 6.8 COBOL Fragment for Problem 5

WORKING-STORAGE SECTION.
01 END-OF-FILE-SWITCH PIC X(3) VALUE 'YES'.

PROCEDURE DIVISION.
MAINLINE.

PERFORM PROCESS-RECORDS
UNTIL END-OF-FILE-SWITCH = 'YES'

PROCESS-RECORDS.
READ EMPLOYEE-FILE
AT END MOVE 'YES' TO END-OF-FILE-SWITCH
END-READ.
Chapter 7— Editing and Coding Standards

P R O G R A M M I N G T I P

Scope terminators are one of the most powerful enhancements in COBOL-85, and in the opinion of the
authors, justify in and of themselves, conversion to the new standard. In its simplest role a scope terminator is
used in place of a period to end a conditional statement—for example, END-IF to terminate an IF statement.
(A scope terminator and a period should not appear together unless the period also ends the sentence.)
One of the most important reasons for using scope terminators is that they eliminate the very subtle
column 73problem which has always existed, and which is depicted below. The intended logic is straightforward,
and is supposed to apply a discount of two percent on an order of $2,000 or more. The amount due (NET) is
equal to the amount ordered less the discount (if any).

IF AMOUNT-ORDERED-Til ISWEEK < 2000


MOVE ZEROS TO CUSI0MER-DISC0UNT
ELSE
COMPUTE CUSTOMER-DISCOUNT = AMOUNT-ORDERED-THISWEEK * .02,]..
I COMPUTEJET = ATOUNT-ORDERED-THISWEEK - CUSTOMER-DISCOUNT.\

Amount ordered Discount Net


3000 60 2940
4000 80 3920
1000 0 3920
5000 100 4900
1500 0 [ 49oo ]
The COBOL statements appear correct, yet the output is wrong! In particular, the net amounts are
wrong for any order less than $2,000 (but valid for orders of $2,000 or more). The net amount for orders less
than $2,000 equals the net for the previous order (that is, the net for an order of $1,000 is incorrectly printed as
$3,920, which was the correct net for the preceding order of $4,000). The net amount for an order of $1,500
was printed as $4,900, and so on. Why?
The only possible explanation is that the COMPUTE NET statement is not executed for net amounts less
than $2,000. The only way that can happen is if the COMPUTE NET statement is taken as part of the ELSE
clause, and that can happen only if the ELSE is not terminated by a period. The period is present, however, so
we are back at ground zero—or are we? The period is present, but in column 73, which is ignored by the
compiler. Hence the visual code does not match the compiler interpretation, and the resulting output is
incorrect. Replacing the period by the END-IF delimiter will eliminate this and similar errors in the future.
(Remember, a period may appear af the end of the sentence after the END-IF terminator.)
Chapter 7 — Editing and Coding Standard

(continued)

10 1ST-ACTIVITY-FEE PIC 99 VALUE 25.


10 IST-CREDIT-LIMIT PIC 99 VALUE 6.
10 2ND-ACTIVITY-FEE PIC 99 VALUE 50.
10 2ND-CREDIT-LIMIT PIC 99 VALUE 12.
10 3RD-ACTIVITY-FEE PIC 99 VALUE 75.
05 MINIMUM-SCHOLAR-GPA PIC 9V9 VALUE 2.5.

HEADING-LINE.
05 FILLER PIC X VALUE SPACES.
05 FILLER PIC X(12) VALUE 'STUDENT NAME'
05 FILLER PIC X(10) VALUE SPACES.
05 FILLER PIC X(7) VALUE 'CREDITS'.
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(7) VALUE 'TUITION' .
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(9) VALUE 'UNION FEE'.
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(7) VALUE 'ACT FEE'.
05 FILLER PIC X{2) VALUE SPACES.
05 FILLER PIC X(ll) VALUE 'SCHOLARSHIP'.
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(10) VALUE 'TOTAL BILL'.
05 FILLER PIC X(48) VALUE SPACES.

01 DETAIL-LINE.
05 FILLER PIC X VALUE SPACES.
05 DET-LAST-NAME PIC X(15).
05 FILLER PIC X(2) VALUE SPACES.
05 DET-INITIALS PIC X(2).
05 FILLER PIC X(5) VALUE SPACES.
05 DET-CREDITS PIC Z9.
05 FILLER PIC X(4) VALUE SPACES.
05 DET-TUITION PIC $$$$,$$9.
05 FILLER PIC X(6) VALUE SPACES.
05 DET-UNION-FEE PIC $$$9 BLANK WHEN ZERO
05 FILLER PIC X(5) VALUE SPACES.
05 DET-ACTIVITY-FEE PIC $$$9 BLANK WHEN ZERO
05 FILLER PIC X(6) VALUE SPACES.
05 DET-SCHOLARSHIP PIC $$,$$$9 BLANK WHEN ZERO
05 FILLER PIC X(4) VALUE SPACES.
05 DET-IND-BILL PIC $$$$,$$9CR.
05 r-r ri L tL rQ rjK
I
PIC X(47) VALUE SPACES.

DASH-LINE.
05 FILLER PIC X(31) VALUE SPACES.
05 FILLER PIC X(8) VALUE ALL '-'.
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(8) VALUE ALL
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(7) VALUE ALL '-'.
C h a p t e r 8 Data Validation

true or false. Four types of conditions are possible: relational, class, sign, a n d
condition-name, each of which is discussed m a separate section.

T h e relational condition is the m o s t c o m m o n type of condition a n d has appeared


throughout the book. A s y o u already k n o w there is considerable variation in the w a y
the relational operator m a y be expressed. In all instances, however, the condition
compares the quantities o n either side of the relational operator to determine
whether (or not) the condition is true.
T h e data type of the quantities being c o m p a r e d m u s t b e the same; for example,
a numeric data item m u s t be c o m p a r e d to a numeric literal a n d a n o n n u m e r i c data
item to a n o n n u m e r i c literal. Failure to d o so produces a syntax error during
compilation. T h e relational condition is illustrated in Figure 8.1.

The Relational Condition

IS NOT GREATER THAN


IS NOT >
IS ;NOT LESS THAN
IS NOT <
identifier-l identifier-2
IS [NOT EQUAL TO
1 i teral-2
IS NOTj =
expression-1 expression-2
IS NOT GREATER THAN OR EQUAL TO
IS NOT >=
IS NOT LESS THAN OR EQUAL TO
IS NOT <=

05 NUMERIC-FIELD PIC 9(5).


05 ALPHANUMERIC-FIELD PIC X(5).

IF NUMERIC-FIELD = 10 . . . (valid entry)


IF NUMERIC-FIELD = '10' . . . (invalid entry)
IF ALPHANUMERIC-FIELD = IO . . . (invalid entry)
IF ALPHANUMERIC-FIELD = '10' . . . (valid entry)
The Stand-Alone Edit Program

Hierarchy Chart for Validation Program

CREATE
VALID FILE

GET WRITE READ PROCESS


TODAYS ERROR RENTAL RENTAL
DATE HEADINGS RECORD RECORDS

VALIDATE WRITE READ


RENTAL VALID RENTAL
RECORD RECORD RECORD

VALIDATE VALIDATE VALIDATE VALIDATE VALIDATE VALIDATE VALIDATE


VALIDATE
CONTRACT CAR DATE DAYS MILES MILEAGE
NAME INSURANCE
NUMBER TYPE RETURNED RENTED DRIVEN RATE

WRITE WRITE WRITE WRITE WRITE WRITE WRITE V WRITE


ERROR LINE ERROR LINE ERROR LINE ERROR LINE ERROR LINE ERROR LINE ERROR LINE ERROR LINE

Data Validation Program

1 IDENTIFICATION DIVISION.
2 PROGRAM-ID. VALCARS8.
3 AUTHOR. CVV.
4
5 ENVIRONMENT DIVISION.
6 INPUT-OUTPUT SECTION.
7 FILE-CONTROL.
8 SELECT RENTAL-FILE ASSIGN TO 'A:\CHAPTR08\VALCARS.DAT'
9 ORGANIZATION IS LINE SEQUENTIAL.
10 SELECT VALID-RENTAL-FILE ASSIGN TO 'A:\CHAPTR08\VALRENT.DAT
11 ORGANIZATION IS LINE SEQUENTIAL.
12 SELECT ERROR-FILE
13 ASSIGN TO PRINTER.
14
15 DATA DIVISION.
16 FILE SECTION.
17 FD RENTAL-FILE
18 RECORD CONTAINS 56 CHARACTERS.
19 01 RENTAL-RECORD.
20 05 REN-CONTRACT-NO PIC 9(6).
21 05 REN-NAME.
22 10 REN-LAST-NAME PIC X(15).
23 10 REN-FIRST-NAME PIC X(10).
Chapter 8 Data Validation

4 Limitations of COBOL-74

ADD 1 TO
MALE-COUNTER

;hart

1
IF VALID-RECORD-SW = Y'
IF SEX = 'M'
ADD 1 TO MALE-COUNTER
END-IF
IF INCOME > 50000
ADD 1 TO HIGH-INCOME-CTR
END-IF
END-IF.
(b) C O B O L - 8 5

IF VALID-RECORD-SW = 'Y'
PERFORM DO-MORE-TESTS.

DO-MORE-TESTS.
IF SEX = 'M'
ADD 1 TO MALE-COUNTER.
IF INCOME > 50000
ADD 1 TO HIGH-INCOME-CTR.
Chapter 9 - More About the Procedure Division

T h e Data Division entries in Figure 9.9a contain several data n a m e s that appear in
both S T U D E N T - R E C O R D a n d P R I N T - L I N E — f o r example, C R E D I T S — a n d a n y
Procedure Division reference to C R E D I T S will produce a compiler error indicating a
nonunique data name. This is because the compiler cannot determine w h i c h
C R E D I T S (in S T U D E N T - R E C O R D or PRINT-LINE) is referenced. O n e solution is

F i g u r e 9.9 Duplicate Data Names

01 STUDENT-RECORD.
05 STUDENT-NAME PIC X(20).
05 SOCIAL-SECURITY-NUM PIC 9(9).
05 STUDENT-ADDRESS.
10 STREET PIC X(15).
10 CITY-STATE PIC X(15).
05 ZIP-CODE PIC X(5).
05 CREDITS PIC 9(3).
05 MAJOR PIC X(10).
05 FILLER PIC X(3).

PRINT-LINE.
10 STUDENT-NAME PIC X(20).
10 FILLER PIC XX.
10 CREDITS PIC ZZ9.
10 FILLER PIC XX.
10 TUITION PIC $$,$$9.99.
10 FILLER PIC XX.
10 STUDENT-ADDRESS.
15 STREET PIC X(15).
15 CITY-STATE PIC X(15).
15 ZIP-CODE PIC X(5).
10 FILLER PIC XX.
10 SOCIAL-SECURITY-NUM PIC 999B99B9999
10 FILLER PIC X(47).

| (a) Duplicate Data Marries


f

I
MOVE CORRESPONDING STUDENT-RECORD TO PRINT-LINE.

MOVE STUDENT-NAME OF STUDENT-RECORD


TO STUDENT-NAME OF PRINT-LINE.
MOVE SOCIAL-SECURITY-NUM OF STUDENT-RECORD
TO SOCIAL-SECURITY-NUM OF PRINT-LINE.
| MOVE STREET OF STUDENT-RECORD
I TO STREET OF PRINT-LINE.
MOVE CITY-STATE OF STUDENT-RECORD
TO CITY-STATE OF PRINT-LINE.
MOVE CREDITS OF STUDENT-RECORD
TO CREDITS OF PRINT-LINE.
Chapter 9 More About the Procedure Division

Program Design ..
T h e car billing p r o g r a m has two objectives: to complete the two-program sequence
b e g u n in Chapter 8 a n d to illustrate the Procedure Division statements presented in
this chapter. Both objectives impact the design of the p s e u d o c o d e a n d associated
hierarchy chart.
T h e hierarchy chart in Figure 9.11 is written without the priming read of
earlier programs. T h e highest-level m o d u l e , PREPARE-RENTAL-REPORT, has three
subordinates: G E T - T O D A Y S - D A T E , P R O C E S S - R E N T A L - R E C O R D S , a n d W R I T E -
R E N T A L - T O T A L S . P R O C E S S - R E N T A L - R E C O R D S in turn is the driving m o d u l e of
the p r o g r a m a n d performs four lower-level paragraphs: C O M P U T E - I N D T v T D U A L -
BILL, W R I T E - H E A D I N G - L I N E S , W R I T E - D E T A I L - L I N E , a n d I N C R E M E N T - R E N T A L -
T O T A L S . C O M P U T E - I N D I V I D U A L - B I L L has three subordinate modules, C O M P U T E -
MILEAGE-TOTAL, COMPUTE-DAILY-TOTAL, and C O M P U T E - I N S U R A N C E - T O T A L
to c o m p u t e the c o m p o n e n t s of a customer's bill.
T h e paragraph W R I T E - H E A D I N G - L I N E S is subordinate to P R O C E S S - R E N T A L -
R E C O R D S , w h i c h differs from a n earlier hierarchy chart (page 119) that placed the
heading routine o n a higher level. T h e earlier structure, however, produced only a
single heading at the start of processing, whereas the current requirement is to
produce a heading at the top of every page; hence the heading routine will be
executed several times a n d is subordinate to processing a record.
T h e pseudocode in Figure 9.12 takes advantage of the in-line perforin a n d
false-condition branch to eliminate the priming read used in earlier examples. T h e
pseudocode also implements the required page heading routine b y initializing the
line counter to six a n d testing its value prior to writing each detail line. T h e heading

9.11 Hierarchy Chart

PREPARE
RENTAL
REPORT

GET PROCESS WRITE


TODAYS RENTAL RENTAL
DATE RECORDS TOTALS

COMPUTE WRITE WRITE INCREMENT


INDIVIDUAL HEADING DETAIL RENTAL
BILL LINES LINE TOTALS

COMPUTE COMPUTE COMPUTE


MILEAGE DAILY INSURANCE
TOTAL TOTAL TOTAL
Chapter 9 — More About the Procedure Division

'igur® @„13 (continued)

101 05 FILLER PIC X(4) VALUE 'Date'.


102 05 FILLER PIC X(5) VALUE SPACES.
103 05 FILLER PIC X(3) VALUE 'Car'.
104 05 FILLER PIC X(3) VALUE SPACES.
105 05 FILLER PIC X(4) VALUE 'Days'.
106 05 FILLER PIC X(6) VALUE SPACES.
107 05 FILLER PIC X(6) VALUE 'Rental'.
108 05 FILLER PIC X(4) VALUE SPACES.
109 05 FILLER PIC X(5) VALUE 'Miles'.
110 05 FILLER PIC X(2) VALUE SPACES.
111 05 FILLER PIC X(7) VALUE 'Mileage'.
112 05 FILLER PIC X(2) VALUE SPACES.
113 05 FILLER PIC X(7) VALUE 'Mileage'.
114 05 FILLER PIC X(2) VALUE SPACES.
115 05 FILLER PIC X(9) VALUE 'Insurance'.
116 05 FILLER PIC X(6) VALUE SPACES.
117 05 FILLER PIC X(6) VALUE 'Amount'.
118 05 FILLER PIC X(5) VALUE SPACES.
119
120 01 HEADING-LINE-THREE.
121 05 FILLER PIC X VALUE SPACES.
122 05 FILLER PIC X(6) VALUE ' Number
123 05 FILLER PIC X(4) VALUE SPACES.
124 05 FILLER PIC X(4) VALUE 'Name'.
125 05 FILLER PIC X(29) VALUE SPACES
126 05 FILLER PIC X(8) VALUE 'Return
127 05 FILLER PIC X(2) VALUE SPACES.
128 05 FILLER PIC X(4) VALUE 'Type'.
129 05 FILLER PIC X(2) VALUE SPACES.
130 05 FILLER PIC X(6) VALUE 'Rented
131 05 FILLER PIC X(6) VALUE SPACES.
132 05 FILLER PIC X(5) VALUE 'Total'
133 05 FILLER PIC X(3) VALUE SPACES.
134 05 FILLER PIC X(6) VALUE 'Driven
135 05 FILLER PIC X(4) VALUE SPACES.
136 05 FILLER PIC X(4) VALUE 'Rate'.
137 05 FILLER PIC X(4) VALUE SPACES.
138 05 FILLER PIC X(5) VALUE 'Total'
139 05 FILLER PIC X(6) VALUE SPACES.
140 05 FILLER PIC X(5) VALUE 'Total'
141 05 FILLER PIC X(9) VALUE SPACES.
142 05 FILLER PIC X(3) VALUE 'Due'.
143 05 FILLER PIC X(6) VALUE SPACES.
144
145 01 DETAIL-LINE. le INSPtCT
146 [~05 P E T - C O N T R A C T - N O P i £ " 9 B 9 9 9 B 9 9 . \
147 05 FILLER " " " " ~ PIC X(3)" VALUE SPACES.
148 05 DET-NAME PIC X(30).
149 05 FILLER PIC X(3) VALUE SPACES.
150 05 DET-RETURN-DATE PIC X(8).
Chapter 10 — Screen I-

(coniinued)

i
] DO UNTIL valid-field-switch = spaces
| Accept last-name
| J {— IF last-name = spaces
j | Display 'Error - Missing last name'
| j Move 'NO' to valid-field-switch
j I ELSE
S I Move spaces to valid-field-switch
j | — ENDIF
I ENDDO
I . . . Validation checks for remaining fields
| Display information correct message
I p - DO UNTIL valid confirmation ("Y", "y", "N", or "n")
j Accept confirm-switch
I '— IF valid confirmation
Clear previous error message
ELSE
I Display 'Must be "Y" or "N"'
| L . ENDIF
! | — ENDDO
l
- ENDDO
Compute miles driven = miles in - miles out
r - DO CASE
| Car Type E - Move economy rate to mileage rate
Car lype C - Move compact rate to mileage rate
Car Type M - Move midsize to mileage rate
Car Type F - Move full size rate to mileage rate
Car Type L - Move luxury rate to mileage rate
L - END CASE
Compute mileage total = miles driven * mileage rate
Compute daily total = days rented * daily rate
— IF insurance taken
Compute insurance = insurance rate * days rented
—• END-IF
Compute total bill = mileage amount + daily amount + insurance
Display computed bill
Write valid record to valid record file
Display Another record message
— DO UNTIL valid confirmation ("Y", " y " , "N", or "n")
Accept confirm-switch
— IF valid confirmation
Clear previous error message
f ELSE
| Display 'Must be "Y" or "N"'
I — ENDIF
I— ENDDO
- ENDDO
Close valid-rental-file
Stop run
Chapter 10 — Screen l-O

c (continued)

139 05 SCREEN-PROMPTS.
140 10 LINE 1 BLANK LINE BACKGROUND-COLOR MAGENTA. [
141 10 COLUMN 20 VALUE 'Mavis Car Rental Company' ,
142 BACKGROUND -COLOR MAGENTA
143 FOREGROUND -COLOR BRIGHT-GREEN.
144 10 SCR-DATE PIC X(8) FROM SCREEN-DATE
145 COLUMN 55 BACKGROUND-COLOR MAGENTA
146 FOREGROUND-COLOR BRIGHT-GREEN.
147 10 LINE 3 COLUMN 7 VALUE 'Contract No:'.
1
148 J 0 _ LINE 5 COLUMN 7 VALUE 'Customer Information: .
1
149 !
10 LINE 6 COLUMN 9 VALUE 'Last Name . ;
150 ! 10 COLUMN 25 VALUE 'First'. '
151 10 COLUMN 36 VALUE 'Initial'.
152 10 LINE 9 COLUMN 6 VALUE 'Car Information:'.
153 10 LINE 10 COLUMN 12 VALUE 'Type Code:'.
154 10 COLUMN 25
155 VALUE '(Compact, Economy, Midsize, Fullsize, Luxury)'
156 FOREGROUND -COLOR CYAN.
157 10 COLUMN 26 VALUE ' C HIGHLIGHT.
158 10 COLUMN 35 VALUE ' E' HIGHLIGHT.
159 10 COLUMN 44 VALUE 'M' HIGHLIGHT.
160 10 COLUMN 53 VALUE 'F' HIGHLIGHT.
161 10 COLUMN 63 VALUE 'L' HIGHLIGHT.
162 10 LINE 11 COLUMN 8 VALUE 'Date Returned:'.
163 10 COLUMN 23 VALUE 'mm/dd/yy'
164 FOREGROUND -COLOR BRIGHT-WHITE.
165 10 LINE 12 COLUMN 10 VALUE 'Days Rented:'.
166 10 LINE 13 COLUMN 10 VALUE 'Mileage:'.
167 10 LINE 14 COLUMN 13 VALUE 'Miles In:'.
168 10 LINE 15 COLUMN 12 VALUE 'Miles Out:'.
169 10 COLUMN 37 VALUE 'Mileage Rate:'.
170 10 LINE 16 COLUMN 12 VALUE ' Insurance:'.
171 10 COLUMN 25 VALUE '(Y/N)'
172 FOREGROUND--COLOR CYAN.
173
174 05 SCREEN-INPUTS.
175 10 SCR-CONTRACT-NO PIC 9(6) USING REN-CONTRACT-NO
176 LINE 3 COLUMN 20 REVERSE-VIDEO.
177 10 SCR-LAST-NAME PIC X(15) USING REN-LAST-NAME
178 LINE 7 COLUMN 9 REVERSE-VIDEO.
179 10 SCR-FIRST-NAME PIC X( 10) USING REN-FIRST-NAME
180 LINE 7 COLUMN 25 REVERSE-VIDEO.
181 10 SCR-INITIAL PIC X USING REN-INITIAL '
<
182 LINE 7 COLUMN 36 REVERSE-VIDEO.
183 10 SCR-CAR-TYPE PIC X USING REN-CAR-TYPE
184 LINE 10 COLUMN 23 REVERSE-VIDEO AUTO.
185 10 SCR-RETURNED-MONTH PIC 99 USING REN-RETURNED-MONTH
186 LINE 11 COLUMN 23 REVERSE-VIDEO AUTO. t

187 10 SCR-RETURNED-DAY PIC 99 USING REN-RETURNED-DAY >


188 LINE 11 COLUMN 26 REVERSE-VIDEO AUTO.
Chapter 11 — Introduction to Tables

F i g u r e 11.8 Test Data and Required Output

BENJAMIN, L 05111A3222A2333A3444A3555B3
BORROW, J 04666B3777B3888B3999B4
MILGROM, M 06123C4456C4789C4012C4345C3678C4
(a) Test Data

NAME:BENJAMIN, L OFFICIAL TRANSCRIPT

COURSE # CREDITS GRADE


111 3 A
222 2 A
333 3 A
444 3 A
555 3 B

AVERAGE: 3.79 *DEANS LIST*

NAME:BORROW, J OFFICIAL TRANSCRIPT

COURSE # CREDITS GRADE


666 3 B
777 3 B
888 3 B
999 4 B

AVERAGE: 3.00

NAME:MILGROM, M OFFICIAL TRANSCRIPT

COURSE # CREDITS GRADE


123 4 C
456 4 C
789 4 C
012 4 C
345 3 C
678 4 C

AVERAGE: 2.00
(b) individual Transcripts

STUDENTS ON THE DEANS LIST

TOTAL TOTAL QUALITY


NAME COURSES CREDITS POINTS GPA

BENJAMIN, L 5 14 53 3.79

(c) The Dean's List


Indexes versus Subscripts

Indexes versus Subscripts (Subscripts)

05 ST-NUMBER-OF-COURSES PIC 99
05 ST-COURSE-INFO OCCURS 1 TO 8 TIMES
DEPENDING ON ST-NUMBER-OF-COURSES.
10 ST-COURSE-NUMBER PIC X(3).
10 ST-COURSE-GRADE PIC X.
10 ST-COURSE-CREDITS PIC 99

^-Subscript de--'' scga:e'i:••</ ,n Working-Sioi

05 COURSE-SUBSCRIPT PIC S9(4) COMP. r

(a) Table Definition

PERFORM WRITE-COURSE-DATA
VARYING COURSE-SUBSCRIPT FROM 1 BY 1
UNTIL COURSE-SUBSCRIPT > ST-NUMBER-OF-COURSES.

WRITE-COURSE-DATA.
MOVE ST-COURSE-NUMBER (COURSE-SUBSCRIPT) TO PL-NUMBER.
MOVE ST-COURSE-GRADE (COURSE-SUBSCRIPT) TO PL-GRADE.
WRITE PRINT-LINE FROM PRINT-LINE-ONE
AFTER ADVANCING 1 LINE.

fb) PERFORM V A R Y I N G

MOVE 1 TO COURSE-SUBSCRIPT.
PERFORM WRITE-COURSE-DATA ST-NUMBER-OF-COURSES TIMES.

WRITE-COURSE-DATA.
MOVE ST-COURSE-NUMBER (COURSE-SUBSCRIPT) TO PL-NUMBER.
MOVE ST-COURSE-GRADE (COURSE-SUBSCRIPT) TO PL-GRADE.
WRITE PRINT-LINE FROM PRINT-LINE-ONE
AFTER ADVANCING 1 LINE.
ADD 1 TO COURSE-SUBSCRIPT.

(c) P E R F O R M T I M E S
Chapter 11 Introduction to Tables

Indexes versus Subscripts (Indexes)

05 ST-NUMBER-OF-COURSES PIC 99.


05 ST-C0URSE-INF0 OCCURS 1 TO 8 TIMES
DEPENDING ON ST-NUMBER-OF-COURSES
INDEXED BY COURSE-INDEX.3'"""""
ToST-COURSE-NUMBER"PIC X(3).
10 ST-COURSE-GRADE PIC X.
10 ST-COURSE-CREDITS PIC 99.

PERFORM WRITE-COURSE-DATA
VARYING COURSE-INDEX FROM 1 BY 1
UNTIL COURSE-INDEX > ST-NUMBER-OF-COURSES.

WRITE-COURSE-DATA.
MOVE ST-COURSE-NUMBER (COURSE-INDEX) TO PL-NUMBER.
MOVE ST-COURSE-GRADE (COURSE-INDEX) TO PL-GRADE.
WRITE PRINT-LINE FROM PRINT-LINE-ONE
AFTER ADVANCING 1 LINE.

(b) P E R F O R M V A R Y I N G

Index initialized by a SETeta:eniei-i


SET COURSE-INDEX TO 1.] " "
PERFORM WRITE-COURSE-DATA ST-NUMBER-OF-COURSES TIMES.

WRITE-COURSE-DATA.
MOVE ST-COURSE-NUMBER (COURSE-INDEX) TO PL-NUMBER.
MOVE ST-COURSE-GRADE (COURSE-INDEX) TO PL-GRADE.
WRITE PRINT-LINE FROM PRINT-LINE-ONE
AFTER ADVANCING 1 LINE.
SET COURSE-INDEX UP BY 1.

(c) P E R F O R M T I M E S
COBOL-85 introduced several minor changes in conjunction with table
processing. The new compiler allows seven levels of subscripting as opposed
to the earlier limit of three, but given that the typical programmer seldom uses
three-level tables, this extension is of little practical benefit. (Multiple-level
tables are covered in Chapter 13.) The OCCURS DEPENDING ON clause
may specify a value of zero, whereas at least one occurrence was required in
COBOL-74.
A more significant change is the introduction of relative subscripting
(as explained in Figure 11.6), enabling the reference DATA-NAME
(SUBSCRIPT + integer). Relative subscripting was not permitted in COBOL-
74 (although relative indexing was).

A table is a grouping of similar data whose values are stored in contiguous


storage locations and assigned a single name. Tables are implemented in
COBOL through the OCCURS clause with subscripts or indexes used to
reference individual items in a table. The OCCURS DEPENDING ON clause
implements a variable-length table.

An index is conceptually the same as a subscript but provides more


efficient object code. Indexes are manipulated with the SET statement,
whereas subscripts are initialized with a MOVE statement and incremented
with an ADD statement.

The PERFORM VARYING statement manipulates an index or a subscript to


execute a procedure or series of in-line statements. Omission of the TEST
BEFORE and TEST AFTER clauses defaults to TEST BEFORE and
corresponds to the COBOL-74 implementation.

The PERFORM TIMES statement also provides for repeated execution of a


procedure or in-line statement, but requires the programmer to explicitly
vary the value of the subscript or index.

The optional USAGE IS COMPUTATIONAL clause is used to improve


the efficiency of a program's generated object code, but does not affect
its logic.
Chapter 12 — Table Lookup

P R O G R A M M I N G T I P

Data names defined as switches and/or subscripts should be restricted to a single use. Consider:
Poor Code

01 SUBSCRIPT PIC S9(4) COMP.


01 EOF-SWITCH PIC X(3) VALUE SPACES.

PERFORM INITIALIZE-TITLE-FILE
UNTIL EOF-SWITCH = 'YES'.
MOVE SPACES TO EOF-SWITCH.
PERFORM PROCESS-EMPLOYEE-RECORDS
1
UNTIL EOF-SWITCH = 'YES .
PERFORM COMPUTE-SALARY-HISTORY
VARYING SUBSCRIPT FROM 1 BY 1
UNTIL SUBSCRIPT > 3.
PERFORM FIND-MATCH-TITLE
VARYING SUBSCRIPT FROM 1 BY 1
UNTIL SUBSCRIPT > 100.

improved ooae

01 PROGRAM-SUBSCRIPTS.
05 TITLE-SUBSCRIPT PIC S9(4) COMP.
05 SALARY-SUBSCRIPT PIC S9(4) COMP.
01 END-OF-FILE-SWITCHES.
05 END-OF-TITLE-FILE-SWITCH PIC X(3) VALUE SPACES.
05 END-OF-EMPLOYEE-FILE-SWITCH PIC X(3) VALUE SPACES.

PERFORM INITIALIZE-TITLE-FILE
UNTIL END-OF-TITLE-FILE-SWITCH = 'YES'.
PERFORM PROCESS-EMPLOYEE-RECORDS
UNTIL END-OF-EMPLOYEE-FILE-SWITCH = 'YES'.
PERFORM COMPUTE-SALARY-HISTORY
VARYING SALARY-SUBSCRIPT FROM 1 BY 1
UNTIL SALARY-SUBSCRIPT > 3.
PERFORM FIND-MATCHING-TITLE
VARYING TITLE-SUBSCRIPT FROM 1 BY 1
UNTIL TITLE-SUBSCRIPT > 100.

At the very least, the improved code offers superior documentation. By restricting data names to a single
use, one automatically avoids such nondescript entries as EOF-SWITCH or SUBSCRIPT. Of greater impact,
the improved code is more apt to be correct in that a given data name is modified or tested in fewer places
within a program. Finally, if bugs do occur, the final values of the unique data names (TITLE-SUBSCRIPT and
SALARY-SUBSCRIPT) will be of much greater use than the single value of SUBSCRIPT.
Chapter 12 — Table Lookups

^i"' (continued)

115 05 DET-SALARY PIC $$$$,$$$.


116 05 PIC XX VALUE SPACES.
117 05 DET-INSURANCE PIC $$$$,$$$.
118 05 PIC X(47) VALUE SPACES.
119
120 01 WS-EMPLOYEE-RECORD.
121 05 EMP-NAME PIC X(20).
122 05 EMP-TITLE-CODE PIC X(4).
123 05 EMP-LOC-CODE PIC X(3).
124 05 EMP-EDUC-CODE PIC 9.
125 05 EMP-SALARY PIC 9(6).
126
127 01 WS-TITLE-RECORD.
128 05 TITLE-IN-CODE PIC X(4).
129 05 TITLE-IN-NAME PIC X(15).
130
131 PROCEDURE DIVISION.
132 100 -PRODUCE-EMPLOYEE-REPORT.
1 ?i
"PERFORM 200- IN ITIALIZE -TIT LE - TAB LE 7h
134 OPEN INPUT EMPLOYEE-FILE
135 OUTPUT PRINT-FILE.
136 PERFORM 300-WRITE-HEADING-LINES.
137 PERFORM UNTIL END-OF-EMP-FILE = 'YES'
138 READ EMPLOYEE-FILE INTO WS-EMPLOYEE-RECORD
139 AT END
140 MOVE 'YES' TO END-OF-EMP-FILE
141 NOT AT END
142 PERFORM 400-PROCESS-EMPLOYEE-RECORDS
143 END-READ
144 END-PERFORM.
145 CLOSE EMPLOYEE-FILE
146 PRINT-FILE.
147 STOP RUN.
148
149 200 -INITIALIZE-TITLE-TABLE.
150 OPEN INPUT TITLE-FILE.
151 PERFORM VARYING TITLE-INDEX FROM 1 BY 1
152 UNTIL END-OF-TITLE-FILE = 'YES' -Checks tlx
153 [OR TITLE-INDEX > 999i
154 READ TITLE-FILE INTO WS-TITLE-RECORD
155 AT END
156 MOVE 'YES' TO END-OF-TITLE-FILE
157 NOT AT END
158 ADD 1 TO NUMBER-OF-TITLES
159 MOVE TITLE-IN-CODE TO TITLE-CODE (TITLE-INDEX)
160 MOVE TITLE-IN-NAME TO TITLE-NAME (TITLE-INDEX)
161 END-READ
162 END-PERFORM.
163 IF TITLE-INDEX > 999
164 DISPLAY 'SIZE OF TITLE TABLE IS EXCEEDED'
C h a p t e r 14 Sortinc

(continued)

05 DET-L0CATI0N PIC X(15).


05 FILLER PIC X(3) VALUE SPACES.
05 DET-NAME PIC X(15).
05 FILLER PIC X(2) VALUE SPACES.
05 DET-ACCOUNT-NUMBER PIC 9(6).
05 FILLER PIC X(5) VALUE SPACES.
05 DET-SALES PIC $Z,ZZ9-.
05 FILLER PIC X(7) VALUE SPACES.
05 DET-COMMISSION PIC $Z,ZZ9-.
05 FILLER PIC X(50) VALUE SPACES.

01 COMPANY-TOTAL-LINE.
05 FILLER PIC X(31) VALUE SPACES.
05 FILLER PIC X(25)
VALUE COMPANY TOTAL
05 COMPANY-SALES-TOTAL PIC $Z(3),ZZ9-.
05 FILLER PIC X(5) VALUE SPACES.
05 COMPANY-COMM-TOTAL PIC $Z(3),ZZ9-.
05 FILLER PIC X(51) VALUE SPACES.

PROCEDURE DIVISION.
0000-SORT-SALES-RECORDS.

DESCENDING KEY SORT-COMMISSION


INPUT PROCEDURE 100-CALCULATE-COMMISSION
OUTPUT PROCEDURE 200-PREPARE-COMMISSION-REPORT.
STOPRUN.

|100-CALCULATE-COMMISSION.
' OPlN^NPUrSALES^FILE^ INPUT zDUF
PERFORM UNTIL NO-DATA-REMAINS
READ SALES-FILE INTO SALES-RECORD-IN
AT END
MOVE 'NO' TO DATA-REMAINS-SWITCH
NOT AT END
COMPUTE SR-COMMISSION ROUNDED =
SR-SALES * SR-COMMISSION-PERCENT
SIZE ERROR DISPLAY 'ERROR ON COMMISSION FOR
SR-NAME
END-COMPUTE /'
I IF SR-COMMISSION > 100
RELEASE SORT-RECORD FROM SALES-RECORD-IN
END-IF
END-READ
END-PERFORM.
CLOSE SALES-FILE.

! 200-PREPARE-COMMISSION-REPORT.
OPEN OUTPUT PRINT-FILE. ::EDUR
PERFORM 230-GET-TODAYS-DATE.
salesperson a n d then adding, or rolling, the salesperson total to the c o m p a n y total.
T h e latter is m o r e efficient in that fewer additions are performed.
Similar reasoning applies to the two-level report of Figure 15.3b, in w h i c h the
location total c a n be c o m p u t e d two different w a y s — b y adding the value of each
incoming transaction to a running location total, or b y waiting for a control break
o n salesperson, then rolling the salesperson total to the location total. In similar
fashion, the c o m p a n y total m a y be obtained in three ways. First, b y adding the
value of every incoming transaction to a running c o m p a n y total. Second, b y rolling
the salesperson total into the c o m p a n y total after a one-level break o n salesperson.
O r third, b y rolling the location total into the c o m p a n y total after a two-level break
o n location. T h e third approach is the m o s t efficient.
Y o u should be able to extend this logic to the three-level report of Figure 15.4b,
w h i c h maintains a running total for each salesperson, then roils the salesperson
total into the location total (after a break o n salesperson), rolls the location total
into the region total (after a break o n location), a n d finally rolls the region total into
the c o m p a n y total (after a break o n region).

Three-Level Control Break

000069 BENWAY 023q 10 CHICAGO MIDWEST


476530 BENWAY 023u 05 CHICAGO MIDWEST
988888 BENWAY 0450 01 CHICAGO MIDWEST
999340 BENWAY 0334 30 CHICAGO MIDWEST
000100 HUMMER OlOw 05 CHICAGO MIDWEST
649356 HUMMER 0345 05 CHICAGO MIDWEST
694446 HUMMER 0904 10 CHI C A M MIDWEST
203000 HAAS 8900 05 ST. LOUIS " MIDWEST
277333 HAAS 009x 08 ST. LOUIS MIDWEST
475365 HAAS 0333 05 ST. LOUIS MIDWEST
583645 KARLSTROM 0145 04 BALTIMORE NORTHEAST break
800396 KARLSTROM 3030 09 BALTIMORE NORTHEAST
700039 MARCUS 0932 10 BALTIMORE NORTHEAST
750020 MARCUS 0305 05 BALTIMORE NORTHEAST
444333 ADAMS lOOv 01 NEW YORK NORTHEAST
555666 ADAMS 2003 20 NEW YORK NORTHEAST
987654 ADAMS 2005 10 NEW YORK NORTHEAST
000101 CLARK 1500 10 TRENTON NORTHEAST
000104 CLARK 0500 03 TRENTON NORTHEAST
130101 CLARK 3200 20 TRENTON NORTHEAST
576235 CLARK 0100 03 TRENTON NORTHEAST
444444 FEGEN 0100 02 ST. PETERSBURG SOUTHEAST
476236 FEGEN 037v 03 ST. PETERSBURG SOUTHEAST
555555 FEGEN 0304 05 ST. PETERSBURG SOUTHEAST
100000 JOHNSON 030s 06 ST. PETERSBURG SOUTHEAST
248545 JOHNSON 0345 14 ST. PETERSBURG SOUTHEAST
400000 JOHNSON 070y 08 ST. PETERSBURG SOUTHEAST
878787 JOHNSON 1235 12 ST. PETERSBURG SOUTHEAST
C h a p t e r 1 5 — Control Breaks

(continued)

SALES ACTIVITY REPORT PAGE 3

REGION: SOUTHEAST
LOCATION: ST. PETERSBURG

SALES ACTIVITY REPORT PAGE 2

REGION: NORTHEAST
LOCATION: BALTIMORE

SALES ACTIVITY REPORT PAGE 1

REGION: MIDWEST
LOCATION: CHICAGO
SALESPERSON: BENWAY
ACCOUNT t SALES COMMISSION
U U U U U 3 231- 23-
476530 235- 12-
988888 450 5
999340 334 100

** SALESPERSON TOTAL $ 318 $ 70

SALESPERSON: HUMMER
ACCOUNT # SALES COMMISSION
000100 107- 5-
649356 345 17
694446 904 90

** SALESPERSON TOTAL $ 1,142 $ 102

**** LOCATION TOTAL $ 1,460 $ 172

LOCATION: ST. LOUIS


SALESPERSON: HAAS
ACCOUNT # SALES COMMISSION
203000 8,900 445
277333 98- 8-
475365 333 17

** SALESPERSON TOTAL $ 9,135 $ 454

**** LOCATION TOTAL $ 9,135 $ 454

****** REGION TOTAL $ 10,595 $ 626

i:
Chapter 15 — Control Break

mt& " i S . S (continued)

44
45 WORKING-STORAGE SECTION.
46 01 FILLER PIC X(14)
47 VALUE 'WS BEGINS HERE'.
48
49 01 SALES-RECORD-IN.
50 05 SR-ACCOUNT-NUMBER PIC 9(6).
51 05 FILLER PIC X.
52 05 SR-NAME PIC X(15).
53 05 SR-SALES PIC S9(4).
54 05 FILLER PIC XX.
55 05 SR-COMMISSION-PERCENT PIC V99.
ETC
05 FILLER PIC XX.
uu
57 05 SR-LOCATION PIC X(15).
58 05 SR-REGION PIC X(ll).
59
60 01 PROGRAM-SWITCHES-AND-COUNTERS
si DATA-REMAINS-SW i ir
V X
\j ~> PIC X(3) U A I
KttLUt'YES'.
62 88 NO-DATA-REMAINS VALUE 'NO' .
63 05 PREVIOUS-NAME PIC X(15) VALUE SPACES.
64 05 PAGE-COUNT PIC 99 VALUE ZEROES.
65
66 01 CONTROL-BREAK-TOTALS.
67 05 INDIVIDUAL-TOTALS.
68 10 IND-COMMISSION PIC S9(4).
69 05 SALESPERSON-TOTALS.
70 10 SALESPERSON-SALES-TOT PIC S9(6).
71 10 SALESPERSON-COMM-TOT PIC S9(6).
72 05 COMPANY-TOTALS.
73 10 COMPANY-SALES-TOT PIC S9(6) VALUE ZEROS.
74 10 COMPANY-COMM-TOT PIC S9(6) VALUE ZEROS.
75
76 01 REPORT-HEADING-LINE.
77 05 FILLER PIC X(25) VALUE SPACES.
78 05 FILLER PIC X(21)
79 VALUE 'SALES ACTIVITY REPORT'.
80 05 FILLER PIC X(19) VALUE SPACES.
81 05 FILLER PIC X(5) VALUE 'PAGE '
82 05 HDG-PAGE PIC Z9.
83 05 FILLER PIC X(60) VALUE SPACES.
84
85 01 SALESPERSON-HEADING-LINE-ONE.
86 05 FILLER PIC X(15) VALUE SPACES.
87 05 FILLER PIC X(13)
88 VALUE 'SALESPERSON: '.
89 05 HDG-NAME PIC X(15).
90 05 FILLER PIC X(89) VALUE SPACES.
91
92 01 SALESPERSON-HEADING-LINE-TWO.
93 05 FILLER PIC X(23) VALUE SPACES.
Chapter

CI (continued)

INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SALES-FILE ASSIGN TO 'A:\CHAPTR15\S0RTIN.DAT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT PRINT-FILE
ASSIGN TO PRINTER.
SELECT SORT-WORK-FILE
ASSIGN TO 'A:\CHAPTR15\S0RTWK.DAT'.
SELECT SORTED-SALES-FILE ASSIGN TO 'A:\CHAPTR15\S0RT0UT.DAT
ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD SALES-FILE
RECORD CONTAINS 58 CHARACTERS
DATA RECORD IS SALES-RECORD.
01 SALES-RECORD PIC X(58).

FD PRINT-FILE
RECORD CONTAINS 132 CHARACTERS
DATA RECORD IS PRINT-LINE.
01 PRINT-LINE PIC X(132).

SD SORT-WORK-FILE
RECORD CONTAINS 58 CHARACTERS
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
05 SORT-ACCOUNT-NUMBER PIC 9(6).
05 FILLER PIC X.
05 SORT-NAME PIC X(15).
05 FILLER PIC X(10).
05 SORT-LOCATION PIC X(15).
05 SORT-REGION PIC X(ll).

FD SORTED-SALES-FILE
RECORD CONTAINS 58 CHARACTERS
DATA RECORD IS SORTED-SALES-RECORD.
01 SORTED-SALES-RECORD PIC X(58).

WORKING-STORAGE SECTION.
01 FILLER PIC X(14)
VALUE "WS BEGINS HERE'.

01 SALES-RECORD-IN.
05 SR-ACCOUNT-NUMBER PIC 9(6).
05 FILLER PIC X.
05 SR-NAME PIC X(15).
C h a p t e r 1 5 Control Breaks

lure I S . t O (continued)

194 ! SORT-LOCATION
195 SORT-NAME
196 WITH DUPLICATES IN ORDER
197 USING SALES-FILE Keys in SORT statement match control breaks

198 GIVING SORTED-SALES-FILE.


199
200 220-READ-SORTED-SALES-FILE.
201 READ SORTED-SALES-FILE INTO SALES-RECORD-IN
202 AT END MOVE 'NO' T O D A T A - R E M A I N S - S W
203 END-READ.
204
205 240-PR0CESS-0NE-REGI0N. ^-Region heading written for each new region
206 PERFORM 300-INITIALIZE-REGI0N.
207 [PERFORM 320-WRITE-REGION-HEADINGT]^
208 PERFORM 340-PR0CESS-0NE-L0CATI0N
209 UNTIL SR-REGION NOT EQUAL PREVIOUS-REGION
210 _ O R NO-DATA-REMAINS.
211 | PERFORM 360-I^ITE-RW0N-T0TAL71
212 PERFORM 380-INCREMENT-COMPANY-TOTAL. Region total written alter cont'd break ct
e t
e c

213
214 260-WRITE-C0MPANY-T0TAL.
215 MOVE COMPANY-SALES-TOT TO COMPANY-SALES-TOTAL.
216 MOVE COMPANY-COMM-TOT T O COMPANY-COMM-TOTAL.
217 WRITE PRINT-LINE FROM COMPANY-TOTAL-LINE
218 AFTER ADVANCING 2 LINES.
219
220 300-INITIALIZE-REGION.
221 MOVE SR-REGION TO PREVIOUS-REGION.
222 INITIALIZE REGION-TOTALS.
223
224 320-WRITE-REGION-HEADING.
225 A D D 1 TO PAGE-COUNT.
226 MOVE PAGE-COUNT TO HDG-PAGE.
227 W R I T E P R I N T - L I N E FROM REPORT-HEADING-LINE
228 AFTER ADVANCING PAGE.
229 MOVE SR-REGION TO HDG-REGION.
230 WRITE PRINT-LINE FROM REGION-HEADING-LINE
231 AFTER ADVANCING 2 LINES.
232
233 340-PROCESS-ONE-LOCATION.
234 PERFORM 400-INITIALIZE-LOCATION.
235 PERFORM 420-WRITE-L0CATI0N-HEADING.
236 PERFORM 440-PR0CESS-0NE-SALESPERS0N
237 UNTIL SR-LOCATION N O T EQUAL PREVIOUS-LOCATION
238 O R SR-REGION N O T EQUAL PREVIOUS-REGION
239 OR NO-DATA-REMAINS.
240 PERFORM 460-WRITE-L0CATI0N-T0TAL.
C h a p t e r 1 6 — Subprograms

Input Program fiMPtJTSOlI)


T h e input program in Figure 16.9 reviews data validation a n d screen I/O as presented
in Chapter 10. It also functions as a subprogram, a n d hence the Linkage Section in
lines 53-70 which defines the data n a m e s passed from the calling (fitness) program.
Note the relationship b e t w e e n the C A L L statement in the calling p r o g r a m (lines
80-82 in Figure 16.8) a n d the Procedure Division header in line 120 of this program,
both of which contain the 01 entry, I N P U T - I N F O R M A T I O N . T h e latter is copied
into both programs in accordance with the p r o g r a m m i n g tip o n page 481.
T h e input p r o g r a m also contains a second C O P Y statement, C O P Y C O L O R C P Y ,
to define the various colors available with screen I/O. T h e Screen Section defines a n
input screen consistent with the display s h o w n earlier in Figure 16.4; it also utilizes
various features of screen I/O (line a n d c o l u m n positioning, reverse video, a n d
underlining) as presented in Chapter 10.
T h e Procedure Division accepts a n d validates the input parameters, o n e at a
time, in accordance with the table of error messages defined in lines 37-48. E a c h
parameter is processed in a separate paragraph, w h i c h utilizes the D O U N T I L
(TEST A F T E R ) construct described earlier in Chapters 9 a n d 10.

F i g u r e 16.9 Input Subprogram

1 IDENTIFICATION DIVISION.
2 PROGRAM- ID. INPUTSUB. _
3 AUTHOR. CVV.
4
5 DATA DIVISION.
6 WORKING-STORAGE SECTION.
7 01 FILLER PIC X(38)
8 VALUE 'WS BEGINS HERE FOR SUBPROGRAM INPUTSUB'.
9
10 01 PROGRAM-SWITCHES.
11 05 VALID-FIELD-SWITCH PIC XX.
12 88 VALID-FIELD VALUE SPACES.
13 05 CONFIRM-SWITCH PIC X.
14 88 ALL-DATA-VALID VALUE 'Y' 'y'.
15
16 COPY COLORCPY.
STCPY> 01 SCREEN-COLORS. PIC S9(4) C0MP-5.
18 * COLORS FOR FOREGROUND AND BACKGROUND
19 78 BLACK VALUE 0.
20 78 BLUE VALUE 1.
21 78 GREEN VALUE 2.
22 78 CYAN VALUE 3.
23 78 RED 4.
24 78 MAGENTA 5.
25 78 BROWN 6.
26 78 WHITE 7.
27 * ADDITIONAL COLORS FOR FOREGROUND ONLY
28 78 BRIGHT-BLACK 8.
29 78 BRIGHT-BLUE 9.
30 78 BRIGHT-GREEN 10.
31 78 BRIGHT-CYAN 11.
i r e 16,13 COBOL Programs for Problem 4

IDENTIFICATION DIVISION.
PROGRAM-ID. MAINPROG.

WORKING-STORAGE SECTION.
COPY INPUTREC
01 INPUT-DATA.
05 INPUT-NAME PIC X(15).

01 PASSED-PARAMETERS
05 PARM-A PIC 9(4).
05 PARM-B PIC XX.

PROCEDURE DIVISION.

CALL 'PROGA' USING PARM-A, PARM-B, INPUT-DATA.


CALL 'PROGB' USING PARM-A.

(a) Main Program

IDENTIFICATION DIVISION.
PROGRAM-ID. PROGA

LINKAGE SECTION.
COPY INPUTREC
01 INPUT-DATA.
05 INPUT-NAME PIC X(15).

01 NEW-DATA-NAMES.
05 NEW-NAME-A PIC XX.
05 NEW-NAME-B PIC 9(4).

PROCEDURE DIVISION
USING NEW-NAME-A, NEW-NAME-B, INPUT-DATA.
(b) First Subroutine

IDENTIFICATION DIVISION.
PROGRAM-ID. PROG-B.

LINKAGE SECTION.
01 PASSED-PARAMETERS.
05 PARM-A PIC 9(4).

PROCEDURE DIVISION
USING PARM-A.
(b) Second Subroutine
C h a p t e r 17 — Sequential File Maintenance

'igure 1 7 . S (continued)

I
800000000VILLAR C
999999999GILLENS0N MANYC10 c
A !
(a) Transaction File

INVALID TRANSACTION CODE 100000000GRABER P 30000


MISSING LOCATION CODE 222222222NEW GUY RT A
MISSING OR NON-NUMERIC COMMISSION RATE 222222222NEW GUY RT A
INVALID LOCATION CODE 333333333ESMAN TNNY 09 A
MISSING NAME OR INITIALS 555555555J0RDAN B0S07 A
SOCIAL SECURITY NUMBER OUT OF SEQUENCE 666666666J0HNS0N M NYC12 A
MISSING OR NON-NUMERIC SALES AMOUNT 800000000VILLAR C C

(b) Error Messages

O0O00OOOOBOROW JSATL07 A
O0OO0OO0OBOROW JS 10000C
O000OO0OOBOROW JS 20000C
400000000MOLDOF BLATL15 A
444444444RICHARDS IM 05000C
700000000MILGROM A D
800000000VAZQUEZ C 55000C
999999999GILLENS0N MANYC10 A

(c) Valid Transaction file


i!

Figure 1 7 . 6 Hierarchy Chart for Data Validation Program

EDIT
TRANSACTION
FILE

PROCESS
TRANSACTIONS

DO VALID DO DO DO WRITE
DO SEQUENCE
CODE ADDITION CORRECTION VALID
CHECK
CHECK CHECKS CHECKS TRANSACTION

WRITE ERROR WRITE ERROR WRITE ERROR WRITE ERROR WRITE ERROR
MESSAGE MESSAGE MESSAGE MESSAGE MESSAGE
Chapter 17 Sequential File Maintenance

jure 17.8 (continued)

86 05 WS-PREVIOUS-SOC-SEC-NUMBER PIC X(9) VALUE SPACES.


87 05 WS-ERR0R-C0DE PIC 99.

89 PROCEDURE DIVISION.
90 100-EDIT-TRANSACTION-FILE.
91 OPEN INPUT TRANSACTION-FILE
92 OUTPUT VALID-TRANSACTION-FILE
93 ERROR-FILE.
94 PERFORM UNTIL WS-EOF-SWITCH = 'YES'
95 READ TRANSACTION-FILE INTO WS-TRANSACTION-RECORD
96 AT END
97 MOVE 'YES' TO WS-EOF-SWITCH
98 NOT AT END
99 PERFORM 210-PR0CESS-TRANSACTI0NS
100 END-READ
101 END-PERFORM.
102 CLOSE TRANSACTION-FILE
103 VALID-TRANSACTION-FILE
104 ERROR-FILE.
105 STOP RUN.
106
107 210-PR0CESS-TRANSACTI0NS.
108 MOVE 'YES' TO WS-VALID-RECORD-SWITCH.
109 PERFORM 300-DO-SEQUENCE-CHECK.
110 PERFORM 310-DO-VALID-CODE-CHECK.
111 IF ADDITION
112 PERFORM 320-DO-ADDITION-CHECKS
113 ELSE
114 IF CORRECTION
115 PERFORM 330-D0-C0RRECTI0N-CHECKS
116 END-IF
117 END-IF.
118 IF WS-VALID-RECORD-SWITCH = 'YES'
119 PERFORM 340-WRITE-VALID-TRANSACTION
120 END-IF. to output file
121
122 300-DO-SEQUENCE-CHECK.
123 IF TR-SOC-SEC-NUMBER < WS-PREVIOUS-SOC-SEC-NUMBER
124 MOVE 1 TO WS-ERROR-CODE
125 PERFORM 400-WRITE-ERR0R-MESSAGE~^-~^_^
126 END-IF.
127 MOVE TR-SOC-SEC-NUMBER TO WS-PREVIOUS-SOC-SEC-NUMBER.
128
129 310-DO-VALID-CODE-CHECK.
130 IF NOT VALID-CODES • '
131 IMOVE 2 TO WS-ERROR-CODE
132 PERFORM 400-WRITE-ERROR-MESSAGE
133 END-IF.
134
135 320-DO-ADDITION-CHECKS.
Chapter 17 Sequential File Maintenance

F i g u r e 17 Output of the Sequential Update


I
I
P ATL1500000000
| 100000000GRABER MABOS0800020000
j 200000000RUBIN IRBOS1000113000
I 300000000ANDERSON BLATL1500000000
j 400000000MOLDOF JSNYC1000045000
j 500000000GL.ASSMAN RTNYC0800087500
j 600000000GRAUER A SF 0900120000
700000000MILGR0M C ATL1200060000
800000000VAZQUEZ E NYC07000O250O
900000000CLARK
(a) Old Master

0OO0000OOBOROW JSATL07 A
| O0O0OOOO0B0ROW JS 10000C
j OOOOOOOOOBOROW JS 20000C
j 400000000MOLDOF BLATL15 A
| 444444444RICHARDS IM 050O0C
j 700000000MILGR0M A D
1 800000000VAZQUEZ C 55000C
j 999999999GILLENS0N MANYC10 A

1 (b) Valid Transaction File


1

OOOOOOOOOBOROW JSATL0700030000
! 100000000GRABER P ATLI500000000
200000000RUBIN MAB0S0800020000
300000000ANDERSON IRBOS1000113000
400000000MOLDOF BLATL1500000000
500000000GLASSMAN JSNYC1000045000
600000000GRAUER RTNYC0800087500
800000000VAZQUEZ C ATL1200115000
900000000CLARK E NYC0700002500
999999999GILLENS0N MANYC1000000000

(c) New Master File


i

ERROR-DUPLICATE ADDITION: 400000000


ERROR-NO MATCHING RECORD: 444444444

(d) Error Messages


C h a p t e r 1 8 - Indexed Files

set contains the highest key in the corresponding control area; thus 377, 619, a n d
800 are the highest keys in thefirst,second, a n d third control areas, respectively.
E a c h control area has its o w n sequence set. T h e entries in thefirstsequence set
s h o w the highest keys of the control intervals in thefirstcontrol area to be 280, 327,
a n d 377, respectively. Note that the highest entry in the third control interval, 377,
corresponds to the highest entry in thefirstcontrol area of the index set.
Figure 18.1 illustrates two kinds of pointers, vertical a n d horizontal. Vertical
pointers are used for direct access to a n individual record. For example, a s s u m e
that the record with a key of 449 is to be retrieved. V S A M begins at the highest level
of index (that is, at the index set). It concludes that record key 449, if it is present, is
in the second control area (377 is the highest key in thefirstarea, whereas 619 is the
highest key in the second control area). V S A M follows the vertical pointer to the
sequence set for the second control area a n d draws itsfinalconclusion: record key
449, if it exists, will be in thefirstcontrol interval of the second control area.
Horizontal pointers are used for sequential access only. In this instance, V S A M
begins at the first sequence set a n d uses t h e horizontal pointer to get from that
sequence set record to the o n e containing the next highest key. Put another way, the
vertical pointer in a sequence set points to data; the horizontal pointer indicates the
sequence set containing the next highest record.
Figure 18.1 c o n t a i n s several allocations of free space, w h i c h are distributed in
o n e of two ways: as free space within a control interval or as a free control interval
within a control area. In other words, as V S A M loads a file, e m p t y space is deliberately
left throughout the file. This is d o n e to facilitate subsequent insertion of n e w records.
Figure 18.2 s h o w s the changes brought about b y the addition of two n e w
records, with keys of 410 a n d 730, to the file of Figure 18.1. Addition of the first
record, key 410, poses n o problem, as free space is available in the control interval

Control Interval Split

Vert. Vert. Vert.


Index set 377 619 800
Pntr. Pntr. Pntr.

Horiz. Horiz.
Sequence Set Pntr. W Sequence Set Pntr. Sequence Set
Vert. Vert. Vert. Vert. Vert. Vert. Vert. Vert.
280 327 377 469 619 700 800
Pntr. Pntr. Pntr. Pntr. Pntr. Pntr. Pntr. Pntr.

7 7 7
251 312 345
7 7 394 500
7 7 7
627 717 746

269 318 346 400 502 642 722 748

280 327 377 410 598 658 730 800


FREE
449 617 675
FREE FREE FREE FREE FREE
469 619 700

Control Interval
Split

Control Area Control Area Control Area


C h a p t e r 1 8 - Indexed Files

In t h i s e x a m p l e C U S T O M E R - L O A N - N U M B E R is a g r o u p i t e m a n d c o n s i s t s o f t h e
. :* / * ' ! I f ' I V MV * 1 ' ! > \ ! I I \ « 111 *I I I I / U \ ' X I I I \ .1 1 1 1 * l > I 1-.- i' . I

c i c i i i c n i a i y nciiis, i j u j 1 u i v i m \ - i i u i v i D m d i m n / m \ - , \ u i v u n c v e i y v a l u e ui m e
r e c o r d k e y ( C U S T O M E R - L O A N - N U M B E R ) m u s t b e u n i q u e , b u t t h e r e c a n b e several
l o a n s for t h e s a m e c u s t o m e r , w i t h e a c h l o a n a s s i g n e d a n e w l o a n n u m b e r . C u s t o m e r
1 1 1 1 1 1 , for e x a m p l e , m a y h a v e t w o o u t s t a n d i n g l o a n s , w i t h r e c o r d k e y s o f 1 1 1 1 1 1 0 0 1
a n d 1 1 1 1 1 1 0 0 4 , r e s p e c t i v e l y . (Loans 0 0 2 a n d 0 0 3 m a y h a v e b e e n p r e v i o u s l y p a i d
off.) T h e p r o b l e m is t o retrieve all l o a n s for a g i v e n c u s t o m e r , w h i c h l e a d s t o a
d i s c u s s i o n o f t h e START s t a t e m e n t .

Ttw Statement
T h e START s t a t e m e n t m o v e s n o n s e q u e n t i a l l y ( r a n d o m l y ) i n t o a n i n d e x e d file t o
t h e first r e c o r d w h o s e v a l u e is e q u a l t o , greater t h a n , o r n o t l e s s t h a n t h e v a l u e
c o n t a i n e d i n t h e identifier. T h e INVALID KEY c o n d i t i o n is r a i s e d if t h e file d o e s
n o t c o n t a i n a r e c o r d m e e t i n g t h e s p e c i f i e d criterion. S y n t a c t i c a l l y , t h e START
s t a t e m e n t h a s t h e form:

IS EQUAL TO
IS =
IS GREATER THAN
IS >
START file- name KEY identifier
IS NOT LESS THAN
IS NOT <
IS GREATER THAN OR EQUAL TO
IS >=

[INVALID KEY imperative-statement-l]

[NOT INVALID KEY imperative-statement-2]

[END-START]

T h e START s t a t e m e n t c a n b e u s e d i n c o n j u n c t i o n w i t h a c o n c a t e n a t e d k e y a s
s h o w n i n Figure 18.13. Note, however, that START only moves to the designated
record, but does not read the record. I n o t h e r w o r d s , a READ s t a t e m e n t is r e q u i r e d
i m m e d i a t e l y f o l l o w i n g START. T h e s u b s e q u e n t PERFORM s t a t e m e n t will t h e n
retrieve all l o a n s for t h e c u s t o m e r i n q u e s t i o n .

The READ, DELETE, WRITE, REWRITE, and START statements contain both
an optional scope terminator and a false-condition branch. As indicated
throughout the text, these elements are new to COBOL-85 and were not
available in COBOL-74.
Sixteen I/O status codes (i.e., the majority of the entries in Table
18.1) are new to COBOL-85. The new codes (02, 04, 05, 07, 15, 24, 25,
34, 35, 37, 38, 39, 41, 42, 43, 46, and 49) were added to eliminate the
need for vendor-specific file status codes that treated the same error
condition in different ways.
C h a p t e r 20 — Object-Oriented COBOL Programming

«• T h e I n s t a n c e d o e s n o t h a v e s p e c i a l d a t a o t h e r t h a n that n e e d e d t o m a n a g e
LllC lllC

» T h e o n l y o b j e c t that it is k n o w s is S t u d e n t .

« W h a t it d o e s is t o r e a d r e c o r d s f r o m t h e file, test t o s e e w h e t h e r t h e y m e e t t h e
s p e c i f i c a t i o n s , a n d c r e a t e S t u d e n t i n s t a n c e s c o n t a i n i n g t h e S t u d e n t data.

Of t h e t h r e e m e t h o d s d e f i n e d for t h e i n s t a n c e , o n l y o n e G e t S t u d e n t is i n v o k e d
f r o m o u t s i d e . T h e o t h e r t w o m e t h o d s are "private" m e t h o d s t o b e u s e d o n l y b y
other m e t h o d s in the StudentDM.
B e g i n n i n g at line 6 5 , G e t S t u d e n t m a i n t a i n s a L o c a l - s t o r a g e S e c t i o n t o h o l d a n
e n d - o f - f i l e s w i t c h a n d a Linkage S e c t i o n t o r e c e i v e t h e i n v o k i n g p a r a m e t e r s a n d t o
p a s s b a c k t h e h a n d l e for t h e S t u d e n t i n s t a n c e .
T h e P r o c e d u r e D i v i s i o n h a s b o t h u s i n g a n d r e t u r n i n g c l a u s e s to a l l o w u s e o f
t h e i t e m s i n t h e Linkage S e c t i o n . T h e a l g o r i t h m o f t h e m e t h o d is to initialize t h e
Student handle to null a n d t h e n invoke t h e ReadRecord m e t h o d . T h e invoke
s t a t e m e n t u s e s t h e k e y w o r d s e l f. Sel f refers t o t h e p o i n t e r o f t h e current i n s t a n c e .
A n i n s t a n c e a l w a y s k n o w s its o w n m e m o r y l o c a t i o n a n d c a n r e f e r e n c e t h a t l o c a t i o n
b y u s i n g s e l f.
O n c e t h e r e c o r d is read, G e t S t u d e n t e v a l u a t e s t h e c r e d i t h o u r s a g a i n s t t h e
m i n i m u m a n d t h e m a j o r a g a i n s t t h e r e q u i r e d major. If t h e t e s t is s u c c e s s f u l , t h e
r o u t i n e i n v o k e s t h e C r e a t e S t u d e n t m e t h o d , a n d u s e s t h e result t o return t h e S t u d e n t
H a n d l e a n d t o s t o p t h e l o o p . If t h e t e s t w a s n o t s u c c e s s f u l , t h e r o u t i n e i n v o k e s
ReadRecord m e t h o d to get t h e next record. W h e n ReadRecord runs o u t of records,
it returns "NO" t o t h e D a t a - R e m a i n s - S w i t c h . T h e l o o p s t o p s , a n d b e c a u s e n o i n s t a n c e
o f S t u d e n t h a s b e e n c r e a t e d t h e m e t h o d r e t u r n s a n u l l h a n d l e to t h e i n v o k i n g
procedure.

P R O G R A M M I N G T I P

The C O B O L Evaluate statement is m u c h more powerful than C A S E statements in other languages. In most
languages, the C A S E statement can test a condition for only one variable at a time. The Evaluate statement,
however, allows testing of several conditions at once. Consider the Eval uate statement in the GetStudent
method of StudentDM.

evaluate Stu-Credits also Stu-Major


when >= ls-Stu-Credits also = Is-Stu-Major
invoke self 'CreateStudent'
returning ls-theStuHandle
when other
invoke self 'ReadRecord'
returning
Data-Remains-Switch
end-evaluate
Notice that the Eval uate statement tests conditions for Stu-Credits but also for Stu-Major. Al so is the
keyword that tells the Eval uate to test both variables in an A N D relationship. The first When clause compares the
respective variables to ls-Stu-Credits and Is-Stu-Major. The use of the keyword Al so makes sure that proper
testing occurs.
This test will allow only records that have Stu-Credits greater than or equal to ls-Stu-Credits A N D Stu-
Major equal to Is-Stu-Major.
The Evaluate statement can virtually eliminate the need for nested IFs w h e n used properly.
A p p e n d i x A >•*, r-ocus Personal COBOL ioi Windows- Users Guide and Tutorial

Analyze—This function is not available in Personal C O B O L .


Edit lock on execute—is option keeps the user from m a k i n g changes to the
p r o g r a m while it is in the execution phase. T h e function is intended to
prevent accidental changes to the program. This is the s a m e function as Edit
lock in the Edit m e n u .

Configure Autofix Words

Aiilftl i* rfMrie-,
H&mtm m y typing

Ui»nge it fa thin:
If H the"

Find options is the third option available in this section. Again, Find options is
generally best left alone by the student p r o g r a m m e r . Figure A.51 s h o w s the C O B O L
data item finds. O n c e p r o g r a m m e r s have b e c o m e experienced in debugging
programs, they m a y w a n t to explore s o m e of the options here.

»0 Execute Options Window

P Hide sn Hun
"~ F}aiilfat k >»«
r &st threshold levcJ
f* finely ;?e

r l do \ock oft execute

0fe I Hrfe
Appendix B Getting Started

Open the Windows95 Start Menu

O p e n the W i n d o w s 9 5 Start M e n u b y clicking o n the mm4 c o m m a n d button o n the


task bar or the keyboard Start key.

Select the Run Option and Start the Setup P r o c e s s

Click o n the R u n option from the Start M e n u s h o w n in Figure B.la. This action
brings u p the W i n d o w s h o w n in Figure B.lb. In this example, "D:" refers to the C D -
R O M drive. O n your m a c h i n e the C D - R O M m a y have s o m e other letter. If y o u are
installing from diskettes, y o u probably w o u l d use A: instead off):. "Setup.exe" is the
n a m e of the program to install Personal C O B O L . Click o n the O K button a n d the
W e l c o m e message box s h o w n in Figure B.lc appears. (Tick o n O K again.

Starting Animator Installation

0 m*

ZD-Rom dn
jP

If** n*on*.tm • Ad <r.n«tl CtruTi.jf l.ffHUI l o t

yaw tjm&iHm
I ' I F I ! IFW ILK !RL«LI(N IFF I'LHIH ULF INTT^LWJFFT
FM.TTL' Y O U I TTF» P I R T I »HT* I .HL< « J I H A I I U I I )
•/(HI ILL *K»T W 4 H I !TI iftdJflB THIS S N F L V I W nvm
M I L N M ^ITLUJI ••ITM •! F I U MFH

You might also like