+ All Categories
Home > Education > Cobol from micro to mainframe

Cobol from micro to mainframe

Date post: 14-Dec-2014
Category:
Upload: gonzalo-santiago
View: 1,243 times
Download: 21 times
Share this document with a friend
Description:
Cobol from micro to mainframe
Popular Tags:
908

Click here to load reader

Transcript
  • 1. COBOLFrom Micro to MainframeRT:elEntT T. [3Ft.AUEnl AR{f L VnzcpuEz VtLL,A,Fl ATTTHUR R. EIUgs THIRD EDITIOil

2. ISBN O-I3_?1DBI7-Alffifiilflillilililllililtlflll 3. T h e f o l l o w i n g s o f t w a r e a n d s u p p l e m e n t s are available f r o m P r e n t i c e Hall: SOFTWAREMicro F o c u s P e r s o n a l COBOL for W i n d o w s 3.1 w i t h o b j e c t orientation and Personal Dialog System. Compatible with W i n d o w s 9 5 and W i n d o w s N T , P e r s o n a l COBOL p r o v i d e s all t h e t o o l s to h e l p y o u l e a r n a n d u s e COBOL. T h e software i n c l u d e s a n i n t e g r a t e d editor, c o m p i l e r a n d a n i m a t o r for creating, d e b u g g i n g a n d e x e c u t i n g COBOL p r o g r a m s . P r e n t i c e Hall offers a n affordable p a c k a g e of COBOL: From Micro to Mainframe, Third Edition w i t h t h e M i c r o F o c u s P e r s o n a l COBOL C o m p i l e r . P l e a s e o r d e r ISBN 0-13-975178-5. eWEB S I T E D o w n l o a d e v e r y COBOL p r o g r a m in t h e text as w e l l as d a t a files for t h e n e a r l y o n h u n d r e d s t u d e n t p r o j e c t s f r o m t h e COBOL: From Micro toMainframe web site at:http://www.prenhall.com/grauer_cobol. Instructor's R e s o u r c e M a n u a l (ISBN# 0 - 1 3 - 0 8 1 5 1 3 - 6 ) P r e n t i c e Hall C u s t o m Test. B a s e d o n t h e p o w e r f u l t e s t i n g t e c h n o l o g y d e v e l o p e d b y E n g i n e e r i n g Software A s s o c i a t e s , Inc. (EAS), P r e n t i c e Hall C u s t o m T e s t a l l o w s t h e e d u c a t o r to c r e a t e a n d tailor t h e e x a m t o their o w n n e e d s . Please order ISBN* 0-13-081515-2W e are e s p e c i a l l y grateful to o u r e d i t o r s at P r e n t i c e Hall, Laura Steele, A l a n Apt, a n d Marcia H o r t o n , w i t h o u t w h o m this p r o j e c t w o u l d n o t h a v e b e e n p o s s i b l e . W e a l s o w a n t to t h a n k t h e m a n y o t h e r i n d i v i d u a l s w h o h e l p e d p r o d u c e t h e third e d i t i o n . Irwin Zucker, w h o s u p e r v i s e d t h e p r o d u c t i o n , Kate Kaibni, editorial a s s i s t a n t , w h o w o r k e d h a r d to p r o v i d e u s w i t h t i m e l y c h a p t e r r e v i e w s , a n d Joel B e r m a n , o u r m a r k e t i n g m a n a g e r at P r e n t i c e Hall, w h o d e v e l o p e d t h e i n n o v a t i v e c a m p a i g n t o m a k e this b o o k a s u c c e s s . W e a l s o w a n t t o a c k n o w l e d g e o u r r e v i e w e r s , w h o t h r o u g h their c o m m e n t s a n d c o n s t r u c t i v e c r i t i c i s m , m a d e t h i s a far b e t t e r b o o k : R o b e r t V. B i n d e r , R o b e r t B i n d e r S y s t e m s C o n s u l t i n g , Inc. D i n o n Boyer, U n i v e r s i t y o f A k r o n G e o r g i a B r o w n , N o r t h e r n Illinois U n i v e r s i t y Jan D e L a s s e n , B r i g h a m Y o u n g U n i v e r s i t y Ida M. F l y n n , U n i v e r s i t y o f P i t t s b u r g h Frank T. Gergelyi, NJIT Ken Goldsmith, University of M i a m i T o m Gorecki, St. Charles C o m m u n i t y C o l l e g e Carol C. G r i m m , P a l m B e a c h C o m m u n i t y C o l l e g e M o n i c a H o l m e s , Central M i c h i g a n U n i v e r s i t y A n n W. H o u c k , P i m a C o m m u n i t y C o l l e g e David Lee J a m e s W. P a y n e , Kellogg C o m m u n i t y C o l l e g e N i c h o l a s R o s s , U n i v e r s i t y o f Illinois at C h i c a g o W e n d e l l I. P o p e , U t a h State U n i v e r s i t y D a n i e l H. R i n d f l e i s c h , C o m p u t e r Specialist w i t h F e d e r a l G o v e r n m e n t D a n i e l R. Rota, R o b e r t M o r r i s C o l l e g e Richard H. S a r a c u s a , N o r t h e a s t e r n U n i v e r s i t y Ron Teemley, DeVry Institute of T e c h n o l o g y D o n a t Valcourt, Northeastern University Ron Williams, M c L e n n o n C o m m u n i t y College Jackie Zucker, U n i v e r s i t y o f M i a m i 4. A final w o r d o f t h a n k s t o y o u , o u r readers, for c h o o s i n g t h i s b o o k . P l e a s e feel free to c o n t a c t u s w i t h a n y c o m m e n t s or s u g g e s t i o n s via e m a i l .Robert Grauer r g r a u e r @ u m i a m i .miarni. e d u Carol Vazquez Villar Arthur R. Buss [email protected] 5. rreiaceM iIntroduction Test Data 13 Elements of COBOL 13 Reserved Words 13 Programmer-Supplied Names 14 Literals 15 Symbols 16 Level Numbers 16 Picture Clauses 17 A S e c o n d Look at COBOL 17 Summary 19 Fill-in 20 True/False 20 Problems 21Overview 2 The First Problem 2 Programming Specifications 3 Required Logic 5 Flowcharts 6 Pseudocode 8 A First Look at COBOL 9 Identification Division 11 Environment Division 11 Data Division 11 Procedure Division 12From Coding Form to Computer Overview 26 From Coding Form to Computer 26 The COBOL Coding Form 28 Use of an Editor 28 The Compile, Link, and Execute Sequence 30 Learning by Doing 3 2 Errors in Entering the Program 33 Errors in Operating System Commands 33Errors in Compilation 33 Errors in Execution 35 Errors in Data Input 37 Evolution of COBOL 38 There's Always a Reason Summary 41 Fill-in 41 True/False 4240 Problems 43A Methodology for P r o g r a m D e v e l o p m e n t Overview 48 The Tuition Billing Problem 48 Structured Design 50 Evaluating the Hierarchy Chart Completeness 53 Functionality 54 Span of Control 54 Structured Programming 5452Sufficiency of the Basic Structures 56 Expressing Logic 57 The Traditional Flowchart 57 Pseudocode 57 Warnier-Orr Diagrams 59 Top-Down Testing 61 Summary 66 Fill-in 67 True/False 68 Problems 68 6. ContentsC h a p t e r 4 : T h e I d e n t i f i c a t i o n , Environment, a n d D a t a D i v i s i o n s 7 3 Overview 74 COBOL Notation 74 Identification Division 75 Environment Division 76 CONFIGURATION SECTION 76 INPUT-OUTPUT SECTION 76 Data Division 77 FILE SECTION 77WORKING-STORAGE SECTION82The Tuition Billing Program 8 4 Programming Specifications 84 COBOL Entries 87 Limitations of COBOL-74 90 Summary 90 Fill-in 92 True/False 92 Problems 93975: T h e P r o c e d u r e D i v i s i o n Overview 98 OPEN 98 CLOSE 99 READ 99 Placement of the READ Statement 100 WRITE 100 STOP RUN 102 MOVE 102 Restrictions on the Move Statement 103 Alphanumeric Field to Alphanumeric Field 103 Numeric Field to Numeric Field 104 Group Moves 105 PERFORM 105 IF 106 The ELSE Clause 106 Indentation 106 EVALUATE 109 Arithmetic Statements 109The ROUNDED Clause 109 The SIZE ERROR Clause 110 COMPUTE 110 ADD 112 SUBTRACT 112 MULTIPLY 114 DIVIDE 115 Programming Tip: Use the COMPUTE Statement 116 Assumed Decimal Point 117 The Tuition Billing Program 118 Test Data 126 Hierarchy Chart 127 COBOL Program Skeleton 128 Limitations of COBOL-74 128 Summary 130 Fill-in 131 True/False 132 Problems 133139C h a p t e r 6: D e b u g g i n g Overview 140 Errors in Compilation 140 Common Compilation Errors 149 Errors in Execution 151 File Status Codes 156 Another Run Time Error 158 Logic Errors 159Tips for Debugging 160 DISPLAY Statement 161 The Structured Walkthrough Summary 163 Fill-in 164 True/False 165162 Problems 165 7. ContentsChapter 7: Editing and Coding Standards Overview 170 Editing 170 The Decimal Point 172 Zero Suppression 172 Dollar Signs 174 Comma 174 Asterisks for Check Protection 175 Insertion Characters 175 Synopsis 175 Signed Numbers 176 CRandDB 176 Plus and Minus Signs 177BLANK WHEN ZERO Clause 177 The Tuition Billing Program Revisited 178 Coding Standards 179 Data Division 179 Programming Tip; Avoid Literals 180 Procedure Division 181 Programming Tip: Use Scope Terminators 182 Both Divisions 183 A Well-Written Program 184 Summary 189 Fill-in 190 True/False 191 Problems 191C h a p t e r Us Data Validation Overview 196 System Concepts: Data Validation 196 The IF Statement 197 Relational Condition 198 Class Test 199 Sign Test 200 Condition-Name Test (88-Level Entries) 200 Compound Test 200 Hierarchy of Operations 201 Implied Conditions 203 Nested IFs 203 NEXT SENTENCE 205Cna^ici-S * More About theOverview 2 3 0 PERFORM 231 TEST BEFORE/TEST AFTER 231 In-line Perform 232 Performing Sections 232 PERFORM THRU 232 Programming Tip: Perform Paragraphs, Not Sections 233 READ 234 False-Condition Branch 234 READ INTO 235 WRITE FROM 236 INITIALIZE 236 String Processing 237 INSPECT 237169195 ACCEPT Statement 206 Calculations Involving Dates 206 The Stand-Alone Edit Program 2 0 7 Programming Specifications 208 Error Messages 211 Pseudocode 211 Hierarchy Chart 212 The Completed Program 212 Limitations of COBOL-74 221 Summary 2 2 3 Fill-in 224 True/False 224 Problemsedure Division225229STRING 238 UNSTRING 240 Reference Modification 240 ACCEPT 2 4 2 Duplicate Data Names 243 Qualification 244 MOVE CORRESPONDING 245 The Car Billing Program 246 Programming Specifications 246 Program Design 248 The Completed Program 249 Limitations of COBOL-74 258 Summary 2 5 8 Fill-in 259 True/False 260 Problems 260 8. Contentsw. .Overview 266 ACCEPT 266 Programming Tip: Micro Focus Level 78The Use of COBOL Constants 267 DISPLAY 269 The Tuition Billing Program Revisited 270 Programming Specifications 270 Hierarchy Chart 271 Pseudocode 271 The Completed Program 273 Programming Tip: The Hidden Power of the Alt key 274... wveivl6wi" Introduction to Tai oud,uCar Validation and Billing Program 278 Programming Specifications 279 The Screen Section 280 Hierarchy Chart 284 Pseudocode 284 The Completed Program 287 Limitations of COBOL-74 297 Summary 297 Fill-in 298 True/False 299 Problems 299s301 OCCURS DEPENDING ON 310introduction to Tables 302 OCCURS Clause 303 Processing a Table 304 PERFORM VARYING 304 A S e c o n d Example 306 Problems with the OCCURS Clause 308 Rules for Subscripts 308 Relative Subscripting 308 USAGE Clause 309.265-i C: S c r e e n l-OTable LookupsOverview 332 System C o n c e p t s 332 Types of Codes 333 Characteristics of Codes 333 Sequential Table Lookup 334 Binary Table Lookup 335 Positional Organization and Direct Lookups 336 Initializing a Table 336 Hard Coding 336 Input-Loaded Tables 338 Table Lookups 339 PERFORM VARYING Statement 340 SEARCH Statement 340The Student Transcript Program 311 Programming Specifications 311 Program Design 313 The Completed Program 314 Indexes versus Subscripts 321 The SET Statement 322 Limitations of COBOL-74 322 Summary 325 Fill-in 326 True/False 326 Problems 327331 Programming TipRestrict Subscripts and Switches to a Single Use 342 SEARCH ALL Statement 344 Direct Lookup 344 Range-Step Tables 345 A Complete Example 347 Programming Specifications 347 Program Design 349 The Completed Program 350 Limitations of COBOL-74 357 Summary 357 Fill-in 358 True/False 359 Problems 360 9. Contentsc:;zn i. PERFORM VARYING 382Overview 3 6 4 System C o n c e p t s 364 COBOL Implementation 366 One-Level Tables 3 6 6 PERFORM VARYING 366A Sample Program 3 8 4 Programming Specifications 384 The Completed Program 386 Table Lookups 3 9 0 A Calorie Counter's Delight 3 9 2 Programming Specifications 392 Range-Step Tables 392 The Completed Program 394 Limitations of COBOL-74 398 Summary 3 9 8 Fill-in 399 True/False 399 Problems 400Two-Level Tables 3 6 8 Errors in Compilation 369 PERFORM VARYING 370 A Sample Program 3 7 3 Programming Specifications 373 Program Design 375 The Completed Program 375 Three-Level Tables 3 8 0.',[k-,.403: SortingUSING/GIVING Option 4 1 4 INPUT PROCEDURE/OUTPUT PROCEDURE Option 4 1 9 Comparing Options 426 MERGE Statement 426 Limitations of COBOL-74 428 Summary 4 2 8 Fill-in 429 True/False 430 Problems 431Overview 4 0 4 System C o n c e p t s 405 Collating S e q u e n c e 405 Embedded Sign 406 COBOL Implementation 408 SORT Statement 409 SD (Sort Description) 410 RELEASE and RETURN 410 Programming Specifications 411.363Multilevel TablesiKV s.y:c,ikic Control Overview 4 3 6 System C o n c e p t s 436 Running versus Rolling Totals 440 One-Level Control Breaks 4 4 3 Programming Specifications 443 Hierarchy Chart 444 Pseudocode 446 The Completed Program 446 Two-Level Control Breaks 451 Hierarchy Chart 451 Pseudocode 452Breaks40 C The Completed Program 454 Three-Level Control Breaks 4 6 0 Hierarchy Chart 460 Pseudocode 462 The Completed Program 463 Programming Tip: How to Write a Control Break Program 470 Limitations of COBOL-74 471 Summary 471 Fill-in 472 True/False 472 Problems 473 10. Contents475' v.. Subprograms 110 AND STU-MAJOR= 'ENGINEERING1In t h e first p o r t i o n o f t h e s t a t e m e n t , STU-CREDITS is c o m p a r e d to 1 1 0 , a n u m e r i c literal. N u m e r i c literals a d h e r e to t h e f o l l o w i n g rules: 1. A n u m e r i c literal c a n b e u p to 18 digits l o n g . 2. A n u m e r i c literal m a y b e g i n w i t h a l e a d i n g ( l e f t m o s t ) p l u s or m i n u s s i g n . 3. A n u m e r i c literal m a y c o n t a i n a d e c i m a l p o i n t , b u t it m a y not e n d w i t h a decimal point. T h e s e c o n d p a r t o f t h e IF s t a t e m e n tcontains a nonnumericliteral,'ENGINEERING'. N o n n u m e r i c literals a d h e r e to t h e f o l l o w i n g rules: 1. A n o n n u m e r i c literal is e n c l o s e d i n a p o s t r o p h e s (or q u o t a t i o n m a r k s ) a s specified by the compiler. 2. A n o n n u m e r i c literal m a y b e u p to 160 c h a r a c t e r s i n l e n g t h . 3. A n o n n u m e r i c literal m a y c o n t a i n a n y t h i n g , i n c l u d i n g b l a n k s , n u m b e r s , a n d r e s e r v e d w o r d s , b u t n o t a n o t h e r a p o s t r o p h e (or q u o t a t i o n m a r k ) . E x a m p l e s of b o t h n u m e r i c a n d n o n n u m e r i c literals are s h o w n T a b l e 1.3. 27. C h a p t e r1IntroductionNumeric and Nonnumeric Literals123.4Valid numeric literal'123.4'Valid nonnumeric literal+123Valid numeric literal'IDENTIFICATION DIVISION'Valid nonnumeric literali c^.:t i v a u v j i i u i i i c i ivj n i d i a i123-Invalid numeric literalthe minus sign must be in the leftmost positioni i layi u i c i t u w i n i a i j ^ ^ n i ic*i p w n l iSymbols are of t h r e e t y p e s p u n c t u a t i o n , a r i t h m e t i c , a n d r e l a t i o n a l , a s l i s t e d in T a b l e 1.4.IfelLfr: 1 , 4SymbolsDenotes end of C O B O L entryPunctuationDelineates clauses 1or" ()Sets off nonnumeric literals Encloses subscripts or expressions Addition SubtractionArithmetic+Multiplication Division Exponentiation/Equal to Greater thanRelational >Less than=Less than or equal too c on o ra t > HI u ^ ir u j t v j ci v.- ova p i i uiffc^i-tirit u um^i ^ni i v i niui. i ye n t r i e s for H E A D I N G - L I N E (lines 58-73), DETAIL-LINE (lines 75-92), a n d TOTALLINE i n l i n e s 107-121. N o t e , t o o , t h e s e p a r a t e e n t r y for DASH-LINE ( l i n e s 94-105), w h i c h m a k e s u s e o f t h e ALL literal t o e s t a b l i s h a r o w o f d a s h e s . Look carefully at t h e u s e o f t h e FILLER a n d a s s o c i a t e d VALUE c l a u s e s i n e a c h o f t h e s e p r i n t l i n e s , t o c r e a t e t h e n e c e s s a r y literal i n f o r m a t i o n , a n d t h e c o r r e s p o n d e n c e b e t w e e n t h e s e e n t r i e s a n d t h e COBOL s p e c i f i c a t i o n s .Figure 4 . 1 0Identification, Environment, and Data Divisions for Tuition Billing Program1 2 3 4 5 6IDENTIFICATION DIVISION. PROGRAM-ID. TUITI0N5. AUTHOR. CAROL VAZQUEZ VILLAR./r l LC-tuii i rui_.8 9 10 11 12ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. SELECT STUDENT-FILE ASSIGN TO 'A:CHAPTR05TUITI0N.DAT' ORGANIZATION IS LINE SEQUENTIAL. SELECT PRINT-FILE ASSIGN TO PRINTER.vs i 97. C h a p t e rFigure 4 , 1 013 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 624The Identification,Environment,andIdentification, Environment, and Data Divisions for Tuition Billing ProgramDATA DIVISION. FILE SECTION. FD STUDENT-FILE RECORD CONTAINS 27 CHARACTERS. 01 STUDENT-RECORD. 05 STU-NAME. 10 STU-LAST-NAME PIC X(15). 10 STU-INITIALS PIC XX. 05 STU-CREDITS PIC 9(2). 05 STU-UNION-MEMBER PIC X. PIC 9(4). 05 STU-SCHOLARSHIP 05 STU-GPA PIC 9V99. I'SiU-NAMEDataDivisions(continued)is a group item with two elementary items implied decimal pointFDPRINT-FILE RECORD CONTAINS 132 CHARACTERS. 01 PRINT-LINE PIC X(132). WORKING-STORAGE SECTION. 01 DATA-REMAINS-SWITCH 010101PIC X(2)VALUE SPACES.INDIVIDUAL-CALCULATIONS. 05 IND-TUITI0N 05 IND-ACTIVITY-FEE 05 IND-UNI0N-FEE 05 IND-SCHOLARSHIP 05 IND-BILLPIC PIC PIC PIC PIC9(4) 92/ 9(2) 9(4) 9(6)VALUE VALUE VALUE VALUE VALUEZEROS. ZEROS. ZEROS. ZEROS. ZEROS.UNIVERSITY-TOTALS. 05 UNI-TUITION 05 UNI-UNION-FEE 05 UNI-ACTIVITY-FEE 05 UNI-SCHOLARSHIP 05 UNI-IND-BILLPIC PIC PIC PIC PIC9(6) 9(4) 9(4) 9(6) 9(6)VALUE VALUE VALUE VALUE VALUEZEROS. ZEROS. ZEROS. ZEROS. ZEROS.CONSTANTS-AND-RATES. 05 PRICE-PER-CREDIT 05 UNION-FEE 05 ACTIVITY-FEES. 10 1ST-ACTIVITY-FEE 10 1ST-CREDIT-LIMI1 10 2ND-ACTIVITY-FEE05PIC 9(3) PIC 9(2)PIC 99 PIC 99 PIC 99 1 U CnU-^kWL I LH I - l i 99 10 3RD-ACTIVITY-FEE PIC 99 MINIMUM-SCHOLAR-GPA PIC 9V9- VALUE clauses initialize data namesVALUE 200. VALUE 25. VALUE VALUE VALUE VALUE VALUE VALUE25. 6. 50. 12. 75. 2.5.-Separate areas for heading and detail lines01HEADING-LINE. 05 FILLER 05 FILLER 05 FILLER 05 FILLERPIC X PIC X(12) PIC X(10) PIC X(7)VALUE VALUE VALUE VALUE1SPACES. STUDENT NAME' SPACES. 'CREDITS'. 98. TheTuition' ^o.63 64 65 66 67 68 69 70 71 72 73 74 [75. 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92?L W95 96 97 98 99 100 101 102 103 104 105 106 107 108 ~ 109 110 111 112Bittingi JProgram(continued)05 05 05 05 05 05 05 05 05 05 05 01FILLER FILLER FILLER FILLER FILLER FILLER FILLER FILLER FILLER FILLER FILLERDETAIL-LINE. 05 FILLER 05 DET-LAST-NAME 05 FILLER 05 DET-INITIALS 05 FILLER 05 DET-CREDITS 05 FILLER 05 DET-TUITION 05 FILLER 05 DET-UNION-FEE 05 FILLER 05 DET-ACTIVITY-FEE 05 FILLER 05 DET-SCHOLARSHIP 05 FILLER 05 DET-IND-BILL 05 FILLERPIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PICX(2) X(7) X(2) X(9) X(2) X(7) X(2) X(ll) X(2) X(10) X(48)VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUESPACES. 'TUITION'. SPACES. 'UNION FEE'. SPACES. 'ACT FEE'. SPACES. 'SCHOLARSHIP'. SPACES. 'TOTAL BILL'. SPACES.VALUE SPACES PIC X PIC X(15) PIC X(2) VALUE SPACES PIC X(2). PIC X(5) VALUE SPACES DTPQfOPIC X(6) VALUE SPACES PIC 9(6). PIC X(7) VALUE SPACES PIC PIC PIC PIC PIC PIC PICX(6) 9(3). X(8) 9(5). X(6) 9(6). X(49)VALUE SPACES VALUE SPACES VALUE SPACES VALUE SPACESareas tor dash and total lines0101DASH-LINE. 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLERPIC X(31) PIC X(8) PIC X(2) PIC X(8) PIC X(2) PIC X(7) PIC X(6) PIC X(7) PIC X(5) PIC X(7) PIC X(49)TOTAL-LINE. 05 FILLER 05 FILLER VALUE 'U 05 FILLER 05 TOT-TUITIONPIC X(8) VALUE SPACES. PIC X(17) TOTALS'. PIC X(8) VALUE SPACES. PIC 9(6).VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUESPACES. ALL '-' SPACES. ALL '-' SPACES. ALL -' SPACES. ALL '-' SPACES. ALL '-' SPACES. 99. C h a p t e rF i g u r e 4.10 113 114 115 116 117 118 119 120 1214TheIdentification,Environment,andDataDivisions(continued) 05 FILLER 05 TOT-UNION-FEE 05 FILLER 05 TOT-ACTIVITY-FEE 05 FILLER 05 TOT-SCHOLARSHIP 05 FILLER 05 TOT-IND-BILL 05 FILLERPIC PIC PIC PIC PIC PIC PIC PIC PICX(6) 9(4). X(5) 9(4). X(7) 9(6). X(6) 9(6). X(49)VALUE SPACES. VALUE SPACES. VALUE SPACES. VALUE SPACES. VALUE SPACES.The CONFIGURATION SECTION, SOURCE-COMPUTER, and OBJECTCOMPUTER entries are optional in COBOL-85 but are required in COBOL74. The LABEL RECORDS clause is optional in COBOL-85 but is required in COBOL-74. The BLOCK CONTAINS clause is optional in both compilers, but its omission has different effects. Omitting the clause in COBOL-85 causes the system to take the blocking factor from the operating environment (and is equivalent to the IBM entry BLOCK CONTAINS 0 RECORDS). Omission of the clause in COBOL-74 defaults to the implementor-designated number, regardless of what was specified in the control statements to the operating system. COBOL-85 allows two new relationships, GREATER THAN OR EQUAL TO and LESS THAN OR EQUAL TO, in the condition portion of an IF statement. These were not allowed in COBOL-74, which used NOT LESS THAN as the equivalent of GREATER THAN OR EQUAL TO. The word FILLER is optional in COBOL-85, whereas it is required in COBOL-74.SUMMARY Points IItoRememberCOBOL notation is the standardized form used to express permissible COBOL formats. Uppercase letters indicate COBOL reserved words, whereas lowercase letters denote programmer-supplied information. Brackets [ ] imply an optional entry, whereas braces { } indicate a choice between required entries. Any underlined item is required. The PROGRAM-ID paragraph is the only required entry in the Identification Division; the AUTHOR paragraph is strongly recommended. The 100. Environment Division contains the FILE-CONTROL paragraph that defines the files used in a program through SELECT statements. The FILE SECTION contains a file description for every file previously defined in a SELECT statement in the Environment Division. The file description is followed by a record description to describe the fields within a file. The PICTURE clause indicates the size and type of a data name. An elementary item always has a PICTURE clause, whereas a group item does not. Level numbers assume values from 01 to 49 inclusive, with 01 assigned to the record as a whole. Level numbers need not be assigned consecutive values. The WORKING-STORAGE SECTION contains additional record descriptions for data names not found in the FILE SECTION. VALUE clauses assign an initial value to a data name of a numeric literal, a nonnumeric literal, or a figurative constant. K e y WordsandConceptsAlphanumeric item Assumed (implied) decimal point Blocking factor Braces Brackets COBOL notation Detail line Elementary item Figurative constant File descriptionCOBOLGroup item Heading line Level numbers Logical record Numeric item Physical record Record description Size Total line TypeElementsALL ASSIGN AUTHOR BLOCK CONTAINS DATA RECORD IS DATE-COMPILED DATE-WRITTEN FD FILE SECTION FILE-CONTROL FILLER INPUT-OUTPUT SECTIONINSTALLATION LABEL RECORDS PICTURE PROGRAM-ID RECORD CONTAINS SECURITY SELECT SPACES VALUE WORKING-STORAGE SECTION ZEROS 101. C h a p t e rThe4Identification,Environment,andDataDivisionsF I L L - I N 1. TheDivision is the first division in a COBOL program.2. Theparagraph is the only required entry in the IdentificationDivision. 3. In the COBOL notation,indicate that one of the enclosed elementsmust be included. 4. Required reserved words are written inletters and are5. Lowercase letters indicateinformation.6. The Environment Division contains 7. Thesections.statement ties a programmer-chosen file name to a systemname. 8. Aitem is divided into one or more elementary items.9. An elementary item always has aclause.10. Level numbers appearing under a 01 record may range from 11. The Data Division contains thetoand12. The presence of a V in a numeric picture indicates ansections. ,decimalpoint. 13. Incoming numeric fields (may/may not) contain an actual decimal point. 14.denotes a field that is not referenced by name.15. The records in onespecifies the number of record.TRUE/FALSE 1. The Identification Division may contain up to six paragraphs. 2. The PROGRAM-ID paragraph is the only required paragraph in the Identification Division. 3. Square brackets indicate a required entry. 4. Braces imply that one of the enclosed entries must be chosen. 5. A COBOL program that runs successfully on a PC would also run successfully on a mainframe with no modification whatever. 6. A level number may assume any value from 01 to 49. 7. A 01-level entry cannot have a PICTURE clause. 8. All elementary items have a PICTURE clause. 9. A group item may have a PICTURE clause. 10. 01-level entries may appear in both the File and Working-Storage Sections of the L j a i a[_JIVIOIV_Mr. 102. 11. A data n a m e at the 10 level will always be an elementary item. 12. A data n a m e at the 05 level m a y or m a y not have a P I C T U R E clause. 13. PICTURE, P I C T U R E IS, PIC, and PIC IS are all acceptable forms of the P I C T U R E clause. 14. P I C T U R E IS 9(3) and P I C T U R E IS 999 are equivalent entries. 15. The File Section is required in every C O B O L program. 16. A n incoming numeric field m a y contain an actual decimal point. 17. The R E C O R D C O N T A I N S clause is required in an FD.PROBLEMS 1. Consider the accompanying time card. S h o w an appropriate record description for this information in C O B O L ; use any P I C T U R E clauses you think appropriate.Time-Record Name FirstMiddleNumberDateLastMOfDAHours YR2. In which division(s) do you find the a. P R O G R A M - I D paragraph?h. D A T E - C O M P I L E D paragraph?b. F I L E - C O N T R O L paragraph?i INPUT-OUTPUT SECTION? .c. C O N F I G U R A T I O N S E C T I O N ?j. File n a m e s ?d. W O R K I N G - S T O R A G E S E C T I O N ?k. Level numbers?e. FILE S E C T I O N ?I S E L E C T statements? .f FD's? .m . V A L U E clauses?g. A U T H O R paragraph?n. P I C T U R E clauses?3. Given the following record layout: 01 EMPLOYEE-RECORD. 05 SOC-SEC-NUMBER 05 EMPLOYEE-NAME. 10 LAST-NAME 10 FIRST-NAME 10 MIDDLE-INIT 05 FILLER 05 BIRTH-DATE. 10 BIRTH-MONTH 10 BIRTH-DAY 10 BIRTH-YEAR 05 FILLERPIC 9(9). PIC X(12). PIC X(10). PIC X. PIC X. PIC PIC PIC PIC99. 99. 99. X(3). 103. C h a p t e rThe405Identification,Environment,andDataDivisionsEMPLOYEE-ADDRESS. 1 Am t D n A r T i T i U c_ M CD v r c15 HOUSE-NUMBER 15 STREET-NAME 10 CITY-STATE-ZIP. 15 CITY 15 STATE 15 ZIP 05 FILLER a. List all group items.PIC X(6). PIC X(10). PIC PIC PIC PICX(10). X(4). 9(5). X(3).b. List all elementary items. c. State the record positions in which the following fields are found: SOC-SEC-NUMBER EMPLOYEE-NAME LAST-NAMEFIRST-NAME MIDDLE-INIT BIRTH-DATE BIRTH-MONTH BIRTH-DAYBIRTH-YEAR EMPLOYEE-ADDRESS sNUMBER-AND-STREETHOUSE-NUMBERSTREET-NAMEKCITY-STATE-ZIP CITY STATE . |p sZ4. Given the following record layout (assume that FIELD-I is the last entry under FIELD-A), 01 FIELD-A 05 FIELD-B 10 FIELD-C 10 FIELD-D 05 FIELD-E 05 FIELD-F 10 FIELD-G 10 FIELD-H 10 FIELD-I answer true or false. a. FIELD-C is an elementary item. b. FIELD-E is an elementary item. c. FIELD-E should have a picture. d. FIELD-F should have a picture. e. FIELD-B must be larger than FIELD-C. f. FIELD-C must be larger than FIELD-D. g. FIELD-C must be larger than FIELD-H. 104. h. FIELD-B and FIELD-D end in the s a m e column. i FIELD-A and FIELD-i end in the s a m e column, . j. FIELD-E could be larger than FIELD-F. k. FIELD-D could be larger than FIELD-E. I FIELD-F and FIELD-G start in the s a m e column. . 5. Use the C O B O L notation introduced at the beginning of the chapter and the general format of the F D entry to determine whether the following are valid F D entries. a. FD EMPLOYEE-FILE. b. FD EMPLOYEE-FILE BLOCK CONTAINS 10 RECORDS RECORD CONTAINS 100 CHARACTERS LABEL RECORDS ARE STANDARD DATA RECORD IS EMPLOYEE-RECORD. C. FD EMPLOYEE-FILE BLOCK 10 RECORDS RECORD 100 CHARACTERS LABEL RECORDS STANDARD DATA RECORD EMPLOYEE-RECORD. 6. indicate whether each of the following entries is speiied correctly and whether it is syntactically valid. a. E N V I R O N M E N T DIVISION b. W O R K I N G - S T O R A G E - S E C T I O N c. IDENTIFICATION-DIVISION d. WRITTEN-BY e. DATA-DIVISION f FILE S E C T I O N . g. P R O G R A M ID h. D A T E - W R I T T E N i.DATE-EXECUTEDj. I N P U T - O U T P U T S E C T I O N k. F I L E - C O N T R O L S E C T I O N I D A T E DIVISION . m. C O M M E N T S 105. Overview PE& CLOSE R A E DPlacement of the READ Statement W RT I ESTOP R U N MOVERestrictions on the MOVE Statement Alphanumeric Field to Alphanumeric Field Numeric Field to Numeric Field Group MovesThe ROUNDED Clause The SIZE ERROR Clause COMPUTE ADD SUBTRACT MULTIPLY DIVIDE Programming Tip: Use the COMPUTE Statement Assumed Decimal Point T h e Tuition Billing P r o g r a mTest Data Hierarchy ChartPERFORMC O B O L Program SkeletonIFLimitations of COBOL-74 Summary Fill-inThe ELSE Clause Indentation EVALUATE Arithmetic StatementsTrue/False Problems 106. C h a p t e rSTheProcedureDivisionOBJECTIVES After reading this chapter you will be able to: MWrite the OPEN, CLOSE, READ, and WRITE statements necessary for sequential file processing.IDescribe the purpose of the priming (initial) READ statement, and place it correctly in the Procedure Division.Discuss the rules of the MOVE statement as they apply to numeric and alphanumeric fields.MDescribe the PERFORM statement; show how this statement is used to process a file until all of its records have been read.iDescribe the IF statement and how it is used with and without an ELSE clause; explain the significance of the END-IF scope terminator.*Use the EVALUATE statement to implement a case (multibranch) construct.HState the hierarchy of operations for a COMPUTE statement; describe the individual arithmetic statements, ADD, SUBTRACT, MULTIPLY, and DIVIDE.MDescribe the ROUNDED and SIZE ERROR options as they apply to any of the arithmetic statements.BExplain the relationship between a Procedure Division and its associated hierarchy chart.OVERVIEW This is a long chapterthe longest in the text. It focuses on the Procedure Division, which is the portion of a COBOL program that contains the logic. The chapter is long because it presents the many statements needed to write a basic program such as the tuition billing program introduced in Chapter 3. We begin with the COBOL statements used for I/O (input/output) operations; OPEN, CLOSE, READ, and WRITE, and continue with the STOP RUN statement to terminate program execution. We learn about the PERFORM statement to implement a loop, the IF statement to implement the selection structure, and the EVALUATE statement to implement a case structure. We study the MOVE statement to copy data from one location to another and end with the arithmetic statements: COMPUTE, ADD, SUBTRACT, MULTIPLY, and DIVIDE. The chapter concludes with the completed COBOL listing for the tuition billing program of Chapter 3.OPENT h e OPEN s t a t e m e n t initiates p r o c e s s i n g for a file. It indicates the nature of t h e file (input or output) a n d e n s u r e s that a specific device is available for the I/O operations. T h e OPEN s t a t e m e n t also performs validation functions in c o n j u n c t i o n with t h e LABEL 107. RECORDS c l a u s e of the FD; for example, if label records are specified for a n input file, the OPEN" s t a t e m e n t checks the header label of that file to ensure that the proper file is available for p r o c e s s i n g . An abbreviated format of t h e OPEN s t a t e m e n t is:If INPUT 1 OPEN} file-name-1 . . (OUTPUT] T h e s y n t a x o f t h e O P E N s t a t e m e n t i n d i c a t e s a m a n d a t o r y s e l e c t i o n for t h e t y p e o f f i l e I N P U T is u s e d for a file that is read, w h e r e a s O U T P U T is u s e d for a file that is w r i t t e n to. T h e b r a c k e t s a n d e l l i p s i s a s s o c i a t e d w i t h f i l e - n a m e - 2 i m p l y t h a t m u l t i p l e files c a n b e o p e n e d i n t h e s a m e s t a t e m e n t a s w a s d o n e i n l i n e s 4 3 a n d 4 4 o f t h e e n g i n e e r i n g s e n i o r p r o g r a m i n Figure 1.6:OPEN INPUT STUDENT-FILE OUTPUT PRINT-FILE. E a c h file r e f e r e n c e d i n a n O P E N s t a t e m e n t m u s t h a v e b e e n p r e v i o u s l y d e f i n e d i n a SELECT s t a t e m e n t i n t h e E n v i r o n m e n t D i v i s i o n , a n d i n a c o r r e s p o n d i n g F D i n t h e D a t a D i v i s i o n . All files m u s t b e o p e n e d b e f o r e t h e y c a n b e a c c e s s e d ; t h e o p e r a t i n g s y s t e m will t e r m i n a t e e x e c u t i o n o f a COBOL p r o g r a m t h a t a t t e m p t s t o r e a d (or write) a n u n o p e n e d file.CLOSEThe CLOSE s t a t e m e n t is e x e c u t e d w h e n a c c e s s to a file is n o longer necessary, s u c h as w h e n all records have b e e n read from a n input file or w h e n all records h a v e b e e n written to a n o u t p u t file. The CLOSE s t a t e m e n t releases the I/O devices a s s o c i a t e d with t h e file; it also writes trailer labels at t h e e n d of files o n disk or tape in c o n j u n c t i o n with t h e LABEL RECORDS clause of the FD. All o p e n files s h o u l d b e c l o s e d before processing terminates. T h e format of the CLOSE is simply: CLOSE f i l e - n a m e - 1 [, f i l e - n a m e - 2 . . . ] T h e brackets and ellipsis associated w i t h file-name-2 indicate that multiple files c a n b e c l o s e d i n t h e s a m e s t a t e m e n t . T h e t y p e o f file, I N P U T or O U T P U T , i s n o t s p e c i f i e d w h e n t h e file is c l o s e d b e c a u s e t h e d i s t i n c t i o n b e t w e e n i n p u t a n d o u t p u t is n o l o n g e r i m p o r t a n t . L i n e s 5 1 a n d 5 2 i n t h e e n g i n e e r i n g s e n i o r p r o g r a m p r o v i d e an example:CLOSE STUDENT-FILE PRINT-FILE. A CLOSE s t a t e m e n t c a n a p p e a r a n y w h e r e w i t h i n a p r o g r a m b u t t y p i c a l l y a p p e a r s i m m e d i a t e l y b e f o r e t h e p r o g r a m t e r m i n a t e s , that is, i m m e d i a t e l y b e f o r e t h e STOP RUN statement.READThe READ s t a t e m e n t transfers data from a n o p e n file into m e m o r y , p r o v i d e d a record is available. If, however, n o record is presentthat is, t h e end-of-file c o n d i t i o n has b e e n reachedcontrol p a s s e s to the statement(s) following the AT END clause. An abbreviated format of the READ s t a t e m e n t is s h o w n below: n m u i i i e-nanieAT END statement [END-READl 108. C h a p t e r5TheProcedureDivisionT h e E N D - R E A D s c o p e t e r m i n a t o r is o p t i o n a l b u t s t r o n g l y r e c o m m e n d e d . T h e READ s t a t e m e n t is illustrated i n l i n e s 4 5 - 4 7 o f t h e e n g i n e e r i n g s e n i o r p r o g r a m .READ STUDENT-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH END-READ.P l a c e m e n t f t h e R E A D S t a t e m e n t T h e e n g i n e e r i n g s e n i o r p r o g r a m i n F i g u r e 1.6 c o n t a i n e d t w o d i s t i n c t READ s t a t e m e n t s . T h e r e w a s a n initial, o r p r i m i n g , READ i n l i n e s 4 5 - 4 7 a n d a s e c o n d READ s t a t e m e n t a s t h e last i n s t r u c t i o n o f t h e p e r f o r m e d p a r a g r a p h (lines 6 5 - 6 7 ) . T h e n e c e s s i t y for both s t a t e m e n t s is e x p l a i n e d b y c o n s i d e r i n g Figure 5 . 1 , w h i c h s h o w s c o r r e c t a n d i n c o r r e c t w a y s t o p r o c e s s a file o f t r a n s a c t i o n s . Figure 5.1a, t h e incorrect i m p l e m e n t a t i o n , c a u s e s t h e last r e c o r d o f I N P U T FILE t o b e p r o c e s s e d t w i c e . T o s e e h o w this h a p p e n s , c o n s i d e r a file w i t h o n l y t w o r e c o r d s , A a n d B, r e a l i z i n g t h a t s u c h a file i s r e a d t h r e e t i m e s o n c e for e a c h r e c o r d a n d o n c e t o s e n s e t h e e n d o f file. Realize, t o o , that t h e PERFORM s t a t e m e n t e v a l u a t e s t h e U N T I L c o n d i t i o n before b r a n c h i n g (a d e t a i l e d d e s c r i p t i o n o f t h e PERFORM s t a t e m e n t is f o u n d i n a n u p c o m i n g s e c t i o n ) . In Figure 5.1a, r e c o r d A is r e a d t h e first t i m e PROCESS-RECORDS is p e r f o r m e d , with e x e c u t i o n c o n t i n u i n g through t h e r e m a i n d e r of t h e PROCESS-RECORDS p a r a g r a p h , at w h i c h p o i n t DATA-REMAINS-SWITCH is still s e t t o 'YES'. H e n c e , PROCESS-RECORDS is e x e c u t e d a s e c o n d t i m e , d u r i n g w h i c h t i m e it r e a d s a n d p r o c e s s e s r e c o r d B. S i n c e DATA-REMAINS-SWITCH is still s e t t o 'YES', PROCESSRECORDS i s e x e c u t e d a third t i m e , d u r i n g w h i c h t h e e n d - o f - f i l e c o n d i t i o n i s s e n s e d immediately. Execution c o n t i n u e s , however, to t h e e n d o f t h e paragraph, c a u s i n g t h e last r e c o r d (record B) t o b e p r o c e s s e d t w i c e . In t h e correct i m p l e m e n t a t i o n o f F i g u r e 5.1b, a n initial (priming) READ is e x e c u t e d before p e r f o r m i n g t h e paragraph PROCESS-RECORDS, w h i c h also c o n t a i n s a READ s t a t e m e n t . T h e first t i m e PROCESS-RECORDS is p e r f o r m e d , it p r o c e s s e s r e c o r d A, a n d its last s t a t e m e n t r e a d s r e c o r d B. S i n c e DATA-REMAINS-SWITCH i s still s e t t o 'YES', PROCESS-RECORDS is e x e c u t e d a s e c o n d t i m e t o p r o c e s s r e c o r d B, w i t h t h e e n d i n g READ s t a t e m e n t s e n s i n g t h e e n d - o f - f i l e c o n d i t i o n . DATA-REMAINSSWITCH is s e t t o 'NO', w h i c h i n turn t e r m i n a t e s t h e PERFORM s t a t e m e n t .WRITEThe WRITE s t a t e m e n t transfers data from m e m o r y to t h e printer (or other o p e n o u t p u t device). Consider:WRITE record-name AFTER ADVANCING [BEFOREjintegerLINE LINESPAGET h e A D V A N C I N G o p t i o n c o n t r o l s t h e l i n e s p a c i n g o n a printer; for e x a m p l e , s p e c i f i c a t i o n o f AFTER A D V A N C I N G 3 LINES p r o d u c e s triple s p a c i n g ( t h e printer s k i p s t w o l i n e s a n d w r i t e s o n t h e third). C o n v e r s e l y , s p e c i f i c a t i o n o f t h e BEFORE o p t i o n first w r i t e s t h e l i n e , t h e n s k i p s t h e d e s i g n a t e d a m o u n t . S p e c i f i c a t i o n o f 109. WRITEFiu.* 6 Placement of the R E A D StatementPREPARE-TUITION-REPORT.MOVE 'YES' TO DATA-REMAINS-SWITCH. PERFORM PROCESS-RECORDS UNTIL DATA-REMAINS-SWITCH = 'NO'./ Hrst statement or performed paragraph is the REAUPROCESS-RECORDS. READ INPUT-FILE | , AT END MOVE 'NO' TO DATA-REMAINS-SWITCH Y END-READ.(a) Incorrect ImplementationPREPARE-TUITION-REPORT.MOVE 'YES' TO DATA-REMAINS-SWITCH. READ INPUT-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH END-READ. PERFORM PROCESS-RECORDS UNTIL DATA-REMAINS-SWITCH = 'NO'.irnmi READ is mmciAeci once and only oncePROCESS-RECORDS., Last statement o' performed paragraph is another READREAD INPUT-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH END-READ. (b) Correct If 110. Chapter5TheProcedureDivisionPAGE, in lieu of LINES, will c a u s e o u t p u t to b e g i n o n t o p of a n e w p a g e . O m i s s i o n of t h e A D V A N C I N G o p t i o n d e f a u l t s to s i n g l e s p a c i n g . T h e e x a m p l e s b e l o wWRITE PRINT-LINE. WRITE PRINT-LINE AFTER ADVANCING 2 LINES. WRITE PRINT-LINE AFTER ADVANCING PAGE. will s i n g l e s p a c e , d o u b l e s p a c e , a n d a d v a n c e to t h e t o p of a n e w p a g e , r e s p e c t i v e l y . T h e WRITE s t a t e m e n t c o n t a i n s a record n a m e , w h e r e a s t h e READ s t a t e m e n t c o n t a i n s a file n a m e . T h e r e c o r d n a m e i n t h e WRITE s t a t e m e n t will a p p e a r as a 0 1 e n t r y i n t h e File S e c t i o n of t h e D a t a D i v i s i o n . T h e file i n w h i c h it is c o n t a i n e d will a p p e a r in SELECT, F D , OPEN, a n d CLOSE s t a t e m e n t s .The format of the STOP RUN s t a t e m e n t is simply:STOP RUN T h e STOP R U N s t a t e m e n t t e r m i n a t e s e x e c u t i o n of a COBOL p r o g r a m a n d returnscontrol to the operating system. [STOP R U N n e e d not b e ( a n d typically is not) the last p h y s i c a l s t a t e m e n t in t h e p r o g r a m . ] All files s h o u l d b e c l o s e d prior t o e x e c u t i n g t h e STOP R U N s t a t e m e n t .T h e MOVE s t a t e m e n t c o p i e s d a t a f r o m o n e l o c a t i o n t o a n o t h e r ; for e x a m p l e , t h e s t a t e m e n t MOVE A TO B c o p i e s t h e v a l u e i n l o c a t i o n A to l o c a t i o n B. T h e v a l u e of A is i n t w o p l a c e s after t h e m o v e h a s t a k e n p l a c e , w h i l e t h e initial v a l u e o f B is g o n e ( h a v i n g b e e n r e p l a c e d b y t h e v a l u e of A). T h e s y n t a x of t h e M O V E s t a t e m e n t is: [ i d e n t i f i e r - ll MOVE { ) TO i d e n t i f i e r - 2 [literal-1 j ~r L, identifier-3 J. ..Consider the following examples: 1. MOVE200 TO PRICE-PER-CREDIT. 2. MOVE 'ABC UNIVERSITY' T O S C H O O L - N A M E . 3. MOVE S T U - N A M E TO P R I N T - N A M E . 4. MOVE ZEROS TO T O T A L - N U M B E R . 5. MOVE SPACES TO PRINT-LINE. E x a m p l e o n e m o v e s a n u m e r i c literal, 200, t o t h e d a t a n a m e PRICE-PERCREDIT. E x a m p l e t w o m o v e s a n o n n u m e r i c literal, 'ABC UNIVERSITY', t o SCHOOLN A M E . E x a m p l e t h r e e c o p i e s d a t a f r o m a n i n p u t a r e a t o a n o u t p u t a r e a for s u b s e q u e n t p r i n t i n g . E x a m p l e s four a n d five u s e t h e figurative c o n s t a n t s , ZEROS a n d SPACES, t o initialize a c o u n t e r a n d print line, r e s p e c t i v e l y . T h e b r a c k e t s a n d e l l i p s i s a s s o c i a t e d w i t h identifier-3 i n t h e COBOL s y n t a x indicate the s a m e item c a n b e m o v e d to multiple data n a m e s . Thus the single statement:MOVE 10 TO FIELD-A FIELD-B FIELD-C. 111. is e q u i v a l e n t to t h e t h r e e i n d i v i d u a l s t a t e m e n t s : MOVE 10 TO F I E L D - A .MOVE 10 TO FIELD-B. MOVE 10 TO FIELD-C.T h e r e s u l t s o f a MOVE s t a t e m e n t d e p e n d o n t h e t y p e of d a t a i n t h e s e n d i n g a n d / o r r e c e i v i n g field. W e c o n c e n t r a t e i n i t i a l l y o n MOVE s t a t e m e n t s i n v o l v i n g o n l y e l e m e n t a r y i t e m s , s i n c e t h e s e s t a t e m e n t s are b y far t h e m o s t c o m m o n . Recall ( f r o m C h a p t e r 4) that e l e m e n t a r y data i t e m s m a y b e of four t y p e s : NumericN u m e r i c d a t a i t e m s , n u m e r i c literals, a n d t h e figurative c o n s t a n t s , ZERO, ZEROS, or ZEROES.AlphabeticA l p h a b e t i c d a t a i t e m s a n d t h e figurative c o n s t a n t s , SPACE a n d SPACESAlphanumericA l p h a n u m e r i c d a t a i t e m s , n o n n u m e r i c literals a n d t h e figurative c o n s t a n t s , SPACE a n d SPACESN u m e r i c EditedN u m e r i c e d i t e d d a t a i t e m s (to be d i s c u s s e d in C h a p t e r 7)In theory a MOVE s t a t e m e n t could involve any c o m b i n a t i o n of these four t y p e s ; i n actuality, h o w e v e r , c e r t a i n t y p e s of m o v e s are n o t p e r m i t t e d a s i n d i c a t e d b y T a b l e 5.1. (You d o n o t h a v e to c o m m i t t h e t a b l e to m e m o r y ; s i m p l y b e aware t h a t c e r t a i n r e s t r i c t i o n s exist, a n d k n o w w h e r e to turn s h o u l d q u e s t i o n s a r i s e later.) iRules of the MOVE Statement (Elementary Data Items)AlphabeticValidValidInvalidAlphanumericInvalidValidInvalidInvalidInvalidNumericInvalidIntegers onlyValidValidNumeric EditedInvalidValidValidInvalidAt first g l a n c e T a b l e 5.1 s e e m s o v e r w h e l m i n g , b u t a s e c o n d l o o k s h o w s it to m a k e i n t u i t i v e s e n s e . Y o u c a n n o t , for e x a m p l e , m o v e a n a l p h a n u m e r i c field t o a n a l p h a b e t i c field ( b e c a u s e t h e a l p h a n u m e r i c field m a y c o n t a i n n u m b e r s , w h i c h are invalid i n a n a l p h a b e t i c field). Y o u c a n , h o w e v e r , d o t h e m o v e i n t h e o p p o s i t e d i r e c t i o n ; that is, y o u c a n m o v e a n a l p h a b e t i c field t o a n a l p h a n u m e r i c field. E v e n T a b l e 5.1 d o e s n o t tell u s e v e r y t h i n g w e n e e d to k n o w a b o u t t h e MOVE s t a t e m e n t . What h a p p e n s , for e x a m p l e , w h e n m o v e s w i t h like fields (an a l p h a n u m e r i c s e n d i n g field to a n a l p h a n u m e r i c r e c e i v i n g field) i n v o l v e PICTURE c l a u s e s of different l e n g t h s ? A d d i t i o n a l e x p l a n a t i o n is r e q u i r e d a s e x p l a i n e d i n t h e n e x t t w o s e c t i o n s .Data moved from an alphanumeric field to an alphanumeric field are moved one character at a time from left to right. If t h e r e c e i v i n g field is larger t h a n t h e s e n d i n g field, it is p a d d e d o n t h e right w i t h b l a n k s ; if t h e r e c e i v i n g field is s m a l l e r t h a n t h e s e n d i n g field, t h e r i g h t m o s t c h a r a c t e r s are t r u n c a t e d . A l p h a n u m e r i c m o v e s are illustrated in T a b l e 5.2. E x a m p l e (a) is trivial, i n t h a t t h e s e n d i n g a n d r e c e i v i n g fields h a v e t h e s a m e p i c t u r e c l a u s e . In e x a m p l e (b) t h e s e n d i n g field is o n e c h a r a c t e r l o n g e r t h a n t h e r e c e i v i n g field; h e n c e t h e r i g h t m o s t 112. ChapterThe5ProcedureDivisionc h a r a c t e r is t r u n c a t e d . D a t a are m o v e d f r o m left to right o n e c h a r a c t e r at a t i m e ; t h u s A, B, C, a n d D are m o v e d i n that order, a n d E is d r o p p e d . In e x a m p l e (c), h o w e v e r , t h e r e c e i v i n g field is o n e c h a r a c t e r l o n g e r t h a n t h e s e n d i n g field. A, B, C, D , a n d E are m o v e d i n that order, a n d a b l a n k is a d d e d at t h e right. T A B L E5 , 2Illustration of the MOVE Statement: Alphanumeric Sending Field to Alphanumeric Receiving FieldMCTOTREICTIJHECONTENTS(a)X(5)A B(b)X(5)C [ D E D E A B CX(5)A B CA B C | DEX(4)(c)fEMTSX(6)jIX(5)Aj BCDA B C D EAll moves involving numeric fields maintain decimal alignment.If t h e i n t e g e r p o r t i o no f t h e r e c e i v i n g field is larger t h a n that o f t h e s e n d i n g field, h i g h - o r d e r (insignificant)zeros are a d d e d to the receiving field. If, however, t h e integer p o r t i o n of the receiving field is s m a l l e r t h a n that o f t h e s e n d i n g field, t h e h i g h - o r d e r (significant) digits of t h e s e n d i n g field are t r u n c a t e d . In s i m i l a r f a s h i o n if t h e d e c i m a l p o r t i o n o f t h e r e c e i v i n g field is larger t h a n t h a t o f t h e s e n d i n g field, l o w - o r d e r z e r o s are a d d e d . A n dfinally,if t h e d e c i m a lp o r t i o n o f t h e r e c e i v i n g field is s m a l l e r t h a n t h a t o f t h e s e n d i n g field, t h e extra p o s i t i o n s are t r u n c a t e d . T h e s e p o i n t s are clarified i n T a b l e 5.3.Illustration of the MOVE Statement: Numeric Sending Field to Numeric Receiving FieldcPICTUREi.-(a)9(5)1 2 3 4 59(5)1 2 3 4 5(b)9(5)1 2 3 4 59(4)2 3 4 5(c)9(5)2 3 4 59(6)0 1 2 3 4 59(3)1 2 39V993 4 5V(d)9(3)V99 1(e)9(3)V99(f)9(3)1 2 3 41 5 v 1 2 3 4 5V1 239(3)V99E x a m p l e (a) is trivial. E x a m p l e (b) a t t e m p t s t o m o v e a1 2 3 0 0five-positionfieldto af o u r - p o s i t i o n field. S i n c e d e c i m a l a l i g n m e n t is a l w a y s m a i n t a i n e d , t h e l e f t m o s t digit (i.e., t h emost significantdigit) is t r u n c a t e d . E x a m p l e (c) m o v e s afive-positions e n d i n g field t o a s i x - p o s i t i o n r e c e i v i n g field, c a u s i n g t h e a d d i t i o n o f a l e a d i n g ( n o n s i g n i f i c a n t ) z e r o . T h e s e n d i n g field in e x a m p l e (d) h a s t w o digits after t h e d e c i m a l p o i n t , b u t t h e r e c e i v i n g field h a s n o n e . H e n c e t h e 4 a n d 5 d o n o t a p p e a r i n t h e r e c e i v i n g field. E x a m p l e (e) t r u n c a t e s t h e m o s t s i g n i f i c a n t d i g i t s . E x a m p l e (f) adds two nonsignificant zeros to the receivingfield. 113. T h e preceding discussion concerned M O V E statements in which the receiving field w a s a n elementary item. T h e results are very different if a group item is involved,because if the receiving field is a group item, the move takes place as though the receiving field were an alphanumeric item, with padding or truncation on the right as necessary. M O V E statements involving group items often produce unexpected results a n d should b e avoided.The P E R F O R M statement transfers control to a procedure (paragraph) elsewhere in the program, allowing the program to be divided into functional modules. A n abbreviated format of the PERFORM statement is: PERFORM procedure-name [UNTIL condition] Consider first t h e s t a t e m e n t without ra n UNTIL clause as illustrated below:COMPUTE TUITION = CREDITS * CHARGE-PER-CREDIT. - PERFORM WRITE DETAIL-LINE. ADD 1 TO NUMBER-OF-STUDENTS.WRITE-DETAIL-LINE. j MOVE STUDENT-NAME TO PRINT-NAME. I MOVE TUITION TO PRINT-TUITION.WRITE PRINT-LINE AFTER ADVANCING 2 L I N E S . WRITE-TOTAL-LINE. JT h e statement P E R F O R M W R I T E - D E T A I L - L I N E transfers control to the first statement in the paragraph W R I T E - D E T A I L - L I N E . W h e n every statement in W R I T E D E T A I L - L I N E h a s b e e n executed (i.e., w h e n the next p a r a g r a p h n a m e is encountered), control returns to the statement immediately after the original P E R F O R M , in this case, to the A D D statement. A loop (iteration) is i m p l e m e n t e d through inclusion of a n U N T I L clause. T h e condition in the U N T I L clause is tested before the paragraph is executed, a n d if the condition is not m e t , control is transferred to the designated paragraph. W h e n the paragraph has completed execution, the condition is retested, a n d if it (the condition) is still not met, the paragraph is executed a second time. T h e process continues until the condition is finally satisfied. Consider: PERFORM PROCESS-RECORDS UNTIL DATA-REMAINS-SWITCH = 'NO'.PROCESS-RECORDS.READ STUDENT-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH END-READ. 114. C h a p t e r5-TheProcedureDivisionT h e paragraph P R O C E S S - R E C O R D S is executed repeatedly until D A T A REIvLAJNS-SWITCH equals 'NO', that is, until there are n o m o r e incoming records. T h e last statement of the performed paragraph is a R E A D statement, so that w h e n the e n d of file is reached, D A T A - R E M A I N S - S W I T C H will be set to 'NO'. This causes the next test of the U N T I L condition to b e successful a n d prevents further execution of the P R O C E S S - R E C O R D S paragraph.IFT h e IF statement is one of the most powerful statements in C O B O L . O u r present concern, however, is with only a few of the available options, with additional consideration deferred to Chapter 8. A n abbreviated format of the IF statement is J_F condition THEN statement-1 [ELSE statement-2 ] [END-IFl T h e IF statement is terminated by the optional (but highly r e c o m m e n d e d ) E N D - I F scope terminator and/or a period. Consider: IF STU-CREDITS > 110 AND STU-MAJOR = 'ENGINEERING' MOVE STU-NAME TO PRINT-NAME MOVE STU-CREDITS TO PRINT-CREDITS MOVE STU-GPA TO PRINT-GPA WRITE PRINT-LINE END-IF. If the condition is true, then every statement between the IF (condition) a n d the E N D - I F (and/or period) will be executed. H e n c e , w h e n a n engineering senior is processed, three M O V E statements a n d o n e W R I T E statement are executed. If, however, the condition is false, then all four statementsthree M O V E s a n d a W R I T E a r e bypassed. A s indicated, the IF statement is terminated b y the E N D - I F scope terminator a n d / or a period, a n d the inclusion of both appears redundant. ( M a n y p r o g r a m m e r s do, however, use both entries.) E N D - I F , despite the fact that it is a n optional entry, has distinct advantages (as will be explained in Chapter 7) a n d should b e used in every instance.The E L S E Clause T h e ELSE clause is optional as implied by the square brackets in its syntax. Figure 5.2a contains a n E L S E clause, whereas it is omitted in Figure 5.2b. If the condition in Figure 5.2a is true, statement-1 is executed; whereas if it is false, statement-2 is executedin either case execution continues with statement-3. Figure 5.2b, however, omits the E L S E clause so that if the condition is false, the IF statement is terminated immediately.Indentation Indentation in a n IF statement is extremely important to emphasize a programmer's understanding of a statement's intended effect. Consider Figure 5.3, w h i c h contains a flowchart a n d corresponding C O B O L code. 115. F i g u r e 5.2The IF StatementSTATEMENT,STATEMENTSTATEMENT,fa) With E L S E OptionSTATEMENT,STATEMENT,(a) Without E L S E OptionT h e flowchart in Figure 5.3a indicates that if the condition A - B is true, the statements M O V E I T O C a n d M O V E 1 T O I) are to be executed. If, however, the condition is false, then the statements M O V E Z E R O T O C a n d M O V E Z E R O T O D are to b e executed instead. In either casethat is, whether the condition is true or falsewe are to write a detail line. T h e latter is indicated b y the IF a n d E L S E branches meeting in a c o m m o n exit point, which leads to thefinalW R I T E statement. T h e C O B O L code in Figure 5.3b is carefully aligned to reflect this interpretation. Recall that the rules of C O B O L require only that a n IF statement appear in the B margin, that is, in c o l u m n s 12-72. H e n c e the indentation in Figure 5.3b is d o n e solely for the purpose of m a k i n g a p r o g r a m easier to read, rather than to satisfy a 116. C h a p t e r;ig.5ta S.JThe5ProcedureDivisionThe ELSE Clause/llFALSETRUEf1>MOVE ZERO TOCMOVE 1 TOC> MOVE ZERO TODfMOVE 1 TODX WRITE DETAIL-LINEMOVE 1 TO C MOVE 1 TO D ELSE MOVE ZERO TO C MOVE ZERO TO D END-IF. WRITE DETAIL-LINE.b ) C O B O L Coderule of C O B O L . Nevertheless, proper indentation is essential a n d goes a long w a y to improve the quality of your work. Accordingly, w e suggest the following guidelines: 1. Begin the IF statement in c o l u m n 12. 2. Put the w o r d E L S E o n a line by itself a n d directly under the IF. 3. Indent detail lines associated with either the IF or E L S E four columns. 4. Put 1-ND-IF o n a line b y itself directly under the IF statement. 117. 1 h e EVALUATE statement i m p l e m e n t s the case (rnultibranch) construct of structured programming. It has w h a tfirstappears to be a rather complicated syntax, but in actuality is quite easy to use. Consider: identi fier-1 EVALUATEexpressionTRUE FALSE condition-1WHENimperati ve-statement - 1TRUE FALSE[WHEN OTHER imperative-statement.-2] [END-EVALUATE] A n example of the E V A L U A T E statement is s h o w n below in conjunction with the tuition billing p r o g r a m presented in Chapter 3. T h e specifications for the p r o g r a m indicate that activity fee is dependent o n the n u m b e r of credits ($25 for 6 credits or fewer, $50 for 7 to 12 credits, a n d $75 for 13 credits or more). Consider: EVALUATE TRUE WHEN STU-CREDITS 6 AND STU-CREDITS 12 MOVE 75 TO IND-ACTIVITY-FEE END-EVALUATE. T h e different conditions (i.e., the ranges for the n u m b e r of student credits) are presented in the various W H E N clauses. T h e E N D - E V A L U A T E scope terminator is a required entry.C O B O L does arithmetic in o n e of two ways. It has individual statements for the basic arithmetic operations (addition, subtraction, multiplication, a n d division), a n d a C O M P U T E statement that combines multiple operations into o n e statement. A s y o u shall see, the C O M P U T E statement is generally easier to use, a n d so w e begin with it. Note, too, that all of these statements have optional R O U N D E D a n d SIZE E R R O R clauses, w h i c h are discussed prior to the individual statements.Th R t Li.vl'-j-cJ/C.~.seT h e R O U N D E D clause (in a n y arithmetic statement) causes C O B O L to carry a calculation to o n e m o r e decimal place than is specified in the result field. If the value of the extra decimal place is 5 or larger, the answer is r o u n d e d up; if it is 4 or less, the answer is u n c h a n g e d . If the R O U N D E D clause is omitted, C O B O L truncates any extra decimal positions regardless of their value. Table 5.4 s h o w s the effect of the R O U N D E D option in w h i c h the values of A a n d B are a d d e d to produce a value for C. 118. C h a p t e rThe5ProcedureDivisionThe ROUNDED Clause#99 Value before execution123456ADD A B GIVING C12345657ADD A B GIVING C ROUNDED12345658(immaterial)Value after execution ofBoth of the examples in Table 5.4 a d d the s a m e n u m b e r s (1.23 a n d 4.56) to produce a s u m of 5.79. Both examples also specify the s a m e P I C T U R E clause for the s u m , w h i c h contains only a single decimal place. T h efirststatement, however, does not contain the R O U N D E D clause, a n d hence the .09 is truncated, leaving 5.7 as the final answer. T h e second example contains the R O U N D E D clause, producing a m o r e accurate 5.8.T h e SIZE E R R O R clause is available for all arithmetic statements a n d produces a warning w h e n the result of calculation is too large for the designatedfield.Consider: 05 05 05HOURLY-RATE HOURS-WORKED GROSS-PAYPIC 99. PIC 99. PIC 999.COMPUTE GROSS-PAY = HOURLY-RATE * HOURS-WORKED. Let us a s s u m e that H O U R L Y - R A T E a n d H O U R S - W O R K E D are 25 a n d 40, respectively. T h e result of the multiplication should b e 1,000. G R O S S - P A Y , however, is defined as a three-position numeric field a n d is too small to hold the result. H e n c e its value is truncated a n d only the three rightmost digits are retained; in other words, G R O S S - P A Y b e c o m e s 000. T h e situation is prevented b y the inclusion of the SIZE E R R O R clause: COMPUTE GROSS-PAY = HOURLY-RATE * HOURS-WORKED ON SIZE ERROR PERFORM ERROR-ROUTINE END-COMPUTE. This time, if the results of the computation are too large a n d exceed the size allotted in the P I C T U R E clause, control passes to the statement(s) following the SIZE E R R O R clause. T h e latter contains a n error routine to display a n error message or take other corrective action.T h e C O M P U T E statement c o m b i n e s multiple arithmetic operations into a single statement of the form: COMPUTE {identifier-1 [RQtJNDED]} = expression-1 [ON SIZE ERROR imperative-statement-1] fEND-COMPUTEl T h e C O M P U T E statementfirstcalculates the value of the expression o n the right side of the equal sign, then stores that value in the data n a m e o n the left. T h e 119. ArithmeticStatementsexpression within the C O M P U T E statement consists of data n a m e s , numeric literals, arithmetic symbols, a n d parentheses. Spaces should precede a n d follow arithmetic symbols. A space is also required before a left parenthesis a n d after a right parenthesis. Parentheses are used to clarify, a n d in s o m e cases, alter the sequence of, operations within a n expression, but anything contained within parentheses m u s t also be a valid expression. Expressions are evaluated according to the following rules: 1. Anything contained in parentheses is evaluated first as a separate expression. 2. T h e symbols +, -, *, /, a n d ** denote addition, subtraction, multiplication, division, a n d exponentiation, respectively. Exponentiation is d o n e first, then multiplication or division, then addition or subtraction. 3. If rule 2 results in a tie (e.g., if both multiplication a n d division are present), then evaluation proceeds from left to right. Table 5.5 contains examples to illustrate the formation a n d evaluation of expressions in a C O M P U T E statement.TAI1LE S.M The COMPUTE instruction2Initial ValuesCOMPUTE C = A + B.2Simple additionCOMPUTE C = A + B * 2.2Value before execution Value after execution ofMultiplication before addition Parenthesis evaluated first10COMPUTE C = (A + B ) * 2.2COMPUTE C = A * * B.2Algebraically, c = aCOMPUTE C = B " A.2Draically, c = b0aTable 5.6 should further clarify the use of this all-important statement. This table contains several algebraic expressions a n d the corresponding C O M P U T E statements to accomplish the intended logic. -,x . .=a+ba +b x =-The COMPUTE Instruction(continued)COMPUTE X = A + B . COMPUTE X = (A + B ) / 2 .(a + >)c2 a +bCOMPUTE X = ( A + B ) * C / 2 .COMPUTE X = (A + B) /(2 * C ) .2c x =VaCOMPUTE X = A * * . 5 . COMPUTE X = (A * * 2 +2) / C 120. ChapterS -----TheProcedureDivisionA Di D T h e A D D statement has two basic formats:[identifier - l j [literal-1^J]identifier-2] [literal-2JG I V I N G j i d e n t i f i e r - 3 [ROUNDED]! . . .[ON S I Z E ERROR imperative- statement - l] [ E N D - A D D]In the first format the value of identifier-2 is replaced b y the result of the addition; in the second format the value of identifier-2 is unchanged, because the result is stored in identifier-3 (and beyond). T h e w o r d T O is required in the first format, but optional in the second. T h e three dots in either format indicate that identifier-1 or literal-1 can b e repeated as m a n y times as necessary (so that multiple items c a n b e a d d e d together.) Examples 5.1 a n d 5.2 illustrate the A D D statement. T h e first instruction adds the values of A a n d B (5 a n d 10) to the value of C (20), a n d puts the s u m of 35 back into C. Example 5.2, however, does not include the initial value of C in the calculation; it adds the values of A a n d B (5 a n d 10), a n d places the s u m of 15 in C. Example 5.1ADD A B TO C BCBCB e f or & 6 x ecutio 11: AfterExample 5.2execution:ADD ATO BGIVINGC ABefore Afterexecution: execution:Table 5.7 contains additional examples of the A D D statement, with all examples operating o n the initial values of A, B, a n d C (5, 10, a n d 30, respectively). T h e last example changes the values of both B a n d C. 121. ArithmeticStatementsL_ft,VThe ADD InstructionValue before execution51030ADD A TO C.51035ADD A B TO C.51045ADD A TO B GIVING C51015ADD A 18 B GIVING C.51033ADD A 18 B T O C .51063ADD 1 TO B C.51131Value after execution ofT h e SUBTRACT statement has two formats: fidentifier-l] r ) . . . F R O M ndentifier-2 [literal-1 J 1SUBTRACT {r 1y i iIROUNDED> J[ON S I Z E E R R O R imperative - statement -1] [END-SUBTRACT]fidentifier-l] SUBTRACT fidenti fier-2 . . . FROMliteral-1j(literal-2G I V I N G {identifier-3 [ROUNDED]] . . . [ON S I Z E E R R O R imperative-statement-l] [END-SUBTRACT]In the first format the initial value of identifier-2 is replaced by the result of the subtraction. In the second format the initial value of either identifier-2 or literal-2 is unchanged, as the result is stored in identifier-3 (and beyond). Examples 5.3 a n d 5.4 illustrate the S U B T R A C T statement. In E x a m p l e 5.3 the S U B T R A C T statement causes the value of A (5) to b e subtracted f r o m the initial value of B (15) a n d the result (10) to b e stored in B. Only the value of B w a s changed. In the F R O M ... G I V I N G format of E x a m p l e 5.4 the value of A (5) is subtracted from the value of B (15), a n d the result (10) is placed in C. T h e values of A a n d B are unchanged, a n d the initial value of C (100) is replaced b y 10. Table 5.8 contains additional examples. Example 5.3SUBTRACTAFROMBA 122. C h a p t e rExample 5.4SUBTRACTAFROMBGIVING5A f t e rDivisionB 100[15]e x e c u t i o n :10e x e c u t i o n :T A B L E 5.8ProcedureCA B e f o r eTheThe SUBTRACT InstructionValue before execution10530100Value after execution of S U B T R A C T A F R O M C.51025100SUBTRACT A B FROM C.51015100SUBTRACT A B FROM C GIVING D.5103015S U B T R A C T 10 F R O M C D .5102090MULTIPLY T h e MULTIPLY statement has t w o formats: r[identi f i e r - 1 !,- BY {identifier-2MULTIPLY {[literal-1J -lv,ROUNDEDU1J. . .Jimperative-statement-l][ON S I Z E E R R O R [END-MULTIPLY][identifier- l|MULTIPLY {[identifier-2|} BY {J "[literal-1[literal-2GIVING |identifier-3 [ON S I Z E E R R O R}J[RQUNDEP][ . . .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.5MULTIPLYABY BA B e f o r ee x e c u t i o n :B 123. Example 5.6MULTIPLYA BY B GIVING CjlOj345j 10)A n|20j |20i2001Table 5.9 contains additional examples of the M U L T I P L Y statement. A s in the previous examples, the instructions operate o n the initial values of A, B, a n d C.TABLE 5..SThe MULTIPLY InstructionValue before execution51030MULTIPLY B BY A GIVING C.51050MULTIPLY A B Y B GIVING C.51050MULTIPLY A B Y B.55030MULTIPLY B B Y A.50103051515Value after execution ofMULTIPLY A B Y 3 GIVING B CT h e DIVIDE statement has two formats. In the second format, the primary distinction is b e t w e e n the w o r d s B Y a n d I N T O , w h i c h determine whether identifier-2 is the divisor or the dividend. A s with the other arithmetic statements, the G I V I N G option implies that the result is stored in identifier-3 so that the initial value of identifier-2 or literal-2 is unchanged. Only the second format m a k e s explicit provision for storing the remainder. [identifier-1] r , ii DIVIDE INTO identifier-2 ROUNDED . . . [literal-1 J I J 1J[ON SIZE ERROR imperati ve - statement - lj [END-DIVIDElfidentifier-ll [INTO] [identifier-2] DIVIDE i I"j [BY j [literal-2[literal-1[< ^ror GIVING identifier-3 [ROUNDED J11J.. .J[REMAINDER identifier-4] [ON SIZE ERROR imperati ve - statement -1] [END-DIVIDE] In E x a m p l e 5.7 the value of B (50) is divided b y the value of A (10), a n d the quotient (5) replaces the initial value of B. In E x a m p l e 5.8, w h i c h uses the G I V I N G option, the quotient goes into C, the remainder into D, a n d the values of A a n d B are 124. C h a p t e rP R O G R A M MI N C5TheProcedureDivisioT I PThe 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~-4ACIt 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. 125. 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.7DIVIDE A INTO B.10|[501I5I Example 5.8DIVIDE A INTO B GIVING C REMAINDER D.|10|} 13 j|10! Example 5.9|51|[51I5 IDIVIDE A BY B GIVING C REMAINDER D.,10,SlThe DIVIDE InstructionValue before execution1030530Value after execution of DIVIDE 2 INTO B. DIVIDE 2 INTO B GIVING C. D I V I D E S B Y 5 GIVING A. DIVIDE A INTO B C. DIVIDE A INTO B GIVING C. DIVIDE 3 INTO A GIVING B REMAINDER C.10510302610212Arithmetic 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.10ADD A TO B. 126. C h a p t e rSTheProcedureDivisionIn the example, field B is stored with a n implied decimal point. T h e compiler generates instructions to a d d a n integer n u m b e r (12) to a n u m b e r with o n e decimal place (34V5). It maintains decimal alignment, obtains 4 6 V 5 as a n answer, a n d stores the result in field B. N o w consider w h a t h a p p e n s if the operation is reversed, that is, A D D B T O A. T h e result of the addition is still 46V5; however, the field that stores the s u m , A, is d e n n e d without a decimal point; hence, the .5 will be truncated. It is critical, therefore,to define the receiving field with a sufficient number of decimal places. Table 5.11 contains additional examples. In each instance the instruction is a s s u m e d to operate o n the initial values of A, B, a n d C.? A&L.E 5 . 1 1Arithmetic on Fields with Assumed Decimal PointsPICTURE9999V9S3VSIValue before execution123454712ADD B TO A.463454712ADD A TO B.124654712 8162Value after execution ofADD B TO C.12345ADD C TO B.128164712ADD C TO A.593454712C.123455912ADD A TOT l t e YiJititl Biliii/ijj Pf t jjsffiBtfiT h e tuition billing p r o g r a m w a s first presented in Chapter 3, w h e r e w e produced hierarchy chart, pseudocode, a n d stubs program. W e continued the development of the p r o g r a m in Chapter 4, with specifics of the Identification, Environment, a n d D a t a divisions. N o w w e are able to write the Procedure Division a n d complete the program. W e emphasize, however, that the Procedure Division is not written from scratch, but is developed from w o r k already d o n e in Chapters 3 a n d 4. Consider, therefore, Figure 5.4, w h i c h contains the hierarchy chart a n d detailed pseudocode, a n d m o s t importantly the already working stubs program. T h e stubs p r o g r a m is complete in the sense that it contains all of the paragraphs n e e d e d for the eventual program; it is incomplete because m a n y of its paragraphs exist as o n e sentence D I S P L A Y statements that n e e d to b e e x p a n d e d to perform the indicated task. T h e m o s t difficult w o r k has already b e e n done, however, because the testing in Chapter 3 demonstrated that the overall p r o g r a m flow is correct. Thus, it is relatively simple to e x p a n d the various stub paragraphs in favor of m o r e detailed Procedure Division statements presented in this chapter. T h e paragraphs c a n be i m p l e m e n t e d o n e (or several) at a time; for example, begin with the paragraph to write a heading line, e x p a n d it, then lest it to b e sure it executes correctly. Develop the paragraph to write a detailed line, then e x p a n d the paragraphs to c o m p u t e the individual a m o u n t s (tuition, union fee, activity fee, a n d scholarship), testing each paragraph to b e sure it works properly. Finally, a d d the paragraphs to increment the university totals a n d write the s u m m a r y line at the e n d of the report. T h e c o m p l e t e d p r o g r a m is s h o w n in Figure 5.5. T h e Identification, Environment, a n d Data divisions were developed at the e n d of Chapter 4 a n d areme 127. TheTuitionBillingProgramDeveloping the Procedure DivisionPREPARE TUITION REPORTWRITE HEADING LINEREAD STUDENT FILECOMPUTE INDIVIDUAL BILLCOMPUTE TUITIONitfm y pr UNION FEEPROCESS STUDENT RECORDWRITE DETAIL LINEINCREMENT UNIVERSITY TOTALSCOMPUTE ACTiVITY FEEWRITE UNIVERSITY TOTALSW ~READ STUDENT FILECOMPUTE SCHOLARSHIP(a) Hierarchy Chart Open files Write heading 1ine(s) Read STUDENT-FILE at end indicate no more data DO WHILE data remains Compute tuition = 200 * credits IF union member Union fee = $25 ELSE Union fee = 0 ENDIF DO CASE CASE credits 6 and 12 Activity fee = 75 END CASE IF gpa > 2.5 Scholarship = Scholarship amount ELSE (no scholarship) Scholarship = 0 ENDIF Compute Bill = Tuition + Union fee + Activity fee - Scholarship Increment university totals Write detail line Read STUDENT-FILE at end indicate no more data ENDDO Write university totals Close files Stop run (b) Detailed Pseudocode| I[ j I | f j j [ 1 ) [ 128. Chapter (continued)PROCEDURE DIVISION. PREPARE-TUITION-REPORT. DISPLAY 'PREPARE-TUITION-REPORT paragraph entered'. OPEN INPUT STUDENT-FILE. PERFORM WRITE-HEADING-LINE. PERFORM READ-STUDENT-FILE. PERFORM PROCESS-STUDENT-RECORD UNTIL DATA-REMAINS-SWITCH = 'NO'. PERFORM WRITE-UNIVERSITY-TOTALS. CLOSE STUDENT-FILE. STOP RUN. WRITE-HEADING-LINE. DISPLAY 'WRITE-HEADING-LINE paragraph entered'. READ-STUDENT-FILE. READ STUDENT-FILE AT END MOVE NO' TO DATA-REMAINS-SWITCH END-READ. 1PROCESS-STUDENT-RECORD. DISPLAY ' . DISPLAY 'PROCESS-STUDENT-RECORD paragraph entered'. DISPLAY 'Student record being processed: ' STUDENT-RECORD. PERFORM COMPUTE-INDIVIDUAL-BILL. PERFORM INCREMENT-UNIVERSITY-TOTALS PERFORM WRITE-DETAIL-LINE. PERFORM READ-STUDENT-FILE. 1COMPUTE-INDIVIDUAL-BILL. DISPLAY ' COMPUTE-INDIVIDUAL-BILL paragraph entered'. PERFORM COMPUTE-TUITION. PERFORM COMPUTE-UNION-FEE. PERFORM COMPUTE-ACTIVITY-FEE. PERFORM COMPUTE-SCHOLARSHIP. COMPUTE-TUITION. DISPLAY ' COMPUTE-TUITION paragraph entered'. COMPUTE-UNION-FEE. DISPLAY ' COMPUTE-UNION-FEE paragraph entered'. COMPUTE-ACTIVITY-FEE. DISPLAY ' COMPUTE-ACTIVITY-FEE paragraph entered'. COMPUTE-SCHOLARSHIP. DISPLAY COMPUTE-SCHOLARSHIP paragraph entered . 11INCREMENT-UNIVERSITY-TOTALS. DISPLAY ' INCREMENT-UNIVERSITY-TOTALS paragraph entered'. WRITE-DETAIL-LINE. DISPLAY ' WRITE-DETAIL-LINE paragraph entered'. WRITE-UNIVERSITY-TOTALS. fiirrn A v u i o r l_M tii.DISPLAY 'WRITE-UNIVERSITY-TOTALS paragraph entered'. (c) Stubs Program5TheProcedureDivision 129. TheTuitionBillingProgramThe Tuition Billing Programl 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50IDENTIFICATION DIVISION. PROGRAM-ID. TUITI0N5. AUTHOR. CAROL VAZQUEZ VILLAR. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT-FILE ASSIGN TO 'A:CHAPTR05TUITI0N.DAT ORGANIZATION IS LINE SEQUENTIAL. SELECT PRINT-FILE ASSIGN TO PRINTER. DATA DIVISION. FILE SECTION. FD STUDENT-FILE RECORD CONTAINS 27 CHARACTERS. 01 STUDENT-RECORD. 05 STU-NAME. 10 STU-LAST-NAME PIC X(15). 10 STU-INITIALS PIC XX. 05 STU-CREDITS PIC 9(2). 05 STU-UNION-MEMBER PIC X. 05 STU-SCHOLARSHIP PIC 9(4). 05 STU-GPA PIC 9V99. FD 01PRINT-FILE RECORD CONTAINS 132 CHARACTERS. PRINT-LINE PIC X(132).WORKING-STORAGE SECTION. 01 DATA-REMAINS-SWITCH 010101PIC X(2)VALUE SPACES.INDIVIDUAL-CALCULATIONS. 05 IND-TUITION 05 IND-ACTIVITY-FEE 05 IND-UNION-FEE 05 IND-SCHOLARSHIP 05 IND-BILLPIC PIC PIC PIC PIC9(4) 9(2) 9(2) 9(4) 9(6)VALUE VALUE VALUE VALUE VALUEZEROS ZEROS ZEROS ZEROS ZEROSUNIVERSITY-TOTALS. 05 UNI-TUITION 05 UNI-UNION-FEE 05 UNI-ACTIVITY-FEE 05 UNI-SCHOLARSHIP 05 UNI-IND-BILLPIC PIC PIC PIC PIC9(6) 9(4) 9(4) 9(6) 9(6)VALUE VALUE VALUE VALUE VALUEZEROS ZEROS ZEROS ZEROS ZEROSCONSTANTS-AND-RATES. 05 PRICE-PER-CREDIT 05 UNION-FEE 05 ACTIVITY-FEES.PIC 9(3) PIC 9(2)VALUE 200. VALUE 25.1 130. ChapterS TheProcedureDivision(continued)51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10005 01010110 1ST-ACTIVITY -FEE 10 1ST-CREDIT-LIMIT 10 2ND-ACTIVITY -FEE 10 2ND-CREDIT-LIMIT 10 3RD-ACTIVITY -FEE MINIMUM-SCHOLAR- GPAPIC PIC PIC PIC PIC PICVALUE 25. VALUE 6. VALUE 50. VALUE 12. VALUE 75. VALUE 2.5.99 99 99 99 99 9V9HEADING-LINE. 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLERPIC X PIC X(12) PIC X(10) PIC X(7) PIC X(2) PIC X(7) PIC X(2) PIC X(9) PIC X(2) PIC X(7) PIC X(2) PIC X(ll) PIC X(2) PIC X(10) PIC X(48)VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUEDETAIL-LINE. 05 FILLER 05 DET-LAST-NAME 05 FILLER 05 DET-INITIALS 05 FILLER 05 DET-CREDITS 05 FILLER 05 DET-TUITION 05 FILLER 05 DET-UNION-FEE 05 FILLER 05 DET-ACTIVITY-FEE 05 FILLER 05 DET-SCHOLARSHIP 05 FILLER 05 DET-IND-BILL 05 FILLERPIC X PIC X(15) PIC X(2) PIC X(2). PIC X(5) PIC 9(2). PIC X(6) PIC 9(6). PIC X(7) PIC 9(3). PIC X(6) PIC 9(3). PIC X(8) PIC 9(5). PIC X(6) PIC 9(6). PIC X(49)VALUE SPACES.DASH-LINE. 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLER 05 FILLERSPACES. 'STUDENT NAME SPACES. 'CREDITS'. SPACES. 'TUITION'. SPACES. UNION F E E . SPACES. 'ACT FEE'. SPACES. 'SCHOLARSHIP' SPACES. 'TOTAL BILL'. SPACES.11VALUE SPACES. VALUE SPACES. VALUE SPACES. VALUE SPACES. VALUE SPACES. VALUE SPACES. VALUE SPACES. VALUE SPACES.PIC X(31) PIC X(8) PIC X(2) PIC X(8) PIC X(2) PIC X(7)VALUE SPACES. VALUE ALL VALUE SPACES. VALUE ALL VALUE SPACES. VALUE ALL '-'.I 131. TheTuition^ f101 12 0 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127BillingProgram(continued)05 05 05 05 05FILLER FILLER FILLER FILLER FILLERPIC X(6) PIC X(7) PIC X(5) PIC X(7) PIC X(49)VALUE VALUE VALUE VALUE VALUE01 TOTAL-LINE. 05 FILLER PIC X(8) VALUE 05 FILLER PIC X(17) VALUE 'UNIVERSITY TOTALS'. 05 FILLER PIC X(8) VALUE 05 TOT-TUITION PIC 9(6). 05 FILLER PIC X(6) VALUE 05 TOT-UNION-FEE PIC 9(4). 05 FILLER PIC X(5) VALUE 05 TOT-ACTIVITY-FEE PIC 9(4). 05 FILLER PIC X(7) VALUE 05 TOT-SCHOLARSHIP PIC 9(6). 05 FILLER PIC X(6) VALUE 05 TOT-IND-BILL PIC 9(6). 05 FILLER PIC X(49) VALUE PROCEDURE DIVISION. PREPARE-TUITION-REPORT. OPEN INPUT STUDENT-FILE OUTPUT PRINT-FILE. PERFORM WRITE-HEADING-LINE.SPACES ALL ' SPACES ALL ' SPACESSPACESSPACES SPACES SPACES SPACES SPACES SPACES.. .[1283'^^ 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151PERFORM PROCESS-STUDENT-RECORD UNTIL DATA-REMAINS-SWITCH = 'NO'. PERFORM WRITE-UNIVERSITY-TOTALS. CLOSE STUDENT-FILE PRINT-FILE. STOP RUN. WRITE-HEADING-LINE. MOVE HEADING-LINE TO PRINT-LINE. WRITE PRINT-LINE AFTER ADVANCING PAGE. MOVE SPACES TO PRINT-LINE. WRITE PRINT-LINE. READ-STUDENT-FILE. READ STUDENT-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH END-READ. PROCESS-STUDENT-RECORD. PERFORM COMPUTE-INDIVIDUAL-BILL. PERFORM INCREMENT-UNIVERSITY-TOTALS PERFORM WRITE-DETAIL-LINE. 132. C h a p t e r(continued)PERFORM READ-STUDENT-FILE. > COMPUTE-INDIVIDUAL-BILL. PERFORM COMPUTE-TUITION. PERFORM COMPUTE-UNION-FEE. PERFORM COMPUTE-ACTIVITY-FEE. PERFORM COMPUTE-SCHOLARSHIP. COMPUTE IND-BILL = IND-TUITION + IND-UNION-FEE + IND-ACTIVITY-FEE - IND-SCHOLARSHIP. COMPUTE-TUITION. COMPUTE IND-TUITION = PRICE-PER-CREDIT * STU-CREDITS. COMPUTE-UNION-FEE. IF STU-UNION-MEMBER = 'V MOVE UNION-FEE TO IND-UNION-FEE ELSE MOVE ZERO TO IND-UNION-FEE END-IF. COMPU T E - AC TIVITY- FEE. EVALUATE TRUE WHEN STU-CREDITS 1ST-CREDIT-LIMIT AND STU-CREDITS 2ND-CREDIT-LIMIT MOVE 3RD-ACTIVITY-FEE TO IND-ACTIVITY-FEE WHEN OTHER DISPLAY 'INVALID CREDITS FOR: ' STU-NAME END-EVALUATE. COMPUTE-SCHOLARSHIP. 3 JFSTU^A^^MINlSjM^ j""'" MOVE STU-SCHOLARSHIP TO IND-SCHOLARSHIP ELSE MOVE ZERO TO IND-SCHOLARSHIP END-IF. INCREMENT-UNIVERSITY-TOTALS. ADD IND-TUITION TO UNI-TUITION, ADD IND-UNION-FEE TO UNI-UNION-FEE. ADD IND-ACTIVITY-FEE TO UNI-ACTIVITY-FEE. ADD IND-SCHOLARSHIP TO UNI-SCHOLARSHIP. ADD IND-BILL TO UNI-IND-BILL.MOVE STU-LAST-NAME TO DET-LAST-NAME. MOVE STU-INITIALS TO DET-INITIALS. MOVE STU-CREDITS TO DET-CREDITS. 133. TherTuitionBillingProgram(continued)igijf 5.5I203MOVE IND-TUITION TO DET-TUITION.J204MOVE IND-UNION-FEE TO DET-UNION-FEE.1205MOVE IND-ACTIVITY-FEE TO DET-ACTIVITY-FEE.|206MOVE IND-SCHOLARSHIP TO DET-SCHOLARSHIP.I27 0MOVE IND-BILL TO DET-IND-BILL.[208MOVE DETAIL-LINE TO PRINT-LINE.|209WRITE PRINT-LINEj210|211|22 1|213jMOVE DASH-LINE TO PRINT-LINE.'2151WRITE-UNIVERSITY-TOTALS.214|AFTER ADVANCING 1 LINE.26 1_ _ "WRIJF:_PRJ[NJ-LIN_E. MOVE'UNI-TUITIOITTOTOT-TUTTIONT""!M O V E U N I - U N I O N - F E E T O TOT-UNION-FEE.I27 1MOVE UNI-ACTIVITY-FEE TO TOT-ACTIVITY-FEE. f(218MOVE U N I - S C H O L A R S H I P TO TOT-SCHOLARSHIP.!|29 1M O V E U N I - I N D - B I L L TO T O T - I N D - B I L L .j l?p_ _ j IL2""l21 222MOVE TOTAL-LINE TO PRINT-LINE.~~"""WRITE_ __ jPRINT-LINEAFTER ADVANCING 1 LINE.copied directly from Figure 4.10. T h e completed program appears s o m e w h a t formidable the first time y o u see it, but it has b e e n developed over the last three chapters, a n d y o u should have n o difficulty in following. W e suggest y o u take it in pieces a n d review sections of the text as y o u n e e d t h e m with respect to the following: 1. T h e Identification Division in lines 1-3 contains only the P R O G R A M - I D a n d A U T H O R paragraphs. 2. T h e Environment Division in lines 5-11 contains the S E L E C T statements for the two required files. 3. T h e ID's in lines 15-16 a n d 26-28 correspond to the S E L E C T statements in the Environment Division. 4. T h e description for the incoming data in lines 17-24 matches the p r o g r a m specifications of Chapter 3. 5. Separate 01 entries are defined for individual a n d total calculations (lines 3 3 38 a n d 40-45); also data n a m e s for the constants a n d rates are established in lines 47-56. 6. Heading, detail, dashed, a n d total lines are described separately in W O R K I N G - S T O R A G E (lines 58-73, 75-92, 94-105, and 107-121, respectively); note the use of V A L U E clauses to initialize the various print lines. 7. T h e paragraphs in the Procedure Division correspond o n e to o n e with the blocks in the hierarchy chart of Figure 5.4a. 8. A n initial R E A D statement in line 128 is followed by the P E R F O R M statement in lines 129 a n d 130 to execute P R O C E S S - S T U D E N T - R E C O R D (lines 148152) until there are n o m o r e records. T h e last statement of the performed paragraph is a second R E A D statement. T h e combination of these statements implements the overall logic in the pseudocode of Figure 5.4b. 134. C h a p t e rSTheProcedureDivision9. A n EVALUATE s t a t e m e n t i n l i n e s 1 7 3 - 1 8 3 c o m p u t e s t h e activity f e e according to the n u m b e r of credits taken. 10.11.M u l t i p l e MOVE s t a t e m e n t s are r e q u i r e d w i t h i n t h e p a r a g r a p h to w r i t e a d e t a i l e d l i n e (lines 1 9 9 - 2 1 0 ) , w i t h e a c h s t a t e m e n t m o v i n g a c o m p u t e d v a l u e ( s u c h as I N D - T U I T I O N ) t o t h e c o r r e s p o n d i n g e n t r y i n t h e print l i n e (DETTUITION). T h e n e e d for b o t h d a t a n a m e s will b e m o r e a p p a r e n t after t h e m a t e r i a l o n e d i t i n g in C h a p t e r 7. T h e p a r a g r a p h t o write u n i v e r s i t y t o t a l s requires similar treatment.12.Figure 5.6S e p a r a t e p a r a g r a p h s i n t h e P r o c e d u r e D i v i s i o n c o m p u t e a n i n d i v i d u a l bill (lines 1 5 4 - 1 6 0 ) , i n c r e m e n t u n i v e r s i t y t o t a l s ( l i n e s 1 9 2 - 1 9 7 ) , a n d w r i t e a d e t a i l l i n e (lines 1 9 9 - 2 1 0 ) .M u l t i p l e A D D s t a t e m e n t s are n e e d e d w i t h i n t h e p a r a g r a p h to i n c r e m e n t u n i v e r s i t y t o t a l s (lines 1 9 3 - 1 9 7 ) . E a c h total is s t o r e d i n a s e p a r a t e field a n d thus m u s t b e i n c r e m e n t e d separately.Test Data and Output SMITH JAMES BAKER PART-TIMER JONES HEAVYWORKER LEE CLARK GROSSMAN FRANKEL BENWAY KERBELJB15Y0000230 HR15 0500245 SR09 0500350 JR03Y0000300 PL15Y0000280 HM18 0000200 BL18 0000335 JC06 0000310 SE07 0000215 LF10 0000350 CT03 0250395 NB04 0000100(a) Test Data STUDENT NAME SMITH JAMES BAKER PART-TIMER JONES HEAVYWORKER LEE CLARK GROSSMAN FRANKEL BENWAY KERBELCREDITS JB HR SR JR PL HM BL JC SE LF CT NBUNIVERSITY TOTALS15 15 09 03 15 18 18 06 07 10 03 04TUITIONUNION FEEACT FEESCHOLARSHIPTOTAL BILL003000 003000 001800 000600 003000 003600 003600 001200 001400 002000 000600 000800025 000 000 025 025 000 000 000 000 000 000 000075 075 050 025 075 075 075 025 050 050 025 02500000 00000 00500 00000 00000 00000 00000 00000 00000 00000 00250 00000003100 003075 001350 000650 003100 003675 003675 001225 001450 002050 000375 00082502460000750625000750024550(b) Output 135. TheTuitionBillingProgramYii-st Data T h e test d a t a a n d a s s o c i a t e d o u t p u t are s h o w n i n Figures 5.6a a n d 5.6b, r e s p e c t i v e l y . T h e test d a t a are i d e n t i c a l t o t h o s e u s e d i n t h e original s t u b s p r o g r a m ; t h e o u t p u t , h o w e v e r , is different a n d reflects t h e e x p a n d e d P r o c e d u r e D i v i s i o n o f Figure 5.5. N o t e , t o o , t h e c o r r e s p o n d e n c e b e t w e e n i n d i v i d u a l r e c o r d s i n t h e i n p u t d a t a file a n d t h e a s s o c i a t e d l i n e s i n t h e p r i n t e d report. O b s e r v e , for e x a m p l e , that JB S m i t h , JR Part-Timer, a n d PL J o n e s e a c h h a v e a Y i n c o l u m n 2 0 o f their i n p u t r e c o r d s , a n d that t h e s e are t h e o n l y i n d i v i d u a l s w h o are c h a r g e d a U n i o n F e e . In similar f a s h i o n , J a m e s , Baker, a n d B e n w a y a r e t h e o n l y s t u d e n t s w i t h p o t e n t i a l s c h o l a r s h i p s i n t h e i n c o m i n g data; J a m e s , h o w e v e r , d o e s not have the requisite average and s o h e d o e s not receive a scholarship. The s t u d e n t file h a s 12 r e c o r d s , a n d h e n c e 12 s t u d e n t s a p p e a r in t h e p r i n t e d report. In r e t r o s p e c t , t h e o u t p u t p r o d u c e d isn't very pretty a s it is u n f o r m a t t e d a n d c o n t a i n s e x t r a n e o u s z e r o s t h r o u g h o u t . (Editing is p r e s e n t e d i n C h a p t e r 7 t o g e t h e r w i t h a final v e r s i o n o f t h e p r o g r a m . )Hierarchy Chart,The hierarchy chart was introduced initially as a design, aid and developed before t h e p r o g r a m w a s w r i t t e n ; it is a l s o u s e d a s a d o c u m e n t a t i o n t e c h n i q u e after c o d i n gis completed to better u n d e r s t a n d t h e overall program structure. T h e hierarchy c h a r t d e p i c t s t h e f u n c t i o n s i n h e r e n t i n a p r o g r a m , a n d is c l o s e l y t i e d t o t h e p a r a g r a p h s i n t h e P r o c e d u r e D i v i s i o n . O b s e r v e therefore, t h e p r o p e r t i e s o f t h e h i e r a r c h y chart i n Figure 5.4a a s t h e y relate t o t h e COBOL p r o g r a m i n Figure 5.5. 1. Every b o x ( m o d u l e ) i n t h e h i e r a r c h y chart c o r r e s p o n d s t o a p a r a g r a p h i n t h e COBOL p r o g r a m . T h e r e are t w e l v e different m o d u l e s (the READ a p p e a r s twice) i n t h e h i e r a r c h y chart, a n d t w e l v e p a r a g r a p h s i n t h e p r o g r a m . 2. E a c h p a r a g r a p h i n t h e COBOL p r o g r a m c o n t a i n s a s m a n y PERFORM s t a t e m e n t s a s t h e r e are m o d u l e s in t h e n e x t l o w e r level o f t h e h i e r a r c h y chart. T h u s t h e p a r a g r a p h a t t h e h i g h e s t level, PREPARE-TUITION-REPORT, c o n t a i n s four PERFORM s t a t e m e n t s , o n e for e a c h s u b o r d i n a t e p a r a g r a p h . 3. A p a r a g r a p h c a n b e e n t e r e d o n l y f r o m t h e p a r a g r a p h directly a b o v e it a n d m u s t e v e n t u a l l y return c o n t r o l t o that p a r a g r a p h . H e n c e , PROCESSS T U D E N T - R E C O R D S is e n t e r e d via a PERFORM s t a t e m e n t i n PREPARETUITION-REPORT. P R O C E S S - S T U D E N T - R E C O R D S i n t u r n i n v o k e s four l o w e r l e v e l p a r a g r a p h s , e a c h o f w h i c h r e t u r n s c o n t r o l t o PROCESSS T U D E N T - R E C O R D S , w h i c h e v e n t u a l l y returns c o n t r o l t o PREPARETUITION-REPORT. 4. Every m o d u l e in a h i e r a r c h y chart ( p a r a g r a p h w i t h i n a p r o g r a m ) s h o u l d b e d e d i c a t e d t o a s i n g l e f u n c t i o n . T h e n a t u r e o f that f u n c t i o n s h o u l d b e a p p a r e n t f r o m t h e m o d u l e ' s n a m e a n d s h o u l d c o n s i s t o f a v e r b , o n e or t w oadjectives, a n d a n object. R e m e m b e r , t o o , that a hierarchy c h a r t is very different f r o m flowcharts o r p s e u d o c o d e . A h i e r a r c h y chart s h o w s w h a t h a s t o b e d o n e , b u t n o t w h e n ; it c o n t a i n s n o decision-making logic. Flowcharts a n d p s e u d o c o d e , o n the other h a n d , specify w h e n a n d if a g i v e n b l o c k o f c o d e is e x e c u t e d . W e s a y that h i e r a r c h y c h a r t s a r e functional i n n a t u r e ; t h e y c o n t a i n t h e t a s k s n e c e s s a r y t o a c c o m p l i s h t h e s p e c i f i c a t i o n s b u t d o n o t i n d i c a t e a n o r d e r for e x e c u t i o n . P s e u d o c o d e a n d flowcharts are procedural a n d s p e c i f y l o g i c . 136. ChapterS TheProcedureDivisionO u r objective is for you to write meaningful C O B O L programs, not to m e m o r i z e u hat m u s t appear to b e a n endless list of rules. Y o u m u s t eventually r e m e m b e r certain things, but w e have found the best approach is to pattern your first few C O B O L programs after existing examples such as the tuition billing program. Everything y o u n e e d to get started is contained in that p r o g r a m (Figure 5.5) if y o u will look at it carefully. A s a further aid, Fi


Recommended