+ All Categories
Home > Documents > Cobol From Micro to Mainframe

Cobol From Micro to Mainframe

Date post: 20-Dec-2014
Category:
Upload: david-fernandez-gutierrez
View: 379 times
Download: 60 times
Share this document with a friend
Popular Tags:
908
COBOL From Micro to Mainframe RT:elEntT T. [3Ft.AUEnl AR{f L VnzcpuEz VtLL,A,Fl ATTTHUR R. EIUgs THIRD EDITIOil
Transcript

From Micro to Mainframe

COBOL

RT:elEntT T. [3Ft.AUEnl AR{f L VnzcpuEz VtLL,A,Fl ATTTHUR R. EIUgsTHIRD EDITIOil

ISBN O-I3_?1DBI7-A

lffifiilflillilililllililtlflll

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.e

WEB 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 to

Mainframe 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-2

W 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

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]

rreiace

M i

IntroductionOverview 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 12 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 21

From Coding Form to ComputerOverview 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 33 Errors 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 42

40 Problems 43

A Methodology for P r o g r a m D e v e l o p m e n tOverview 48 The Tuition Billing Problem 48 Structured Design 50 Evaluating the Hierarchy Chart Completeness 53 Functionality 54 Span of Control 54 Structured Programming 54 Sufficiency 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

52

Contents

C 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 3Overview 74 COBOL Notation 74 Identification Division 75 Environment Division 76 CONFIGURATION SECTION 76 INPUT-OUTPUT SECTION 76 Data Division 77 FILE SECTION 77 WORKING-STORAGE SECTION 82 The 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 93

5: T h e P r o c e d u r e D i v i s i o nOverview 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 109 The 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 133

97

C h a p t e r 6: D e b u g g i n gOverview 140 Errors in Compilation 140 Common Compilation Errors 149 Errors in Execution 151 File Status Codes 156 Another Run Time Error 158 Logic Errors 159 Tips for Debugging 160 DISPLAY Statement 161 The Structured Walkthrough Summary 163 Fill-in 164 True/False 165

139

162 Problems 165

Contents

Chapter 7: Editing and Coding StandardsOverview 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 177

169

BLANK 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 191

C h a p t e r Us Data ValidationOverview 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 205 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 Problems

195

225

Cna^ici-

S * More About the

edure Division

229

Overview 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 237

STRING 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

Contents

w. .

-i C: S c r e e n l-O

265Car 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 299

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

...wveivl6w

i" Introduction to Taioud

sOCCURS DEPENDING ON 310

301

introduction 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

The 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 327

.

,

u

Table Lookups

331Programming 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

Overview 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 340

Contents

c:;

z\n i. \

Multilevel TablesPERFORM VARYING 382

363A 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 400

Overview 3 6 4 System C o n c e p t s 364 COBOL Implementation 366 One-Level Tables 3 6 6 PERFORM VARYING 366 Two-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-,.

: Sorting

403USING/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 431

Overview 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

KV\ 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 452

.

i

Breaks

40 CThe 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

Contents

= >

l^L L

Evaluating

the

Hierarchy

Chart

F i g u r e 3.3

(continued)

PREPARE TUITION REPORT

WRITE HEADING LINE

READ STUDENT FILE

PROCESS STUDENT RECORD

WRITE UNIVERSITY TOTALS

COMPUTE INDIVIDUAL BILL

INCREMENT UNIVERSITY TOTALS

READ STUDENT FILE

4th Level Processing Requirements COMPUTE TUITION COMPUTE UNION FEE COMPUTE ACTIVITY FEE COMPUTE SCHOLARSHIP 3a(1) 3a(2i 3a 3 3a(4) Compute tuition Compute union fee Compute activity tee Compute scholarship

(c) Complete Hierarchy Chart (Levels 1-4}

1. Is t h e h i e r a r c h y chart c o m p l e t e ? 2. Are t h e m o d u l e s f u n c t i o n a l ? 3. Is t h e r e effective s p a n o f control?

CompletenessA h i e r a r c h y chart m u s t b e c o m p l e t e ; that is, it h a s t o p r o v i d e for e v e r y f u n c t i o n r e q u i r e d b y t h e p r o g r a m a s s p e c i f i e d i n t h e p r o g r a m m i n g s p e c i f i c a t i o n s . Y o u test for c o m p l e t e n e s s level b y level, starting a t t h e t o p o f t h e h i e r a r c h y chart, a n d w o r k i n g y o u r w a y d o w n , o n e level at a t i m e , b y a s k i n g t h e q u e s t i o n , " D o t h e s u b o r d i n a t e m o d u l e s at t h e n e x t level c o m p l e t e l y d e v e l o p their c o r r e s p o n d i n g m o d u l e s at this level?" If t h e a n s w e r is y e s , m o v e t o t h e n e x t m o d u l e o n t h e p r e s e n t level, or t o t h e first m o d u l e o n t h e next level, a n d r e p e a t t h e q u e s t i o n . If t h e a n s w e r is n o , a d d f u n c t i o n s a s n e c e s s a r y a n d c o n t i n u e t o t h e n e x t m o d u l e . For e x a m p l e , b e g i n w i t h t h e c o m p l e t e d h i e r a r c h y chart o f F i g u r e 3.3c a n d ask y o u r s e l f w h e t h e r t h e m o d u l e s o n level t w o are a d e q u a t e to e x p a n d t h e s i n g l e m o d u l e of level o n e ; t h a t is, d o t h e four m o d u l e s o n l e v e l t w o c o m p l e t e l y e x p a n d t h e PREPARE-TU1TION-REPORT m o d u l e t o w h i c h t h e y are s u b o r d i n a t e ? T h e a n s w e r is yes, s o y o u m o v e to l e v e l t h r e e a n d s e e w h e t h e r t h e m o d u l e s o n this l e v e l a d e q u a t e l y e x p a n d t h e P R O C E S S - S T U D E N T - R E C O R D m o d u l e f r o m level t w o . O n c e a g a i n t h e a n s w e r is y e s , a n d s o y o u p r o g r e s s t o level four. T h e p r o c e s s c o n t i n u e s until y o u h a v e c h e c k e d e v e r y m o d u l e o n e v e r y l e v e l a n d a r e satisfied t h a t all n e c e s s a r y f u n c t i o n s are i n c l u d e d .

C h a o t o r

3

'

t h o d o l o g y

tor

Program

Development

Functionality

,

Every m o d u l e in a h i e r a r c h y chart s h o u l d b e d e d i c a t e d t o a single f u n c t i o n , t h e n a t u r e o f w h i c h s h o u l d b e clear f r o m e x a m i n i n g t h e m o d u l e ' s n a m e . E a c h o f t h e m o d u l e n a m e s in Figure 3.3 c o n s i s t s of a v e r b , a d j e c t i v e (or t w o ) , a n d a n o b j e c t f o r e x a m p l e , C O M P U T E - I N D I V I D U A L - B I L L or WRITE-DETAIL-LINE. I n d e e d , if a m o d u l e c a n n o t b e n a m e d i n this w a y , its f u n c t i o n is p r o b a b l y n o t w e l l d e f i n e d a n d t h o u g h t s h o u l d b e g i v e n t o revising t h e h i e r a r c h y chart. S t a t e d a n o t h e r w a y , y o u s h o u l d reject (or r e d e s i g n ) a n y m o d u l e that d o e s n o t a p p e a r to b e f u n c t i o n a l ; that is, m o d u l e s w h o s e n a m e s c o n t a i n : 1. M o r e t h a n o n e v e r b f o r e x a m p l e , READ-AND-WRITE. 2. M o r e t h a n o n e o b j e c t f o r e x a m p l e , EI) IT-NAM E - A N D - A C C O U NT-DATA. 3. N o n d e s c r i p t i v e or t i m e - r e l a t e d t e r m s f o r e x a m p l e , HOUSEKEEPING, T E R M I N A T I O N - R O U T I N E , INITIALIZATION, or MAINLINE. A n o t h e r w a y o f e x p r e s s i n g t h e n e e d for f u n c t i o n a l m o d u l e s is to strive for m o d u l e i n d e p e n d e n c e ; t h a t is, t h e internal w o r k i n g s of o n e m o d u l e s h o u l d n o t affect t h o s e o f a n o t h e r . P e r h a p s y o u h a v e a l r e a d y b e e n a s s o c i a t e d w i t h a w o r k i n g p r o g r a m in w h i c h c h a n g e s w e r e i m p l e m e n t e d , o n l y t o h a v e s o m e other, a p p a r e n t l y unrelated, portion of the program n o longer work properly. T h e p r o b l e m m a y b e

d u e to paragraphs in the p r o g r a m being u n n e c e s s a r i l y dependent o n o n e another.W h a t w e are s a y i n g is t h a t in a n ideal s i t u a t i o n , c h a n g e s m a d e to o n e paragraph s h o u l d n o t affect t h e r e s u l t s o f a n y o t h e r . In a m o r e practical s e n s e , t h e p a r a g r a p h s h a v e t o b e s o m e w h a t related, o t h e r w i s e t h e y w o u l d n o t b e parts o f t h e s a m e p r o g r a m ; however, the a m o u n t of i n t e r d e p e n d e n c e b e t w e e n paragraphs s h o u l d be m i n i m i z e d to t h e g r e a t e s t e x t e n t p o s s i b l e . W i t h r e s p e c t t o F i g u r e 3 . 3 , for e x a m p l e , a c h a n g e i n t h e p r o c e d u r e for c o m p u t i n g t h e u n i o n f e e s h o u l d n o t affect h o w t h e activity fee is d e t e r m i n e d . T h a t is b e c a u s e t h e m o d u l e s C O M P U T E - U N I O N - F E E a n d C O M P U T E ACTIVITY-FEE are f u n c t i o n a l i n their o w n right, a n d c o n s e q u e n t l y are i n d e p e n d e n t of o n e another.

Span f Control

_

_

T h e span of control o f a m o d u l e is t h e n u m b e r o f s u b o r d i n a t e s it c o n t a i n s . In F i g u r e 3.3, for e x a m p l e , t h e s p a n o f c o n t r o l o f b o t h PREPARE-TUITION-REPORT a n d COMPUTE-INDIVIDUAL-BILL is four. A n effective s p a n of c o n t r o l (for h i e r a r c h y c h a r t s a s s o c i a t e d w i t h C O B O L p r o g r a m s ) is g e n e r a l l y f r o m t w o or t h r e e t o s e v e n , a l t h o u g h that m a y vary d e p e n d i n g o n t h e s i t u a t i o n . Y o u s h o u l d , h o w e v e r , a v o i d extremes in either direction. Programs w i t h ineffective s p a n s of control (too m a n y s u b o r d i n a t e s o r t o o few) are p o o r l y d e s i g n e d a n d difficult t o f o l l o w a n d / o r m a i n t a i n .

Structured PlQgi>gr|irf)Jng

Let u s p a u s e for a m o m e n t t o s e e w h a t h a s b e e n a c c o m p l i s h e d . W e h a v e t a k e n t h e original p r o b l e m a n d d i v i d e d it i n t o a s e r i e s o f m a n a g e a b l e p i e c e s , e a c h o f w h i c h d e s c r i b e s a particular j o b that n e e d s to b e a c c o m p l i s h e d . In o t h e r w o r d s , w e h a v e said what n e e d s to be d o n e to solve the problem, but have not as yet said h o w w e will s o l v e it. T h a t in e s s e n c e is t h e d i f f e r e n c e b e t w e e n s t r u c t u r e d d e s i g n a n d structured programming. A s t r u c t u r e d p r o g r a m is o n e c o n s i s t i n g entirely o f t h r e e t y p e s o f logic structures: s e q u e n c e , s e l e c t i o n (a d e c i s i o n ) , a n d i t e r a t i o n (a l o o p ) . T h e fact t h a t t h e s e s t r u c t u r e s

Structured

Programming

(or b a s i c b u i l d i n g b l o c k s ) are sufficient t o e x p r e s s a n y d e s i r e d l o g i c w a s first postulated in a now-classic paper by B o h m and Jacopini.1

T h e e l e m e n t a r y b u i l d i n g b l o c k s o f s t r u c t u r e d p r o g r a m m i n g are s h o w n in f l o w c h a r t f o r m i n F i g u r e 3.4. F l o w c h a r t s u s e s p e c i a l s y m b o l s t o c o m m u n i c a t e information. A rectangle indicates a processing statement, a d i a m o n d indicates a d e c i s i o n , a n d a s m a l l circle c o n n e c t s p o r t i o n s of t h e f l o w c h a r t . All o f t h e f l o w c h a r t s have o n e key feature in c o m m o n , namely, a leave. T h e sequence s t r u c t u r e i n Figure 3.4a s p e c i f i e s t h a t t h e p r o g r a m s t a t e m e n t s are e x e c u t e d s e q u e n t i a l l y , in t h e o r d e r i n w h i c h t h e y a p p e a r . T h e t w o b l o c k s , A a n d B, m a y d e n o t e a n y t h i n g f r o m s i n g l e s t a t e m e n t s t o c o m p l e t e p r o g r a m s , a n d it is clear that t h e r e is a s i n g l e entry p o i n t a n d a s i n g l e exit p o i n t t o t h e s t r u c t u r e .

single entry point a n d a single exit

point, t h a t is, t h e r e is o n l y o n e w a y t o e n t e r e a c h s t r u c t u r e a n d o n l y o n e w a y to

1 Bohm and Jacopini, "Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules," Communications of the ACM (May 1966).

The Building Blocks of Structured Programming

(c) Iteration

(d)

C s a e

C h a p t e r

3

A Methodology

for

Program

Development

T h e selection

(or IF . . . T H E N . . . ELSE) s t r u c t u r e i n F i g u r e 3.4b s p e c i f i e s a

c h o i c e b e t w e e n t w o a c t i o n s . A c o n d i t i o n i s t e s t e d w i t h o n e o f t w o o u t c o m e s ; if t h e c o n d i t i o n i s true, b l o c k A i s e x e c u t e d , w h i l e if it i s false, b l o c k B is e x e c u t e d . T h e c o n d i t i o n itself i s t h e s i n g l e e n t r y p o i n t , a n d b o t h p a t h s m e e t t o f o r m a s i n g l e exit p o i n t . T h e iteration (or D O . . . WHILE) s t r u c t u r e i n Figure 3.4c s p e c i f i e s r e p e a t e d e x e c u t i o n o f o n e o r m o r e s t a t e m e n t s w h i l e a c o n d i t i o n is true. A c o n d i t i o n i s t e s t e d and, if it is true, b l o c k A i s e x e c u t e d after w h i c h t h e c o n d i t i o n i s r e t e s t e d . If, h o w e v e r , t h e c o n d i t i o n is false, c o n t r o l p a s s e s t o t h e n e x t s e q u e n t i a l s t a t e m e n t after t h e i t e r a t i o n s t r u c t u r e . A g a i n , t h e r e i s a s i n g l e e n t r y p o i n t a n d a s i n g l e exit p o i n t f r o m the structure. T h e case structure i n Figure 3.4d e x p r e s s e s a m u l t i b r a n c h s i t u a t i o n . A l t h o u g h c a s e is actually a s p e c i a l i n s t a n c e o f s e l e c t i o n , it is c o n v e n i e n t t o e x t e n d t h e d e f i n i t i o n o f s t r u c t u r e d p r o g r a m m i n g t o i n c l u d e t h i s fourth t y p e o f b u i l d i n g block. T h e c a s e structure evaluates a c o n d i t i o n a n d b r a n c h e s to o n e o f several paths, d e p e n d i n g o n t h e v a l u e o f t h e c o n d i t i o n . A s w i t h t h e o t h e r b u i l d i n g b l o c k s , t h e r e is o n e e n t r y p o i n t a n d o n e exit p o i n t .

ffficiency of t h e B a s i c S t r u c t u r e sT h e t h e o r y o f s t r u c t u r e d p r o g r a m m i n g s a y s s i m p l y that a n a p p r o p r i a t e c o m b i n a t i o n

of the basic building blocks may be derived to solve any problem. This is possibleb e c a u s e a n entire structure (sequence, selection, iteration, or case) m a y b e substituted a n y w h e r e b l o c k A o r B a p p e a r s . Figure 3.5 s h o w s a c o m b i n a t i o n o f the b a s i c s t r u c t u r e s t o illustrate t h i s c o n c e p t . F i g u r e 3.5 i s e s s e n t i a l l y a s e l e c t i o n s t r u c t u r e . H o w e v e r , i n s t e a d o f s p e c i f y i n g a s i n g l e s t a t e m e n t for t h e t r u e o r false b r a n c h e s , a s w a s d o n e i n F i g u r e 3.4, a c o m p l e t e b u i l d i n g b l o c k is u s e d i n s t e a d . T h u s , if c o n d i t i o n - 1 i s true, a n i t e r a t i o n s t r u c t u r e is e n t e r e d , w h e r e a s , if it is false, a s e q u e n c e s t r u c t u r e i s e x e c u t e d . B o t h t h e i t e r a t i o n a n d s e q u e n c e s t r u c t u r e s m e e t at a s i n g l e exit p o i n t w h i c h b e c o m e s t h e exit p o i n t for t h e initial s e l e c t i o n s t r u c t u r e . F i g u r e 3.5 S u f f i c i e n c y of t h e B a s i c S t r u c t u r e s

FALSE

TRUE

J

A

1

B

TRUE FALSE

Sequence Structure

Iteration Structure

W e n o w turn o u r attention t o w a y s in w h i c h p r o g r a m m e r s express logic, to t h e m s e l v e s a n d t o o t h e r s . W e b e g i n w i t h t h e traditional flowchart, t h e n m o v e t o newer techniques more closely associated with structured programming: p s e u d o c o d e a n d Warnier-Orr d i a g r a m s .

Every p r o g r a m m e r is familiar w i t h t h e traditional flowchart's d e s c r i b e d i n Chapter 1. Although flowcharts have declined i n popularity, they remain i n w i d e s p r e a d u s e , primarily for d o c u m e n t a t i o n . O u r p e r s o n a l p r e f e r e n c e is t o u s e p s e u d o c o d e , b u t w e i n c l u d e Figure 3.6 for c o m p l e t e n e s s . T h e d e c i s i o n a s t o w h i c h t e c h n i q u e t o u s e is b e t w e e n y o u a n d y o u r instructor.

T h e fact that m o s t p r o g r a m m e r s write s i m p l e n o t e s t o t h e m s e l v e s prior t o c o d i n g

a p r o g r a m gave rise to pseudocode, a technique associated w i t h structured programming. A s w e indicated in Chapter 1, pseudocode is defined simply as neatn o t e s t o yourself, a n d u s e s s t a t e m e n t s similar t o c o m p u t e r i n s t r u c t i o n s t o d e s c r i b e l o g i c . Figure 3.7 r e p r e s e n t s the building blocks of structured p r o g r a m m i n g as they w o u l d b e w r i t t e n i n p s e u d o c o d e a n d c o r r e s p o n d s t o t h e flowcharts s h o w n i n Figure 3.4. P s e u d o c o d e c o m e s i n t o p l a y after t h e d e s i g n p h a s e o f a p r o g r a m h a s b e e n n a t u r e a n d i n d i c a t e s what h a s t o b e d o n e , b u t n o t n e c e s s a r i l y when or how. P s e u d o c o d e , o n t h e o t h e r h a n d , is procedural a n d c o n t a i n s s e q u e n c e a n d d e c i s i o n making logic. In other words, p s e u d o c o d e c o n n e c t s t h e m o d u l e s i n a hierarchy chart t h r o u g h l o o p s a n d d e c i s i o n m a k i n g . To better appreciate h o w p s e u d o c o d e expresses programming logic, consider Figure 3.8, w h i c h c o n t a i n s p s e u d o c o d e for t h e t u i t i o n billing p r o g r a m . T w o v e r s i o n s o f t h e p s e u d o c o d e are p r e s e n t e d a n initial a t t e m p t i n Figure 3.8a, a n d a n e x p a n d e d ( m o r e d e t a i l e d ) v e r s i o n i n Figure 3.8b. B o t h v e r s i o n s are e q u a l l y a p p r o p r i a t e , w i t h the c h o i c e b e t w e e n t h e m d e p e n d i n g entirely o n t h e individual, a n d t h e level of d e t a i l h e or s h e d e s i r e s . T h e l o g i c is straightforward a n d b e g i n s w i t h t h e s t e p s for i n i t i a l i z a t i o n ; t o o p e n files, write a h e a d i n g l i n e ( s ) , a n d read t h e first r e c o r d . T h e n , a l o o p (or i t e r a t i o n structure) i s e n t e r e d i n w h i c h t h e p r o g r a m c o m p u t e s t h e s t u d e n t ' s bill (tuition p l u s u n i o n a n d activity f e e s m i n u s s c h o l a r s h i p ) , i n c r e m e n t s t h e u n i v e r s i t y totals t o i n c l u d e t h i s a m o u n t , w r i t e s a detail l i n e , a n d finally r e a d s t h e n e x t s t u d e n t r e c o r d . T h e s t a t e m e n t s i n t h e l o o p are e x e c u t e d c o n t i n u a l l y until all t h e r e c o r d s h a v e b e e n read, at w h i c h p o i n t u n i v e r s i t y t o t a l s a r e w r i t t e n , a n d t h e p r o g r a m t e r m i n a t e s . P s e u d o c o d e h a s a d i s t i n c t b l o c k s t r u c t u r e that is c o n d u c i v e t o s t r u c t u r e d p r o g r a m m i n g . It i s n o t , h o w e v e r , b o u n d b y f o r m a l s y n t a c t i c a l rules ( a l t h o u g h s o m e o r g a n i z a t i o n s h a v e i m p l e m e n t e d s t a n d a r d s ) , n o r d o e s it h a v e s p e c i f i c rules o f i n d e n t a t i o n , w h i c h is d o n e strictly at t h e p r o g r a m m e r ' s d i s c r e t i o n . Its o n l y l i m i t a t i o n is a restriction t o t h e b u i l d i n g b l o c k s of s t r u c t u r e d p r o g r a m m i n g ( s e q u e n c e , s e l e c t i o n , iteration, a n d c a s e ) . With practice, p s e u d o c o d e c a n b e d e v e l o p e d quickly a n d easily. G o o d p s e u d o c o d e s h o u l d b e s u f f i c i e n t l y p r e c i s e t o b e a real a i d i n w r i t i n g a p r o g r a m , while informal e n o u g h to b e u n d e r s t o o d b y nonprogrammers. T h e informality of

C h a p t e r

3

A

Methodology

for

Program

Development

Figure 3.6

Flowchart for Tuition Billing Program

f V START

PROCESS STUDENT

" A J

\1

fCOMPUTE UNION FEE

COMPUTE ACTIVITY FEE

TRUE

PROCESS STUDENT

COMPUTE SCHOLARSHIP

WRITE UNIVERSITY TOTALS

/

INCREMENT UNIVERSITY TOTALS

1

CLOSE FILES

WRITE DETAIL LINE

2.5 Scholarship = Scholarship amount ELSE (no scholarship) Scholarship = 0 I 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

l

| ! j | j j j j I

l u r e 3.9

Warnier-Orr Diagrams for Building Blocks

Statement A Statement B fa) The Sequence Structure

Condition Condition

{statement A {Statement B

(b) The Selection Structure

(0, End)

(Statement A

(c) The Iteration Structure

h o w o f t e n t h e l o o p is to b e p e r f o r m e d . A variable n u m b e r o f i t e r a t i o n s is i m p l i e d b y e n c l o s i n g t w o n u m b e r s , for e x a m p l e (0, End) to i n d i c a t e e x e c u t i o n u n t i l a n e n d - o f file c o n d i t i o n is r e a c h e d . T h e r e is n o s p e c i f i c n o t a t i o n for t h e c a s e c o n s t r u c t . Figure 3.10 c o n t a i n s a Warnier-Orr d i a g r a m for t h e tuition b i l l i n g p r o g r a m . The diagram depicts the s a m e m a n a g e r / s u b o r d i n a t e relationships as the hierarchy chart of Figure 3.3c, b u t u n l i k e t h e h i e r a r c h y chart, is r e a d f r o m left t o right rather t h a n f r o m t o p to b o t t o m . T h e d i a g r a m a l s o c o n t a i n s a d d i t i o n a l i n f o r m a t i o n n o t f o u n d i n t h e h i e r a r c h y chart, n a m e l y t h e l o g i c to i n d i c a t e h o w o f t e n , a n d i n w h a t s e q u e n c e , s u b o r d i n a t e m o d u l e s are e x e c u t e d .

All p r o g r a m s r e q u i r e e x t e n s i v e t e s t i n g t o e n s u r e that t h e y c o n f o r m t o t h e original s p e c i f i c a t i o n s . H o w e v e r , t h e q u e s t i o n o f w h e n c o d i n g e n d s a n d t e s t i n g b e g i n s is n o t as straightforward as it m a y a p p e a r , a n d g i v e s rise to t h e p h i l o s o p h y of top-

down testing.T o p - d o w n t e s t i n g s u g g e s t s that c o d i n g a n d t e s t i n g are parallel activities, a n d e s p o u s e s t h e p h i l o s o p h y that t e s t i n g b e g i n s e v e n b e f o r e a p r o g r a m is c o m p l e t e l y f i n i s h e d . T h i s is a c c o m p l i s h e d b y initially c o d i n g t h e i n t e r m e d i a t e - a n d / o r l o w e r l e v e l p a r a g r a p h s as stubs, that is, partially c o d e d p a r a g r a p h s w h o s e p u r p o s e is t o indicate only that the paragraph has b e e n executed. The stub paragraphs do n o u s e f u l w o r k p e r s e , a n d are u s e d o n l y t o test t h e overall flow o f t h e p r o g r a m . T h e r a t i o n a l e is that t h e h i g h e s t ( a n d m o s t difficult) m o d u l e s s h o u l d b e t e s t e d earlier a n d m o r e o f t e n t h a n t h e l o w e r - l e v e l r o u t i n e s ; t h e latter c o n t a i n d e t a i l e d b u t o f t e n trivial logic, a n d are l e a s t i m p o r t a n t w i t h r e s p e c t to t h e overall p r o g r a m flow. Figure 3.11 is a n e x a m p l e o f s u c h a p r o g r a m . It is complete in t h e s e n s e t h a t it c o n t a i n s a p a r a g r a p h for e v e r y m o d u l e in t h e h i e r a r c h y chart o f F i g u r e 3.3, y e t incomplete i n t h a t m o s t o f its p a r a g r a p h s c o n s i s t o f a s i n g l e DISPLAY s t a t e m e n t .

C h a p t e r

3

A

Methodology

for

Program

Development

Figure 3.10

Warnier-Orr Diagram for Tuition Billing Program I Open files Write-heading-line Read-student-file(1) Compute-tuition (1) Compute-union-fee

Compute-i ndi vidual-bi11 (1)

(1)Compute-acti vi t y - f e e (1) Compute-scholarship (1)

Tuition-Billing Program

Process-record (0, End) Increment-uni versi ty-total s(1)

Wri te-detai1-1i ne(1)

Read-student-file (1)Wri t e - u n i v e r s i t y - t o t a l s (1) Close f i l e s Stop run

(DISPLAY is o n e o f t h e m o s t u s e f u l s t a t e m e n t s i n COBOL a s it a l l o w s t h e p r o g r a m m e r t o print a m e s s a g e o n t h e s c r e e n f o r e x a m p l e , DISPLAY " C O M P U T E - I N D I V I D U A L BILL p a r a g r a p h h a s b e e n e n t e r e d " o r t o print t h e c o n t e n t s o f a d a t a n a m e f o r e x a m p l e , DISPLAY S T U D E N T - R E C O R D w i t h o u t h a v i n g t o o p e n a file. T h e DISPLAY s t a t e m e n t is c o v e r e d i n C h a p t e r 6.) T h e p r o g r a m i n F i g u r e 3 . 1 1 w a s t e s t e d w i t h t h e d a t a o f Figure 3.12a, a n d p r o d u c e d t h e o u t p u t of Figure 3.12b. Y o u m a y n o t think that m u c h h a s b e e n a c c o m p l i s h e d , b u t c l o s e r e x a m i n a t i o n s h o w s t h a t all o f t h e p a r a g r a p h s i n t h e p r o g r a m w e r e e x e c u t e d , a n d further that t h e y w e r e e x e c u t e d i n t h e correct s e q u e n c e . T h e t e s t i n g h a s d e m o n s t r a t e d t h a t t h e overall p r o g r a m f l o w is c o r r e c t . E x e c u t i o n b e g a n w i t h t h e p a r a g r a p h PREPARE-TUITION-REPORT f o l l o w e d b y t h e t h e p a r a g r a p h WRITE-HEADING-LINE. T h e record for t h e first s t u d e n t (JB S m i t h ) w a s r e a d , a n d t h e p a r a g r a p h PROC ESS - S T U D ENT- R E C O R D w a s e n t e r e d . T h e p a r a g r a p h C O M P U T E - I N D I V I D U A L - B I L L w a s e x e c u t e d n e x t , a n d c a l l e d its four s u b o r d i n a t e s t o c o m p u t e t h e t u i t i o n , u n i o n fee, activity f e e , a n d s c h o l a r s h i p . The paragraphs to i n c r e m e n t university totals a n d to write a detail line w e r e also c a l l e d . T h e d a t a for t h e s e c o n d s t u d e n t (HR J a m e s ) w a s r e a d , a n d PROCESSS T U D E N T - R E C O R D ( a n d all its s u b o r d i n a t e s ) w e r e r e - e x e c u t e d . E v e n t u a l l y t h e e n d o f file w a s r e a c h e d , t h e p a r a g r a p h WRITE-UNIVERSITY-TOTALS w a s e x e c u t e d , a n d the program ended. Yes, t h e p r o g r a m r e q u i r e s a d d i t i o n a l d e v e l o p m e n t , b u t t h e h a r d e s t part is over. A n y errors t h a t m a y h a v e e x i s t e d in t h e h i g h e s t - l e v e l m o d u l e s h a v e a l r e a d y

Top-Down

Testing

,

. ...

F i g u r e 3.11

Tuition Billing Program with Stubs

1 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

IDENTIFICATION DIVISION. PROGRAM-ID. TUITI0N3. AUTHOR. CAROL VAZQUEZ VILLAR. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT-FILE ASSIGN TO 'A:\CHAPTR03\TUITI0N.DAT' ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD STUDENT-FILE RECORD CONTAINS 27 CHARACTERS. 01 STUDENT-RECORD PIC X(27). WORKING-STORAGE SECTION. 01 DATA-REMAINS-SWITCH

PIC X(2)

VALUE SPACES.

PROCEDURE DIVISION. PREPARE-TUIT ION-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. PROCESS-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. \Display statement shows current record PERFORM READ-STUDENT-FILE.

C h a p t e r

3

A

Methodology

for

Program

Development

.11

(continued)

49 50 51 52 53 54 55

COMPUTE-INDIVIDUAL-BILL. DISPLAY COMPUTE-INDIVIDUAL-BILL paragraph entered', PERFORM COMPUTE-TUITION. PERFORM COMPUTE-UNION-FEE. PERFORM COMPUTE-ACTIVITY-FEE. PERFORM COMPUTE-SCHOLARSHIP.1

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'.

-roqram siuc

INCREMENT-UNIVERSITY-TOTALS. DISPLAY INCREMENT-UNIVERSITY-TOTALS paragraph entered'.1

WRITE-DETAIL-LINE. DISPLAY ' WRITE-DETAIL-LINE paragraph entered'. WRITE-UNIVERSITY-TOTALS. DISPLAY ' '. DISPLAY 'WRITE-UNIVERSITY-TOTALS paragraph entered'.

b e e n f o u n d , a n d w e r e e a s i e r t o correct t h a n h a d t e s t i n g b e e n d e f e r r e d . Of c o u r s e , later v e r s i o n s o f t h e p r o g r a m c a n still c o n t a i n b u g s , b u t t h e s e errors will o c c u r i n l o w e r level m o d u l e s w h e r e c o r r e c t i o n is g e n e r a l l y easier. T h e m o r e difficult p r o b l e m s will a l r e a d y h a v e b e e n r e s o l v e d i n t h e initial tests, a n d t h a t is p r e c i s e l y t h e g o a l o f t o p - d o w n testing. W e urge y o u to i m p l e m e n t the t o p - d o w n approach to program testing, and offer F i g u r e 3 . 1 3 a s o u r l a s t w o r d o n t h e s u b j e c t . I n t h e t r a d i t i o n a l m o d e o f F i g u r e 3.13a, n o t e s t i n g is d o n e until t h e w e e k e n d b e f o r e t h e p r o g r a m g o e s live (or y o u r a s s i g n m e n t is d u e ) . I n e v i t a b l y l a s t - m i n u t e p a n i c s e t s in, g i v i n g rise t o o v e r t i m e

a n d c h a o s , a n environment unlikely to produce logically correct programs. B yc o n t r a s t , the t o p d o w n a p p r o a c h o f Figure 3 . 1 3 b p r o v i d e s a m o r e u n i f o r m t e s t i n g pattern, b e g i n n i n g a l m o s t i m m e d i a t e l y with the project's i n c e p t i o n a n d c o n t i n u i n g t h r o u g h o u t its d u r a t i o n . T h e r e s u l t s are v a s t l y s u p e r i o r .

Top-Down

Testing

Testing the Tuition Billing ProgramSMITH JAMES BAKER PART-TIMER JONES HEAVYWORKER LEE CLARK GROSSMAN FRANKEL BENWAY KERBEL JB15Y0000230 HR15 0500245 SR09 0500350 JR03Y0000300 PL15Y0000280 HM18 0000200 BL18 0000335 JC06 0000310 SE07 0000215 LF10 0000350 CT03 0250395 NB04 0000100

PREPARE-TUITION-REPORT paragraph entered WRITE-HEADING-LINE paragraph entered PROCESS-STUDENT-RECORD paragraph entered JB15Y0000230 Student record being processed: SMITH COMPUTE-INDIVIDUAL-BILL paragraph entered COMPUTE-TUITION paragraph entered COMPUTE-UNION-FEE paragraph entered COMPUTE-ACTIVITY-FEE paragraph entered COMPUTE-SCHOLARSHIP paragraph entered INCREMENT-UNIVERSITY-TOTALS paragraph entered WRITE-DETAIL-LINE paragraph entered PROCESS-STUDENT-RECORD paragraph entered Student record being processed: JAMES HR15 0500245 COMPUTE-INDIVIDUAL-BILL paragraph entered COMPUTE-TUITION paragraph entered COMPUTE-UNION-FEE paragraph entered COMPUTE-ACTIVITY-FEE paragraph entered COMPUTE-SCHOLARSHIP paragraph entered INCREMENT-UNIVERSITY-TOTALS paragraph entered WRITE-DETAIL-LINE paragraph entered

PROCESS-STUDENT-RECORD paragraph entered Student record being processed: KERBEL NB04 0000100 COMPUTE-INDIVIDUAL-BILL paragraph entered COMPUTE-TUITION paragraph entered COMPUTE-UN ION-FEE paragraph entered COMPUTE-ACTIVITY-FEE paragraph entered COMPUTE-SCHOLARSHIP paragraph entered INCREMENT-UNIVERSITY-TOTALS paragraph entered WRITE-DETAIL-LINE paragraph entered WRITE-UNIVERSITY-TOTALS paragraph entered

C h a p t e r

J -

A Methodology

(or Program

Development

F i g u r e 3.13

Advantages of Top-Down TestingLast-minute panic

Ac o o E ,

0 0 2 13 0 0

$$!$!$

A single (floating) dollar sign is printed before the first significant digit in the edited field, with the leading zero digits, if any, replaced b y blanks. In other words, the floating dollar sign has the s a m e effect as zero suppression. Note, too, that the receiving field m u s t b e at least o n e character longer than the sending field to a c c o m m o d a t e the dollar sign; otherwise, a compiler warning results.

A comma u s e d as a n editing character causes a c o m m a to b e printed, provided a significant digit appears to the left of the c o m m a . T h e c o m m a will b e suppressed, however, if it is preceded b y leading zeros. Consider the following data n a m e s in conjunction with the statement M O V E F I E L D - E T O F I E L D - E - E D I T E D : 05 05 FIELD-E FIELD-E-EDITED PIC 9(4). PIC $$,$$9.IELB-E FIEI

,-iSIMTEB$ $

$Arter

$

9

8 7 6

T

8 , | 7 |65

T h e c o m m a is printed in the indicated position. S u p p c contents of the sending field are less than 1,000, a n d that the statement M O V E F I E L D - F T O F I E L D - F - E D I T E D is executed in conjunction with the fields:

05 05

FIELD-F FIELD-F-EDITED

PIC 9 ( 4 ) . PIC $$,$$9.

coj

0 |0

7Ii

) $ ' $ ; .

0 |0 j8

7 1

j i

! i

j !

$|8|7

T h e c o m m a is suppressed because it w a s not preceded by a significant digit. Observe also h o w the c o m m a is used in conjunction with a floating dollar sign.

T h e asterisk is used as afillcharacter to avoid blanks between a fixed dollar sign a n d the first significant digit as in $****B7. Consider the following fields in conjunction with the statement M O V E F I E L D - G T O F I E L D - G - E D I T E D : 05 05 FIELD-G FIELD-G-EDITED PIC 9 ( 5 ) .PIC $**,**9.

j0[0|00 I 0 0 I8

7 1

*

* i*

T h e dollar sign will print in its fixed position, with asterisks replacing leading zeros. T h e use of the asterisk as afillcharacter is c o m m o n l y referred to as checkprotection.

T h e slash, blank, a n d zero ( /, B, a n d 0, respectively) are insertion characters, m e a n i n g that they are printed exactly w h e r e they appear in a n editedfield.Consider the following fields together with the statement M O V E F I E L D - H T O F I E L D - H EDITED:05 05

FIELD-H FIELD-H-EDITED

PIC 9 ( 6 ) .PIC 99/99/99.

0

3 3

1 618

9

9 0

9;/ 3

9 |9

/ /

9 8

9 9

o

1 6 |8 |9

/

1 |6

F I E L D - H - E D I T E D is an eight-position field a n d is typical of h o w a date field m a y be edited. Blanks a n d zeros m a y be inserted in .similar fashion. Note, however, that the h y p h e n is not a n insertion character a n d cannot, be used to place h y p h e n s within a social security n u m b e r .

Table 7.2 provides a n effective review of the editing characters covered so far. E a c h entry in the table s h o w s the result of a M O V E statement of a numeric sourcefieldto a numeric-edited receiving field. All of the examples maintain decimal alignment as required. (The p w h i c h appears in several examples indicates a space.)

Chapter

7

Fditmg

and

Coding

Standards

Review of Editing Characters

PICTURE

VALUE

PICTURE

EBfTED RESU

9(4) b. 9(4) c. 9(4) d. 9(4)V99 e 9(4)V99 r. 9(4)V99 3 9(4) h. 9(4)V9 i 9(4)V9 . i 9(5) k 9(9) ' 9(4) v.. 9(6) n. 9(6)a.

0678 0678 0678 123456 123456 123456 0008 12345 12345 00045 123456789 1234 080594 080594

Z(4) $9(4) $Z(4) 9(4).99 $9(4).99 $9,999.99 < < < Q t 9(4) 9(4).99 999B99B9999 $$,$$9.00 99/99/99 Z9/99/99

^678 $0678 $^678 1234.56 $1234.56 $1,234.56b y, y, \k y& q ( ppppp$o

1234 1234.50 $***45 123^45^6789 $1,234.00 08/05/94 ^8/05/94

Thus far w e have considered only positive n u m b e r s , a rather unrealistic limitation. N u m e r i c fields with negative values require a n S in their P I C T U R E clause to indicate a signed field, that is, a field that m a y contain either positive or negative values. If the sign (the S in the P I C T U R E clause) is omitted, the value of the data n a m e will always be converted to a positive n u m b e r , regardless of the result of the computation. Consider: 05 05 05 05 FIELD-A FIELD-B FIELD-C FIELD-D PIC S99 PIC 99 PIC S99 PIC 99 VALUE VALUE VALUE VALUE -20. 15. -20. 15.

ADD FIELD-B TO FIELD-A. ADD FIELD-C TO FIELD-D. Numerically, the s u m of -20 a n d +15 is -5, a n d there is n o problem w h e n the result is stored in F I E L D - A as in the first c o m m a n d . In the second c o m m a n d , however, the s u m is stored in F I E L D - D (an unsignedfield),a n d thus it will a s s u m e a value of+5. Accordingly m a n y p r o g r a m m e r s adopt the habit of always using signed fields to avoid a n y difficulty. Signed n u m b e r s require additional editing characters.

Financial statements use either the credit (CR) or debit (1)1$) character to indicate a negative n u m b e r . In other words, the representation of a negative n u m b e r c a n be either C R or D B , a n d d e p e n d s entirely o n the accounting system in use; s o m e systems use C R , whereas others will use D B . Table 7.3 contains four examples that should clarify the matter. In each instance, C R or D B appears only w h e n the sending field is negative [examples (b)

use

a n d (d)j. If the sourcefieldis positive or zero, C R and/or D B are replaced by blanks. T h e essential point is that C O B O L treats C R a n d D B identically, a n d the determination of w h i c h negative indicator to use depends o n the accounting system. '. . . J= C R a n d D B Editing Characters

PICTURE

VALUE

PICTURE

eaiX&Z

RESULT

a b. c ci.

S9(5) S9(5) S9(5) S9(5)

98765 -98765 98765 -98765

,

$$$,999CR $$$,999CR $$$,999DB $$$,999DB

$98,765 $98,765CR $98,765 S98.765DB

Pius a n d Miiiiis Signs

.

.

...

..

Table 7.4 illustrates t h e use of plus a n d m i n u s signs. The repetition of a (plus or minus) sign within t h e edited P I C T U R E clause d e n o t e s a floating (plus or minus) sign, w h i c h will appear in the printed field immediately to the left of the first significant digit. A single (plus or minus) sign, however, indicates a fixed (plus or minus) sign, which prints in the indicated position. Specification of a (fixed orfloating)plus sign displays the sign of the edited field if the n u m b e r is positive, negative, or zero [examples (a), (b), a n d (c)J. Specification of a m i n u s sign, however, displays the sign only w h e n the edited result is negative. T h e receiving field m u s t b e at least o n e character longer than the sending field to a c c o m m o d a t e the sign; otherwise, a compiler warning results. k B L E 7*4 Floating Plus and Minus Sign

VALUE

PICTURE

EDITED R E S U L

S9(4) S9(4) S9(4) S9(4) S9(4) S9(4) S9(4) S9(4)

1234 0123 -1234 1234 0123 -1234 1234 -1234

++,+++ ++,+++ ++,+++--,

,--,

11,119+ 11,119+

+1,234 00+123 -1,234 ,234 000123 -1,234 01,234+ 01,234-

T h e B L A N K W H E N Z E R O clause produces a blankfieldw h e n the associated numeric value is zero. Although the s a m e effect c a n be achieved with certain editing strings, such as Z Z Z Z Z or $$$$$, there are times w h e n the clause is essential. A field with dollars a n d centsfor example, $$$9.99, formatted to print a digit immediately to the left of the decimal pointwill print $0.00. In similar fashion it m i g h t be desirable to blank out a date field with P I C Z9/Z9/Z9 if the values are unavailable. T h e inclusion of B L A N K W H E N Z E R O at the e n d of the P I C T U R E clause in all three instances will accomplish the desired result.

C h a p t e r

7

Editing

and

Coding

Standards

Xh Tuition Billing ProgramD A u i c i t a f int? Visile? U

W e return once m o r e to Figure 7.1, the example with w h i c h w e b e g a n the chapter. The " version of the tuition billing program in Chapter 5 did not include editing characters, a n d so produced the output in Figure 7.1a. N o w w e incorporate . ^ . , . . , . . , . . , the material just presented into a revised version of the p r o g r a m to produce the edited output of Figure 7.1b. T h e necessary changes are highlighted in Figure 7.3, w h i c h compares edited a n d unedited P I C T U R E clauses. T h e changes affect only the detail (7.3a) a n d totalr

e a r

e r

F i g u r e 7.3

Edited versus Unedited P I C T U R E C l a u s e s

EDITED FIELDS

U N E D I T E D FIELDS

01

DETAIL-LINE. 05 FILLER 05 DET-LAST-NAME 05 FILLER 05 DET-INITIALS 05 FILLER 05 DET-CREDITS 05 FILLER 05 DET-TUITI0N 05 FILLER 05 DET-UNION-FEE 05 FILLER 05 DET-ACTIVITY-FEE 05 FILLER 05 DET-SCHOLARSHIP 05 FILLER 05 DET-IND-BILL 05 FILLER

PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC

X X(15). X(2) X(2). X(5) Z9. X(4) X(6) $$$9 X(5) $$$9 X(6)

VALUE SPACES. VALUE SPACES. VALUE SPACES. VALUE SPACES. SPACES. WHEN ZERO. SPACES. WHEN ZERO. SPACES. WHEN ZERO SPACES. SPACES.

$$$$,$ 19.

VALUE BLANK VALUE BLANK VALUE $$.$$$') BLANK X(4) VALUE $$$$,$$9CR. VALUE X(47)

PIC 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 VALUE SPACES VALUE SPACES VALUE SPACES VALUE SPACES VALUE SPACES VALUE SPACES VALUE SPACES VALUE SPACES

!a) Detail Line 01 TOTAL-LINE. VALUE PIC X(8) 05 FILLER PIC X(17) 05 FILLER VALUE 'UNIVERSITY TOTALS'. PIC X(6) VALUE 05 FILLER PIC $$$$,$$9. 05 TOT-TUITION PIC X(2) VALUE 05 FILLER PIC $$$$,$$9. 05 TOT-UNION-FEE PIC X VALUE 05 FILLER PIC $$$$, f$9. 05 TOT-ACTIVITY-FEE PIC X(5) VALUE 05 FILLER PIC $$$$,, . 05 TOT-SCHOLARSHIP m PIC X(4) VALUE 05 FILLER T0T-IND-BILL PIC $$$$, ZERO'. j 05 LEASING-MSG PIC X(40) VALUE 'REFER TO LONG-TERM LEASING'. \ i 05 NON-NUM-MILES-IN-MSG PIC X(40) ! I VALUE 'NON-NUMERIC MILES IN'. | 05 NON-NUM-MILES-OUT-MSG PIC X(40) VALUE 'NON-NUMERIC MILES OUT'. ! 05 LESS-THAN-MILES-MSG PIC X(40) | VALUE 'MILEAGE IN LESS THAN MILEAGE OUT' . : 05 INVALID-MILES-MSG PIC X(40) ! VALUE 'MILES DRIVEN UNREASONABLY LOW . j 05 NON-NUM-RATE-MSG PIC X(40) VALUE 'NON-NUMERIC MILEAGE RATE . 05 MILEAGE-RATE-MSG PIC X(40) i VALUE 'MILEAGE RATE OUT OF RANGE . | 05 INSURANCE-MSG PIC X(40) | !_ __ VALUE 'INSURANCE CODE MUST BE Y OR N'. j " ~ ~ ~ " - 01 TODAYS-DATE. 05 TODAYS-YEAR PIC 99. 05 TODAYS-MONTH PIC 99. 05 TODAYS-DAY PIC 99.1 1 1 1 _

01 HEADING-ERROR-LINE-ONE. 05 FILLER PIC X(26) VALUE SPACES. 05 FILLER PIC X(19) VALUE 'ERROR REPORT AS OF '. 05 HDG-DATE. 10 HDG-MONTH PIC 99/. 10 HDG-DAY PIC 99/. 10 HDG-YEAR PIC 99. 05 FILLER PIC X(79) VALUE SPACES. 01 HEADING-ERROR-LINE-TWO. 05 FILLER 05 FILLER 05 FILLER 05 FILLER

PIC X(10) PIC XX PIC X(9) PIC X(8)

VALUE VALUE VALUE VALUE

'CONTRACT # SPACES. 'LAST NAME' SPACES.

P c. ^ - e s .

L

(continued)

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139i yi A 1HU

05 05 05 05 01

FILLER PIC X(21) VALUE 'ERROR MESSAGE & FIELD'. PIC X(21) VALUE SPACES. FILLER PIC X(8) VALUE 'CONTENTS' FILLER PIC X(46) VALUE SPACES. FILLER

ERROR-LINE. 05 FILLER 05 ERR-CONTRACT-NO 05 FILLER 05 ERR-LAST-NAME 05 FILLER 05 ERR-MESSAGE 05 FILLER 05 ERR-CONTENTS 05 FILLER

PIC PIC PIC PIC PIC PIC PIC PIC PIC

XX VALUE SPACES. 9(6). X(4) VALUE SPACES. X(15). XX VALUE SPACES. X(40). XX VALUE SPACES. X(23). X(38) VALUE SPACES.

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

01

ERROR-DETAILS. 05 ERR-MILES-IN-OUT. 10 FILLER PIC 10 ERR-MILES-IN PIC 10 FILLER PIC 10 ERR-MILES-OUT PIC 05 ERR-RETURNED-DATE. 10 ERR-RETURNED-MONTH-DAY 15 ERR-RETURNED-MONTH 15 ERR-RETURNED-DAY 10 ERR-RETURNED-YEAR ERR-EXPECTED-MILES. 05 10 FILLER PIC 10 ERR-DAYS-RENTED PIC 10 FILLER PIC 10 ERR-MILES PIC

X(4) VALUE 'IN: 9(6). X(6) VALUE OUT: 9(6).

PIC 99. PIC /99. PIC /99. X(6) VALUE 99. X(9) VALUE 9(6). DAYS: MILES:

PROCEDURE DIVISION. OOO-CREATE-VALID-RENTAL-FILE. OPEN INPUT RENTAL-FILE OUTPUT VALID-RENTAL-FILE ERROR-FILE. PERFORM 100-GET-TODAYS-DATE. PERFORM 200-WRITE-ERROR-HEADINGS. PERFORM 300-READ-RENTAL-RECORD. PERFORM 400-PR0CESS-RENTAL-REC0RDS UNTIL NO-DATA-REMAINS. CLOSE RENTAL-FILE VALID-RENTAL-FILE ERROR-FILE. STOP RUN. 100-GET-TODAYS-DATE.

The

Stand-Alone

Edit

Program

1 174175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

(continued) [ACTEPJ~^MOVE TODAYS-DAY T O HDG-DAY. MOVE TODAYS-YEAR T O HDG-YEAR. 200-WRITE-ERROR-HEADINGS. MOVE HEADING-ERROR-LINE-ONE TO ERROR-RECORD. WRITE ERROR-RECORD AFTER ADVANCING PAGE. MOVE HEADING-ERROR-LINE-TWO T O ERROR-RECORD WRITE ERROR-RECORD AFTER ADVANCING 2 LINES. MOVE SPACES T O ERROR-RECORD. WRITE ERROR-RECORD. 300-READ-RENTAL-RECORD. READ RENTAL-FILE

}

MOVETODAYS-MONTH'TO' H O G - M O N T H .

191 192193 194

AT END MOVE 'NO' TO NO-DATA-REMAINS-SWITCH END-READ.400-PR0CESS-RENTAL-REC0RDS.

195196 197 198 199 200 201 202 203 204 205 206 207 208 209

[ MOVj_' Y E V ~ T0~ VAJLID-RECORD-SWITCH. , PERFORM PERFORM PERFORM 500-VALIDATE-RENTAL-RECORD. 600-WRITE-VALID-RECORD. 300-READ-RENTAL-RECORD.

500-VALIDATE-RENTAL-RECORD. PERFORM PERFORM PERFORM PERFORM PERFORM PERFORM PERFORM PERFORM 510-VALIDATE-CONTRACT-NO. 520-VALIDATE-NAME. 530-VALIDATE-CAR-TYPE. 540-VALIDATE-DATE-RETURNED. 550-VALIDATE-DAYS-RENTED. 560-VALIDATE-MILES-DRIVEN 570-VALIDATE-MILEAGE-RATE. 580-VALIDATE-INSURANCE.

210

510-VALIDATE-CONTRACT-NO.

211212 213 214 215 216 217 218 219 220 221 222 223

IILM^NJMI^^MOVE NON-NUMERIC-CONTRACT-MSG TO ERR-MESSAGE MOVE REN-CONTRACT-NO TO ERR-CONTENTS PERFORM END-IF. 599-WRITE-ERROR-LINE

520-VALIDATE-NAME. I F REN-LAST-NAME = SPACES MOVE LAST-NAME-MSG T O ERR-MESSAGE MOVE SPACES TO ERR-CONTENTS PERFORM END-IF. I F REN-FIRST-NAME = SPACES 599-WRITE-ERROR-LINE

C h a p t e r

its

(continued)

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243O/l/l

MOVE FIRST-NAME-MSG TO ERR-MESSAGE MOVE SPACES TO ERR-CONTENTS PERFORM 599-WRITE-ERROR-LINE END-IF.

II ! H M"

FR E

T I A L N0

IJ*

LPHABETI

LT'

~MOVE INITIAL-MSG TO ERR-MESSAGE MOVE REN-INITIAL TO ERR-CONTENTS PERFORM 599-WRITE-ERROR-LINE END-IF.

530-VALIDATE-CAR-TYPE. IF NOT VALID-CAR-TYPES MOVE CAR-TYPE-MSG TO ERR-MESSAGE MOVE REN-CAR-TYPE TO ERR-CONTENTS PERFORM 599-WRITE-ERROR-LINE END-IF. 540-VA LIDAT E\-DATE-RETURNED. !IF VALID-MONTHS IF 30-DAY-MONTH AND REN-RETURNED-DAY B, IF C > D, M O V E E T O F, E L S E M O V E G T O H. b. IF A > B, IF C > D, M O V E E T O F, E L S E M O V E G T O H, E L S E M O V E X T O Y. c. IF A > B, IF C > D, M O V E E T O F, A D D 1 T O E, E L S E M O V E G T O H, A D D 1 T O G. d. IF A > B, M O V E X T O Y, M O V E Z T O W , E L S E IF C > D M O V E 1 T O N, E L S E M O V E 2 T O Y, A D D 3 T O Z. 2. Given the nested IF statement: 1 IF SEX = M' PERFORM PROCESS-MALE-RECORD ELSE IF SEX = 'F' PERFORM PROCESS-FEMALE-RECORD ELSE PERFORM WRITE-ERROR-MESSAGE END-IF END-IF. and the logically equivalent code: IF SEX = 'M' PERFORM PROCESS-MALE-RECORD END-IF. IF SEX = 'F' PERFORM PROCESS-FEMALE-RECORD END-IF. IF SEX NOT = 'M' AND SEX NOT = 'F' PERFORM WRITE-ERROR-MESSAGE END-IF. a. Discuss the relative efficiency of the two alternatives. b. What would be the effect of changing A N D to O R in the third IF of the second set of statements? c. What would be the effect of removing the word E L S E wherever i occurs in the t first set of IF statements? 3. Are the two IF statements logically equivalent? Statement 1: IF A > B IF C > D ADD 1 TO X ELSE ADD 1 TO Y END-IF END-IF.

C h a p t e r

8

Data

Validation

Statement 2: IF A > B AND C > D ADD 1 TO X ELSE ADD 1 TO Y END-IF. I ry the following sets of values to aid in answering the question: a. A = 5,B = 1,C= 10, D= 15. b. A = 1 , B = 5, C = 10, D = 15. 4. Company XYZ has four corporate functions: manufacturing, marketing, financial, and administrative. Each function in turn has several departments, as shown:FUNCTION MANUFACTURING MARkETING FINANCIAL ADMINISTRATIVE DEPARTMENTS 10, 12, 16-30, 4 1 , 56 6-9,15,31-33 60-62, 75 1-4, 78

Establish condition-name entries so that, given a value of EMPLOYEE-DEPARTMENT, you can determine the function. Include an 88-ievel entry, VALID-CODES, to verify that the incoming department is indeed a valid department (any department number not shown is invalid). 5. Given the following COBOL definitions: 05 LOCATION-CODE PIC 99. 88 NEW-YORK VALUE 10. 88 BOSTON VALUE 20. 88 CHICAGO VALUE 30. 88 DETROIT VALUE 40. 88 NORTH-EAST VALUES 10 20. Are the following entries valid as the condition portion of an IF statement? a. IF LOCATION-CODE = '10' b. IF LOCATION-CODE = 40 c. IF NEW-YORK d. IF LOCATION-CODE = 10 OR 20 OR 30 e. IF NEW-YORK OR BOSTON OR CHICAGO f. IF DETROIT = 40 Would the following be valid examples of MOVE statements? g. MOVE 20 TO BOSTON. h. MOVE 20 TO LOCATION-CODE. i. MOVE '20' TO LOCATION-CODE. 6. Given the following pairs of IF statements, indicate whether the statements in each pair have the same effect: a. IF A > B OR C > D AND E = F IF A > B OR (C > D AND E = F) b. IF A > B OR C > D AND E = F IF (A > B OR C > D) AND E = F c. IF A > B OR A > C OR A > D IF A> B OR C OR D d. IF A > B IF A NOT < B OR A NOT = B

Problems

7. Consider the following code, intended to calculate an individual's age from a stored birth date and the date of execution. 01 EMPLOYEE-RECORD. 05 EMP-BIRTH-DATE. 10 BIRTH-MONTH 10 BIRTH-YEAR DATE-WORK-AREA. 05 TODAYS-MONTH 05 TODAYS-DAY 05 TODAYS-YEAR

PIC 99. PIC 99. PIC 99. PIC 99. PIC 99.

01

PROCEDURE DIVISION. ACCEPT DATE-WORK-AREA FROM DATE.

COMPUTE EMPLOYEE-AGE = TODAYS-YEAR - BIRTH-YEAR + TODAYS-MONTH - BIRTH-MONTH. I here are two distinct reasons why the code will not work as intended. Find and correct the errors. 8. Implement the logic in Figure 8.15 with and without scope terminators, corresponding to the implementations in COBOL-74 and COBOL-85. Do you see any distinct advantages to the latter compiler?

Figuufe i i . i S

Flowcharts for Problem 8

statement^

statement,,

statement,

I"

1

1

I

OverviewPERFORM

TEST BEFORE/TEST AFTER In-line Perform Performing Sections PERFORM THRUP r o n r a m m i n n T i n - P e r f o r m PptranrpnhQ READ Mot Sections

False-Condition Branch READ INTO W R I T E FROMINITIALIZE String Processing

INSPECT STRING UNSTRING Reference ModificationACCEPT Duplicate Data Names

Qualification MOVE CORRESPONDINGThe Car

Billing

Program

Programming Specifications Program Design The Completed Program Limitations of COBOL-74 Summary Fill-in True/False Problems

C h a p t e r

9

More

About

the

Procedure

Division

O B J E C T

IVESAfter reading this chapter you will be able to: Differentiate between the DO WHILE and DO UNTIL structures; describe how each is implemented in conjunction with a PERFORM statement. > Define an in-line perform and a false-condition branch; explain how the combination of these features eliminates the need for a priming read statement. Differentiate between a paragraph and a section. Code the READ INTO and WRITE FROM statements in the Procedure Division. Use the INITIALIZE statement. Perform basic string processing operations through use of the INSPECT, STRING, and UNSTRING statements. Define a duplicate data name and use qualification to eliminate ambiguity; describe the use of the MOVE CORRESPONDING statement.

O V E R

VIEWThis chapter completes the two-program sequence begun in Chapter 8 by developing the reporting program for the valid transaction file. The program is also intended to illustrate a series of advanced Procedure Division statements that are presented in the chapter. Many of the statements are new to COBOL-85 and were not available in COBOL-74. We begin with the PERFORM statement and include material on the TEST BEFORE and TEST AFTER clauses which correspond to the DO WHILE and DO UNTIL constructs of structured programming. The in-line perform is presented, as is the THROUGH clause, to perform multiple paragraphs; the use of sections in lieu of paragraphs is also covered. The READ INTO and WRITE FROM clauses are introduced to combine the effects of a MOVE statement with the indicated I/O operation. The ACCEPT statement is expanded to include the DAY-OF-WEEK clause, and the INITIALIZE statement establishes values for multiple data names in a single statement. The INSPECT, STRING, and UNSTRING statements are introduced to implement string processing operations. Duplicate data names, qualification, and the MOVE CORRESPONDING statement are introduced as well.

The program at the end of the chapter is designed very differently from the programs presented thus far as it uses an in-line perform and a false-condition branch to eliminate the priming read used in earlier programs. The program also makes extensive use of scope terminators throughout the Procedure Division.

A simple form of the P E R F O R M statement has been used throughout the text to implement the iteration construct of structured programming: PERFORM procedure-name UNTIL condition T h e condition in the U N T I L clause is tested before the procedure is executed, a n d if the condition is not met, control is transferred to the designated procedure. W h e n the procedure has completed execution, the condition is retested, a n d if it (the condition) is still not met, the procedure is executed a second time. T h e process continues indefinitely until the condition is finally satisfied. In actuality the P E R F O R M statement is considerably m o r e c o m p l e x with m a n y additional options. Consider: PERFORM procedure-name - 1 JTHRQUGH] 'THRU procedure - name - 2

WITH TEST

[BEFORE

UNTIL condition-1

[imperati ve-statement-1 END-PERFORM]

T h e optional T E S T B E F O R E / T E S T A F T E R clause is explained in conjunction with Figure 9.1. Figure 9.1a depicts the D O W H I L E structure that has b e e n used The Iteration Structure

(a) B O W H I L E Construct

(b) DO UNTIL Construct

Chapter

9

More

About

the

Procedure

Division

throughout the book, while Figure 9.1b illustrates the slightly different D O U N T I L structure. T h e difference b e t w e e n the two (aside from the semantics of switching the true a n d false branches) pertains to the sequence in w h i c h the condition a n d statement are executed. T h e D O W H I L E structure of Figure 9.1a tests the condition before executing Block A; the D O U N T I L structure in Figure 9.1b tests the condition after executing Block A. T h e D O W H I L E structure does not execute Block A if the condition is initially false, whereas D O UNTIL guarantees that Block A is executed at least once. T h e P E R F O R M statement includes the T E S T B E F O R E a n d T E S T A F T E R phrases, corresponding to a D O W H I L E a n d D O U N T I L , respectively. Specification of T E S T B E F O R E tests the condition before performing the procedure, a n d corresponds to the D O W H I L E . Specification of T E S T A F T E R performs the procedure a n d then tests the condition, a n d corresponds to a D O U N T I L . Omission of both T E S T B E F O R E a n d T E S T A F T E R (as has b e e n d o n e throughout the text) defaults to T E S T B E F O R E .

T h e procedure-name is enclosed within brackets within the syntax of the P E R F O R M statement a n d thus is a n optional entry. Omission of the procedure n a m e produces a n in-line perform, w h e r e the statements to be executed appear immediately below the P E R F O R M statement itself, as o p p o s e d to the out-of-line execution of a designated procedure elsewhere in the program. For example: PERFORM Statement 1 Statement 2 Other statements to be executed END-PERFORM A n in-line perform functions just as a regular P E R F O R M , except that the statements to b e executed are contained entirely within the statementthat is, between P E R F O R M a n d E N D - P E R F O R M . Omission of the procedure n a m e (that is, specification of a n in-line perform) requires the E N D - P E R F O R M delimiter; conversely, the E N D - P E R F O R M m a y nothe specified in conjunction with performing a paragraph.

T h e procedure n a m e in the P E R F O R M statement can b e either a paragraph or a section. A paragraph consists of o n e or m o r e sentences, whereas a section is m a d e u p of o n e or m o r e paragraphs. Paragraph headers are required to begin in the Amargin (columns 8-11), whereas sentences begin in the B-margin (columns 12-72). T h e compiler recognizes the e n d of o n e paragraph w h e n it senses the beginning of the next paragraphthat is, w h e n it finds the next entry in the A-margin. Section headers also begin in the A m a r g i n a n d are distinguished from paragraph headers b y the reserved w o r d S E C T I O N . W h e n a paragraph is performed, control is transferred to the first sentence in that paragraph a n d remains in that paragraph until the next paragraph is reached. In similar fashion, if the procedure n a m e in a P E R F O R M statement refers to a section (rather than a paragraph), control is transferred to the first paragraph in that section a n d remains in that section until the next section is reached. T h e authors suggest that y o u avoid sections altogether (see tip o n page 233); the material is included here because sections appear in m a n y older C O B O L programs.

PERFORM

PROGRAMMING.

T I P

The motivation behind this guideline is best demonstrated by example. Given the following Procedure Division, what will be the final value of X? PROCEDURE DIVISION. MAINLINE SECTION. MOVE ZERO TO X. PERFORM A. PERFORM B. PERFORM C. PERFORM D. STOP RUN. A SECTION. ADD 1 TO X.B.

ADD I TO X. C. ADD 1 TO X. D. ADD 1 TO X. The correct answer is 7, not 4. A common error made by many programmers is a misinterpretation of the statement PERFORM A. Since A is a section and not a paragraph, the statement PERFORM A invokes every paragraph in that section, namely, paragraphs B, C, and D, in addition to the unnamed paragraph immediately after the section header. A PERFORM statement specifies a procedure, which is either a section or a paragraph, yet there is no way of telling the nature of the procedure from the PERFORM statement itself. Consequently, when a section is specified as a procedure, the unfortunate result is too often execution of unintended code. Can't happen? Did you correctly compute the value of X?

T h e T H R O U G H ( T H R U ) clause executes all statements between the specified procedure n a m e s . T h e procedures m a y b e paragraphs or sections, but proceduren a m e - 1 m u s t be physically before procedure-name-2 within the C O B O L program. A c o m m o n practice is to m a k e procedure-name-2 a single-sentence paragraph consisting of the w o r d EXIT. T h e EXIT statement causes n o action to b e taken; its function is to delineate the e n d of the P E R F O R M . Consider: PERFORM PROCESS-RECORDS THRU PROCESS-RECORDS-EXIT.

PROCESS-RECORDS.

C h a p t e r

9

More

About

the

Procedure

Division

PROCESS-RECORDS-EXIT. EXIT. T h e only practical reason to use a P E R F O R M T H R U statement with a n EXIT paragraph is to enable downward branching to the EXIT statement depending o n a condition within the paragraph. Although a n argument could b e m a d e for this usage in limited instances, the n e e d for such statements as G O T O P R O C E S S R E C O R D S - E X I T should generally be avoided.

The R E A D statement includes two important clausesINTO a n d N O T A T E N D t h a t were not previously presented. Consider: READ file-name RECORD [ JJfJO identifier ] [AT END imperative-statement-1] [NOT AT END imperative-statement-2] [END-READ]

igure 9.2

Structure of a COBOL Prograrr

READ INPUT-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH. PERFORM PROCESS-RECORDS UNTIL DATA-REMAINS-SWITCH = 'NO'.

PROCESS-RECORDS.

READ INPUT-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH.

(a) Priming Read PERFORM UNTIL DATA-REMAINS-SWITCH = 'NO' READ INPUT-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH NOT AT ENDF'v-^t'rp D'v'sr-n - - ' ^ o ^ . ^ >n n,-^.^ ~ , , *n t r c r

jl

j [

jj

j,y

,

{I

|

END-READ END-PERFORM.

j I

(b) False Condition Branch with In-iine Perioral

READ

T h e NOT AT E N D clause specifies a n action for the false branch of a conditional statement; it is c o m m o n l y used is in conjunction with a scope terminator a n d a n in line perform to eliminate the priming read, as s h o w n in Figure 9.2. T h e choice b e t w e e n the priming read in Figure 9.2a a n d the equivalent logic in Figure 9.2b is o n e of personal preference. T h e earlier listings (e.g., the tuition billing p r o g r a m in Chapter 5) used the priming read because it w a s required in COBOL-74 as the earlier compiler h a d neither the false-condition branch nor the in-line perform. M a n y p r o g r a m m e r s are, in fact, so accustomed to the priming read that they continue to use it even though it is n o longer necessary. W e prefer the in line perform a n d false-condition branch, but both techniques are equally acceptable.

T h e READ INTO phrase causes the input record to be stored in t w o places: in the I/O area of the designated file and in the identifier n a m e specified in the I N T O phrase in Working-Storage. T h e statement is illustrated in Figure 9.3, where the input data are available in both E M P L O Y E E - R E C O R D and W S - E M P L O Y E E - R E C O R D . READ INTO is equivalent to the c o m b i n a t i o n of a READ statement a n d a MOVE statement as shown: READ EMPLOYEE-FILE AT END MOVE 'NO' TO DATA-REMAINS-SWITCH NOT AT END .3 The R E A D INTO Statement

FD 01

EMPLOYEE-FILE DATA RECORD IS EMPLOYEE-RECORD. EMPLOYEE-RECORD PIC X(60).

WORKING-STORAGE SECTION. 01 FILLER 01 WS-EMPLOYEE-RECORD. 05 EMP-NAME

PIC X(14) PIC X(25).

VALUE 'WS BEGINS HERE'

PROCEDURE DIVISION.

READ EMPLOYEE-FILE INTO WS-EMPLOYEE-RECORD AT END MOVE 'NO' TO DATA-REMAINS-SWITCH NOT AT END PERFORM PROCESS-THIS-RECORD END-READ.

C h a p t e r

9

More

About

the

Procedure

Division

PERFORM PROCESS-THIS-RECORD n r- n MOVE EMPLOYEE-RECORD TO WS-EMPLOYEE-RECORD.r u nA

T h e advantage of the R E A D I N T O statement is in debugging. If a p r o g r a m ends prematurely, the first task is to identify the record being processed at the instant the problem occurred. T h e F D area is difficult to find, a n d identification of the specific logical record is further complicated b y considerations of blocking. Working-Storage, however, is easy to find because of the literal W S B E G I N S H E R E . T h e technique is not sophisticated, but it does work. O n c e Working-Storage is found, y o u can identify the record in question as well as the values of all other data n a m e s defined in Working-Storage.

T h e WRITE FROM statement is analogous to R E A D I N T O in that it combines the effects of a M O V E a n d a W R I T E into a single statement. T h e general format of the W R I T E statement is:

WRITE record-name [ R M FO [BEFORE], ,a d v a n c i n g

i d e n t i f i e r - ll

[identifier-2 [integer 4 [mnemonic-name)Ipaqf I

LINE LINES

[AFTER J

A single W R I T E F R O M statement, for example, WRITE PRINT-LINE FROM HEADING-LINE AFTER ADVANCING PAGE. is equivalent to the combination of a M O V E a n d a W R I T E statement: MOVE HEADING-LINE TO PRINT-LINE. WRITE PRINT-LINE AFTER ADVANCING PAGE. W R I T E F R O M c a n b e used throughout a p r o g r a m to write heading, detail, a n d total lines.

T h e I N I T I A L I Z E statement sets multiple data n a m e s to initial values in a single statement. Consider: INITIALIZE [identifier-l] ALPHABETIC ALPHANUMERIC

REPLACING NUMERICALPHANUMERIC-EDITED NUMERIC-EDITED

DATA BY

identifier-21iteral- 1

T h e brackets indicate that all parameters are optional; that is, INITIALIZE in a n d of itself is a valid statement that initializes all numeric items in a p r o g r a m to zeros, a n d all n o n n u m e r i c items to spaces. Y o u can also restrict the INITIALIZE statement to one (data n a m e or more,) initialize only specific categories of data n a m e s , and/or initialize to values other than zeros or spaces. T h u s given the C O B O L fragment:01 GROUP-ITEM. 05 05 05 05 NUMERIC-FIELD-1 NUMERIC-FIELD-2 ALPHANUMERIC-FIELD-1 ALPHANUMERIC-FIELD-2 PIC 9 ( 4 ) . PIC 9 ( 4 ) . PIC X(15). PIC X ( 2 0 ) .

T h e statement INITIALIZE G R O U P - I T E M is equivalent to:MOVE ZEROS TO N U M E R I C - F I E L D - 1 . MOVE ZEROS TO NUMERIC-FIELD-2. MOVE SPACES TO ALPHANUMERIC-FIELD-1. MOVE SPACES TO ALPHANUMERIC-FIELD-2.

In similar fashion, INITIALIZE G R O U P - I T E M R E P L A C I N G N U M E R I C B Y Z E R O is equivalent to:MOVE ZEROS TO N U M E R I C - F I E L D - 1 . MOVE ZEROS TO NUMERIC-FIELD-2.

A n dfinally,INITIALIZE G R O U P - I T E M R E P L A C I N G A L P H A N U M E R I C B Y S P A C E S is equivalent to:MOVE SPACES TO ALPHANUMERIC-FIELD-1. MOVE SPACES TO ALPHANUMERIC-FIELD-2.

It is often necessary to operate o n individual characters within afield,w h e n the field is alphanumeric. Operations of this type are called string processing operations, a n d are accomplished with the I N S P E C T , S T R I N G , a n d U N S T R I N G statements in C O B O L . Each of these statements is discussed in detail.

T h e I N S P E C T statement is a convenient w a y to replace o n e character (or character string) with another. Consider:INSPECT i d e n t i f i e r - 1 REPLACING identifier-2 CHARACTERS BY ALL LEADING FIRST [BEFORE } INITIAL [ identifier-3

1 i teral-1AFTERBY

literal-2\ \ INITIAL [AFTER J BEFORE identifier-6 [literal-5

jidentifier- 4

identifier-5 1iteral-4

i1i teral- 3

T h e I N S P E C T statement can b e used with the editing characters of Chapter 7 as illustrated in Figure 9.4. A s s u m e , for example, that social security n u m b e r is stored as a nine-position field (with n o hyphens) in the input record, but is to appear with h y p h e n s in the printed report. T h e M O V E statement transfers the incoming social security n u m b e r to a n 11-position field containing t w o blanks

Chapter f i g u r e 9.401

9

More

About

the

Procedure

Division

The INSPECT StatementRECORD-IN. 05 SOC-SEC-NUM j j

PIC 9(9).

01

PRINT-LINE. 05 S0C-SEC-NUM-0UT PIC 999B99B9999. i

|PROCEDURE DIVISION. |

I[ I

MOVE SOC-SEC-NUM TO SOC-SEC-NUM-OUT. INSPECT SOC-SEC-NUM-OUT REPLACING ALL ' BY1

j |

(denoted b y B in the P I C T U R E clause). T h e I N S P E C T statement replaces every occurrence of a blank in S O C - S E C - N U M - O U T by the desired h y p h e n . Another frequent use of the I N S P E C T statement is the elimination of leading blanks in n u m e r i c fields. (Numeric fields in C O B O L should not contain anything other than the digits 0 to 9 a n d a sign over the rightmost (low-order) position.) Leading blanks c a n be replaced with zeros as follows: INSPECT FIELD-WITH-BLANKS REPLACING LEADING ' ' BY '0'.

T h e S T R I N G statement joins (concatenates) o n e or m o r e fields and/or o n e or m o r e literals into a singlefield.T h u s a S T R I N G statement has the s a m e effect as a series of M O V E statements, except that the destination fields are o n e a n d the s a m e . A n abbreviated f o r m of the C O B O L notation for the S T R I N G follows: identi fier-1 literal-1 identifier-2 literal-2 identifier-3 1iteral-3 SIZE

STRING

DELIMITED BY

INTO identifier-4 [WITH POINTER identifier-5] [END-STRING] T h e above notation c a n be simplified, for our discussion, in the following m a n n e r : STRING sending item INTO receiving field A sending item m a y b e either a n identifier or a literal. E a c h sending item m u s t b e a c c o m p a n i e d by a delimiting clause, w h i c h indicates w h e n to stop m o v i n g characters from the sending field. T h e delimiter c a n take o n e of three forms: 1. A n identifier n a m e that contains the delimiting character(s), 2. A figurative literal or constant w h o s e value is the delimiting character(s), or 3. SIZE, w h i c h transfers the entire contents of the sending item.

tring

Processing

T h e delimiting character(s) itself is not transferred. Figure 9.5 contains an example of the S T R I N G statement in w h i c h the c o m p o n e n t s of a n individual's n a m e are stored separately, then put together to form a single character string. T h e application is not unusual in that a p r o g r a m often requires a person's n a m e in two formats. It is easy, for example, to visualize the n a m e (John H . Smith) as a single entity as it might appear o n a n address label. Y o u w o u l d not, however, w a n t to store the n a m e as a singlefieldas that w o u l d preclude the ability to obtain a n alphabetical

Figure 9,S

The STRING S t a t e m e n t ~ ~ 05 NAME-IN-PIECES. 10 LAST-NAME 10 FIRST-NAME 10 MIDOLE-INTITIAL 05 ENTIRE-NAME

-

1 [ j

PIC X(16) PIC X(10) PIC X. PIC X(29)

(a) Working-Storage holding Areas

MOVE SPACES TO ENTIRE-NAME. STRING FIRST-NAME DELIMITED BY SPACE ' DELIMITED BY SIZE MIDDLE-INITIAL DELIMITED BY SPACE ' ' DELIMITED BY SIZE LAST-NAME DELIMITED BY SPACE INTO ENTIRE-NAME1

(b) STRING Statement

LAST-NAME

SIM

I ITIH

MIDDLE-INITIAL

jH

FIRST-NAME

Ni

ENTIRE-NAME

(1) ENTIRE-NAME j j

(2) ENTIRE-NAME

(3) ENTIRE-NAME

NIB

(4) ENTIRE-NAME

OIH

(5) ENTIRE-NAME

H I N Bf i H

(c) Sequence of Transfer

Chapter

9

More

A bout

the

Pi oc edure

Division

list o n last n a m e ; that is y o u m u s t have access to last n a m e as a separate entity, in order to alphabetize a list. (See problem 3.) T h e Data Division entries in Figure 9.5a define N A M E - I N - P I E C E S to hold the individual fields, a n d E N T I R E - N A M E to hold the concatenated result. Five distinct steps are required to string the individual fields together to form a single n a m e : 1. M o v e F I R S T - N A M E to E N T I R E - N A M E . 2. M o v e a space to E N T I R E - N A M E after the first n a m e . 3. M o v e M I D D L E - I N I T I A L to E N T I R E - N A M E after the space. 4. M o v e a space to E N T I R E - N A M E after the initial. 5. M o v e L A S T - N A M E to E N T I R E - N A M E after the second space. T h e S T R I N G statement in Figure 9.5b accomplishes all five tasks a n d is illustrated in Figure 9.5c. T h e S T R I N G statement executes as follows: 1. T h e characters in the F I R S T - N A M E field are m o v e d (from left to right) to E N T I R E - N A M E until a space is encountered (the delimiter), or the entire contents of F I R S T - N A M E are transferred. 2. T h e literal ' ' (delimiter is SIZE) is m o v e d to the position following the last character of F I R S T - N A M E . 3. T h e M I D D L E - I N I T I A L is m o v e d . 4. T h e literal ' ' (delimiter is SIZE) is m o v e d to the position following the MIDDLE-INITIAL. 5. Finally, each character in L A S T - N A M E is m o v e d until either a space is encountered (the delimiter), or the entire field is transferred.

T h e U N S T R I N G statement breaks a concatenated field into its c o m p o n e n t s a n d is the opposite of the S T R I N G statement. A n abbreviated form of the C O B O L notation for the U N S T R I N G follows:identifier-2 1iteral-1 identi f i e r - 3l literal-2 J

UNSTRING i d e n t i f i e r - 1 |DELIMITED BY

INTO i d e n t i f i e r - 4 [END-UNSTRING]

W e reverse the previous example a n d divide E N T I R E - N A M E into its three c o m p o n e n t s , F I R S T - N A M E , M I D D L E - I N I T I A L , a n d L A S T - N A M E , as s h o w n in Figure 9.6. T h e U N S T R I N G statement operates from left to right o n E N T I R E - N A M E , m o v i n g characters into F I R S T - N A M E until a space is encountered, then into M I D D L E - I N I T I A L , a n d finally into L A S T - N A M E .

Reference modification enables y o u to address a character string that w a s not explicitly definedthat is, a character string within an existing data n a m e . This is d o n e b y specifying the leftmost (starting) position of the string within the data n a m e a n d the length of the string, separating the parameters b y a colon. T h e format for reference modification is s h o w n below a n d is illustrated in Figure 9.7.

String

Processing

The UNSTRING Statement05 NAME-IN-PIECES. 10 LAST-NAME 10 FIRST-NAME 10 MIDDLE-INTITIAL

PIC X(16). PIC X(10). PIC X.

05 ENTIRE-NAME

PIC X(31).

MOVE SPACES TO NAME-IN-PIECES. UNSTRING ENTIRE-NAME DELIMITED BY ' ' INTO FIRST-NAME MIDDLE-INITIAL LAST-NAME.

FIRST-NAME

MIDDLE-INITIAL

LAST-NAME

J

0 H N H B

S M

1 T H

(1) ENTIRE N A M E

J

0 H N 1 H 6

S M

1 T H

(1) ENTIRE N A M E

J 0 H|N

H

s

M

I T H

fC-r-r-r-,J O H N

JH

S M I |T HI LAST-NAME

FIRST-NAME

MIDDLE-NAME

data-name (leftmost position: [length]) In Figure 9.7 T E L E P H O N E - N U M B E R is d e f i n e d a s a 10 p o s i t i o n field w i t h i n a n i n c o m i n g record. P o r t i o n s o f t h i s field are t h e n m o v e d t o EDITED- PH O N E-N U M B ER t h r o u g h r e f e r e n c e m o d i f i c a t i o n ; for e x a m p l e , T E L E P H O N E - N U M B E R (4:3) refers t o p o s i t i o n s 4, 5, a n d 6 w i t h i n T E L E P H O N E - N U M B E R . T h e s p e c i f i c a t i o n of l e n g t h is o p t i o n a l , a n d its o m i s s i o n d e f a u l t s t o t h e e n d o f t h e d a t a n a m e ; i.e., T E L E P H O N E N U M B E R (7:4) a n d T E L E P H O N E - N U M B E R (7:) are e q u i v a l e n t .

Gfcspter

S

More About

the

Procedure

Division

:

iam&

$.7 01

Reference Modification INCOMING-RECORD.

05

TELEPHONE-NUMBER

PIC X(10),

01

EDITED-PHONE-NUMBER. 05 FILLER 05 AREA-CODE 05 FILLER 05 EXCHANGE 05 FILLER 05 DIGITS

PIC PIC PIC PIC PIC PIC

X VALUE '(', X(3). X VALUE ')', X(3). X VALUE X(4).

MOVE TELEPHONE-NUMBER (1:3) TO AREA-CODE. MOVE TELEPHONE-NUMBER (4:3) TO EXCHANGE. MOVE TELEPHONE-NUMBER (7:4) TO DIGITS.

T h e ACCEPT s t a t e m e n t w a s i n t r o d u c e d i n C h a p t e r 8 t o o b t a i n t h e d a t e o f e x e c u t i o n a n d i m p l e m e n t v a r i o u s f o r m s o f d a t e v a l i d a t i o n . T h e s t a t e m e n t i s e x p a n d e d i n this chapter to i n c l u d e t h e day of the w e e k as well as t h e date. Consider:

f DAY-OF-WEEK" DATE ACCEPT identifier-1 FROM < DAY TIME

T h e DAY-OF-WEEK c l a u s e r e t u r n s a n i n t e g e r f r o m 1 t o 7 r e p r e s e n t i n g t h e d a y a c c o r d i n g t o t h e f o l l o w i n g table:INTEGER DAY

1 2 3 4 5 6 7

Monday Tuesday Wednesday Thursday Friday Saturday Sunday

T h e ACCEPT s t a t e m e n t i s i l l u s t r a t e d in Figure 9.8. T h e u s e r d e f i n e s a d a t a n a m e i n W o r k i n g - S t o r a g e f o r e x a m p l e , D A Y - C O D E - V A L U E i n Figure 9.8athen a c c e p t s

The ACCEPT Statement01 01 DAY-CODE-VALUE TODAYS-DATE. 05 TODAYS-YEAR 05 TODAYS-MONTH 05 TODAYS-DAY HDG-LINE. 05 HDG-DAY-OF-WEEK 05 FILLER 05 HDG-DATE PIC 9.

PIC 99. PIC 99. PIC 99.

01

PIC X(9). PIC XX VALUE PIC X(8).

ACCEPT DAY-CODE-VALUE FROM DAY-OF-WEEK. EVALUATE DAY-CODE-VALUE WHEN 1 MOVE ' Monday' TO HDG- DAY -OF -WEEK WHEN 2 MOVE ' Tuesday' TO HDG- DAY -OF--WEEK WHEN 3 MOVE 'Wednesday' TO HDG- DAY -OF--WEEK WHEN 4 MOVE ' Thursday' TO HDG- DAY -OF--WEEK WHEN 5 MOVE ' Friday' TO HDG- DAY -OF--WEEK WHEN 6 MOVE ' Saturday' TO HDG- DAY -OF- WEEK WHEN 7 MOVE ' Sunday TO HDG- DAY -OF- WEEK END-EVALUATE.1

ACCEPT TODAYS-DATE FROM DATE. STRING TODAYS-MONTH '/' TODAYS-DAY '/' TODAYS-YEAR DELIMITED BY SIZE INTO HDG-DATE END-STRING.

the value from D A Y - O F - W E E K into that tlata n a m e . T h e subsequent E V A L U A T E statement expands the one-position code to a literal day. T h e D A T E a n d D A Y clauses were described in Chapter 8 a n d represent the date (in the form y y m m d d ) a n d Julian date (in the form yyddd), respectively. T h e D A T E clause is illustrated in Figure 9.8c for purposes of review.

M o s t programs require that the output contain s o m e of the input, for example, n a m e a n d social security n u m b e r . C O B O L permits the definition of duplicate data names in the Data Division, provided all Procedure Division references to duplicate n a m e s use the appropriate qualification. W e prefer not to use duplicate n a m e s because they violate the prefix coding standard discussed in Chapter 7, but they are used in older programs, a n d are covered here for completeness.

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 01

Duplicate Data Names

STUDENT-RECORD. 05 STUDENT-NAME 05 SOCIAL-SECURITY-NUM 05 STUDENT-ADDRESS. 10 STREET 10 CITY-STATE 05 ZIP-CODE 05 CREDITS 05 MAJOR 05 FILLER

PIC X(20). PIC 9(9). PIC PIC PIC PIC PIC PIC X(15). X(15). X(5). 9(3). X(10). X(3).

PRINT-LINE. 10 STUDENT-NAME 10 FILLER 10 CREDITS 10 FILLER 10 TUITION 10 FILLER 10 STUDENT-ADDRESS. 15 STREET


Recommended