Structured COBOL Programming 10 th edition John Wiley
Structured COBOL Programming 10 th edition John Wiley & Sons, Inc. Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout Power. Point Presentation Winifred J. Rex Bowling Green State University 1
Chapter 6 Moving Data, Printing Information, and Displaying Output Interactively 6 -2
Chapter Objectives To familiarize you with • Options of MOVE statement • Rules for moving fields and literals • Printing edit symbols • Designing and printing reports 3
Chapter Contents • • • Instructions formats of MOVE statement Numeric MOVE Nonnumeric or alphanumeric MOVE Other options of MOVE statement Producing printed output and screen displays • Displaying output interactively using screen input and output 4
MOVE Statement • FORMAT 1 MOVE identifier-1 TO identifier-2 • Copies contents of identifier-1 To identifier-2 • identifier-1 is sending field • identifier-2 is receiving field 5
MOVE Statement Example • Example Move Qty-In To Qty-Out • Contents of Qty-Out replaced by contents of Qty-In • If Qty-In = 253, contents of Qty-Out = 253 after MOVE executed • Qty-In remains unchanged 6
MOVE Statement • FORMAT 2 MOVE literal-1 TO identifier-2 • Value of literal-1 copied to identifier-2 • Data type of literal and identifier should match – Move numeric literals to numeric fields – Move nonnumeric literals to alphanumeric fields 7
MOVE Statement Examples • Examples 05 Qty-Out Pic 999. 05 Color-Out Pic X(4). Move 100 To Qty-Out Move 'Blue' To Color-Out • Contents of Qty-Out replaced with 100 • Contents of Color-Out replaced with 'Blue' 8
Multiple Moves • Full Format MOVE identifier-1 literal-1 TO identifier-2 … Move 0 To Total-1, Total-2, Total-3 • Move may have multiple receiving fields • Contents of all three total fields set to 0 9
Moving Figurative Constants • ZEROS may be moved to numeric or alphanumeric field – Moves 0 to every position of receiving field • SPACES moved only to alphanumeric field – Moves space or blank to every position of receiving field 10
Numeric MOVE • Numeric field or literal is moved to numeric receiving field • When PIC clauses of both fields identical, sending and receiving fields same after MOVE • Previous contents of receiving field are replaced by MOVE 11
Numeric MOVE Rules • Decimal alignment always maintained – If 86. 52 is moved to a field • 86 always treated as integer part of number • . 52 always treated as decimal part of number • Digits in integer part moved right to left starting at decimal point • Digits in decimal part moved left to right starting at decimal point 12
Numeric MOVE Example • Operation: Move Amt-1 To Amt-2 Amt-1 Picture 99 V 99 Contents 12^34 Amt-2 Picture 99 V 99 Contents 67^89 • Digits in integer part moved so that – 2 replaces 7, 1 replaces 6 • Digits in decimal part moved so that – 3 replaces 8, 4 replaces 9 13
Numeric MOVE Example • Operation: Move Amt-1 To Amt-2 Amt-1 Picture 99 V 99 Contents 12^34 Amt-2 Picture 99 V 99 Contents 67^89 • After MOVE, contents of Amt-2 = 12^34 14
Numeric MOVE Rules • If receiving field has more integer positions than sending field – Unfilled high-order (leftmost) integer positions filled with zeros • If receiving field has more decimal positions than sending field – Unfilled low-order (rightmost) decimal positions filled with zeros 15
Numeric MOVE Example • Operation: Move Amt-3 To Amt-4 Amt-3 Picture 9 V 9 Contents 3^4 Amt-4 Picture 99 V 99 Contents 56^78 • Digits in integer part moved so that – 3 replaces 6, 0 replaces 5 • Digits in decimal part moved so that – 4 replaces 7, 0 replaces 8 16
Numeric MOVE Example • Operation: Move Amt-3 To Amt-4 Amt-3 Picture 9 V 9 Contents 3^4 Amt-4 Picture 99 V 99 Contents 56^78 • After MOVE, contents of Amt-4 = 03^40 17
Numeric MOVE Rules • If receiving field has fewer integer positions than sending field – High-order (leftmost) digits truncated • If receiving field has fewer decimal positions than sending field – Low-order (rightmost) digits truncated 18
Numeric MOVE Example • Operation: Move Amt-5 To Amt-6 Amt-5 Picture 99 V 99 Contents 12^34 Amt-6 Picture 9 V 9 Contents 5^6 • Digits in integer part moved so that 2 replaces 5, 1 not moved • Digits in decimal part moved so that 3 replaces 6, 4 not moved 19
Numeric MOVE Example • Operation: Move Amt-5 To Amt-6 Amt-5 Picture 99 V 99 Contents 12^34 Amt-6 Picture 9 V 9 Contents 5^6 • After MOVE, contents of Amt-6 = 2^3 20
Nonnumeric MOVE • Sending field is alphanumeric field or nonnumeric literal • Receiving field is alphanumeric field 21
Rules for Nonnumeric MOVE • Characters moved from left to right • If receiving field longer, low-order (rightmost) positions replaced with spaces • If receiving field shorter, low-order characters in sending field truncated 22
Nonnumeric MOVE Example • Operation: Move Code-1 To Code-2 Code-1 Picture X(3) Contents abc Code-2 Picture X(6) Contents mnopqr • Characters abc replace mno • Remaining three positions in Code-2 replaced with blanks 23
Nonnumeric MOVE Example • Operation: Move Code-3 To Code-4 Code-3 Picture X(5) Contents vwxyz Code-4 Picture X(3) Contents efg • Characters vwx replace efg • y and z are not moved since all positions in Code-4 are filled 24
Group Moves • When receiving field is group item, alphanumeric MOVE rules followed • If subfields are numeric, invalid data may be stored in subfields 25
Group Move Example • Operation: Move "ABCD" To WS-Code 01 WS-Code. 05 WS-Part-1 Pic XX. 05 WS-Part-2 Pic 99. • After MOVE, value of WS-Part-1 is AB, WS-Part-2 is CD • Causes error if WS-Part-2 then used in arithmetic operation 26
MOVE Operations • Avoid using sending field with different data type than receiving field • Move numeric fields, numeric literals or ZEROS to numeric fields • Move alphanumeric fields, nonnumeric literals, ZEROS or SPACES to alphanumeric fields 27
Disk Files vs Output for Users • Conciseness and efficiency important for data stored in disk files • Clarity and readability important for output read by people – Displayed on screen – Printed in reports 28
Output for Users • Edit symbols used to make data fields more readable – Display 001495 as $14. 95 • Use spacing between lines and page margins for readability • Space fields across page or screen • Heading and footing lines added to improve clarity of output 29
Additional Topics • Be certain to look over Qualification of Names – used to be a biggee. But you should still know it. • Also, know the multiple Move statements; that is: Move amt-in to field 1, field 2, field 3. 30
Editing Functions • Report-item - elementary item that contains edit symbols in its PIC clause • Editing occurs when elementary item moved to report-item • PIC clause of report-item determines editing performed • Please note that once numeric items are moved into a report-item field, this report-item field is NO LONGER a numeric field!!! 31
Decimal Points • Implied decimal points (V) do not print • Move field with V in Picture to reportitem field with actual decimal point for printing or displaying 32
Decimal Points • Operation: Move In-Amt To Out-Amt Field Picture Contents Positions In-Amt 99 V 99 12^34 4 Out-Amt 99. 99 12. 34 5 Move In-Amt to Other-Amt 999 012. 340 7 • Actual decimal point – – – Replaces implied one Uses a storage position. Essential for displayed or printed fields Will find these on screens and printed reports. Usually the ‘last’ thing that is done after computations: move the result into a report-item field for printing/displaying. 33
Zero Suppressing • Zeros in leftmost position of number usually suppressed – Value of number not affected 00025 and 25 have same value – Improves readability • Edit symbol Z in PIC clause suppresses leading zeros, replaces with blanks 34
Suppressing Leading Zeros • Use Z for each position where leading zero should be replaced by blank In-Qty Pic 999 018 003 100 000 Out-Qty Pic ZZZ b 18 bb 3 100 bbb 100 has no leading zeros so zeros not suppressed b denotes a blank (space) This last one is not desirable. Will discuss ahead… 35
Suppressing Leading Zeros • To suppress only some leading zeros – Use Z's and 9's in same report-item • Z's must precede 9's Field one is a good picture clause! Examples In-Qty Pic 999 003 000 Out-Qty Pic ZZ 9 bb 3 bb 0 36
Zero Suppression and Decimals • Zeros to right of decimal point are significant so they are not suppressed Examples In-Cost Pic 99 V 99 05^08 00^02 00^00 Out-Cost Pic ZZ. 99 b 5. 08 bb. 02 bb. 00 37
Dollar Signs and Commas • $ and , often used in PICTURE clause with Z and decimal point • Comma suppressed if only zeros precede it Examples In-Cost Pic 9(4)V 99 1027^40 0051^06 0000^04 Out-Cost Pic $Z, ZZZ. 99 $1, 027. 40 $bbb 51. 06 $bbbbb. 04 • Question: Which are numeric fields? Not numeric? 38
Asterisks • Check protection symbol • Used in place of Z to replace leading zeros with * instead of space • Prevents blanks from being filled in with numbers to change amount of check – $ 51. 06 could be changed to $3, 951. 06 – $***51. 06 not easily changed 39
Asterisks Examples WS-Chk-Amt Pic 9(4)V 99 2345^15 0072^08 0000^06 Out-Chk-Amt Pic $*, ***. 99 $2, 345. 15 $***72. 08 $*****. 06 • What are the sizes of these fields? ? Must know this! 40
Plus or Minus Signs • PIC clause with 9’s is unsigned field – Value assumed to be positive • Use S with 9’s in PIC clause of sending field to define signed field – Use PIC of S 9(3) to store -425 • Report-item field must include + or editing symbol to print or display a sign 41
Displaying Minus or Plus Sign • Use - (minus) symbol to display minus sign if sending field is negative – Displays - for negative values – Displays blank (no sign) for positive or unsigned values But still takes one space!!! • Use + (plus) symbol to display sign for both positive and negative values – Displays - for negative values – Displays + for positive or unsigned values 42
Displaying Minus or Plus Sign Examples Assume a PICTURE of S 9(3) for WS-Amt 007 218+ + 082 030 - Out-Amt PICTURE -ZZZ ZZZ+ZZ 9 $ZZZ+ Contents -bb 7 218 b +b 82 $b 3043
Debit and Credit Symbols • For accounting applications replace (minus) sign with – DB to debit an account when negative – CR to credit an account when negative • Always specified to right of report-item • Print only when value moved to report item is negative 44
Debit and Credit Symbols Examples Assume a PICTURE of S 9(3) for WS-Amt 123 123+ Out-Amt PICTURE 999 CR 999 DB Contents 123 CR 123 DB 123 bb 45
Spaces, Zeros, or Slashes • B (space or blank), 0 (zero) or / (slash) may be inserted in report-item PIC as separator • Used to edit either nonnumeric or numeric fields 46
Spaces, Zeros, or Slashes Examples Assume PIC 9(3) for WS-Amt PIC X(3) for WS-Name WS-Amt WS-Name Contents 528 218 RAY JAN Report-Item PICTURE Contents 99 BB 9 52 bb 8 999, 000 218, 000 XBXBX Rb. Ab. Y X/X/X J/A/N 47
Editing and Arithmetic Operations • Editing performed when – Sending field moved to report-item – Result of arithmetic operations stored in report-item • PICTURE of report-item determines type of editing 48
Editing and Arithmetic Operations • All items used in arithmetic operation must be numeric • Error if report-item used in calculation of result • This statement invalid if Total-Out is reportitem Add WS-Total To Total-Out • Report-item may be used after GIVING • GIVING is essentially a Move… 49
Editing and Arithmetic Operations • Add A to B. • Move B to rpt-item field. Fine. – A and B must be numeric fields. • Add A to B Giving C. – A and B must be numeric, – C may be a report-item field whose picture clause causes editing of the sum. – A PIC 999, B PIC 999, C PIC Z, ZZ 9. – Understand? ? 50
Editing with Floating Strings • Used to place $, + or - directly preceding first significant digit – Only these three symbols may be floated! • For example, 000516 becomes $5. 16 instead of $ 5. 16 • In edit string, replace all Z’s with floating character ($, + or -) • Suppresses leading zeros, commas • But the spaces are still counted!!!! 51
Editing with Floating Strings Examples Assume a PICTURE of S 9(4)V 99 for WS-Amt 0002^29 0036^62+ 0007^05 Out-Amt PICTURE $$, $$$. 99 ++, +++. 99 ---99. 99 Contents bbbb$2. 29 bbb+36. 62 bb-07. 05 Move data ‘numerically’ ALL positions of receiving field must be accounted for! First Contents (by authors) is incorrect! 52
BLANK WHEN ZERO Option • To print spaces when sending field is all zeros rather than $. 00 or -0 • For the report-item 05 Qty-Out Pic +++ Blank When Zero. 035 will print as +35 000 will print as bbb (all spaces) 53
Defining Print Records • Define each type of output line as separate 01 -level record in WORKING-STORAGE. – No do not do this. Not the detail line! • May include 01 -level records for heading, detail, total, footing lines, etc. (not detail rec) • Establishes separate storage area for each record • All constants and blanks may be preassigned with VALUE clauses - And this is very good! 54
WRITE … FROM Statement • To transfer data from storage to print area and then print • Replaces MOVE and WRITE Write Print-Rec From Heading-1 instead of Move Heading-1 to Print-Rec Write Print-Rec Heading-1, Heading-2, Trailer-1, etc. commonly used. 55
ADVANCING Option • Single spacing for WRITE is default – Because a Write print-rec really means Write print-rec before advancing one line. • Use AFTER or BEFORE ADVANCING to obtain any number of blank lines between each print line 56
WRITE Statement FORMAT WRITE AFTER BEFORE record-name-1 ADVANCING [FROM identifier-1] integer-1 identifier-2 LINES – integer-1 or identifier-2 must be non-negative integer value – AFTER ADVANCING prints line after paper is spaced – BEFORE ADVANCING prints line before spacing occurs 57
WRITE … AFTER ADVANCING • Write Print-Rec From Heading-Line-1 After Advancing 2 Lines – Causes paper to advance two lines – Heading-Line-1 printed on second line – Creates double spacing - one blank line followed by printed line 58
WRITE … BEFORE ADVANCING • Write Print-Rec From Heading-Line-1 Before Advancing 2 Lines – Heading-Line-1 printed first, then spaces two lines – Results in overprinting if first line already contains printed output – As a rule, use either BEFORE or AFTER in program but not both 59
PAGE option • Word PAGE after ADVANCING causes paper to advance to next page • To advance paper to top of new page and print a heading, code Write Print-Rec From Heading-Line-1 After Advancing Page • Very convenient and used! 60
End-of-Page Control • When page is full, need to advance to next page and print headings again • Define WORKING-STORAGE field for line counter • Initialize line counter to zero and increment it each time a line is printed • When line counter equals or exceeds line limit for page, advance to next page • Always say, If line-counter >= 54 not = 54!!! 61
Printing Page Numbers • • Define WORKING-STORAGE field for page number Initialize and then increment page number each time new page begins Move page number to trailer-line (I prefer trailer line at bottom of page to printed page number in a header line) before printing trailer at bottom of page. If line-counter > 45 move page-number to p-page-nbr write print-rec from trailer-rec after advancing 2 lines move 0 to line-counter add 1 to page-number Perform 1050 -header-routine end-if Discuss! Better: if line-counter > 45 perform xxxx-trailer-routine perform xxxxx-header-routine end-if where header and trailer routines have suitable code… 62
Accessing Date of Run • To get current date in program, access reserved word DATE • Stores date as six-digit field, two digits each for year, month and day • January 25, 2003 stored as 030125 63
Accessing Date of Run FORMAT ACCEPT identifier-1 FROM DATE • For older compilers identifier must be defined as numeric field to hold 6 digits 64
Accessing Date of Run EXAMPLE Accept WS-Date From Date • Where WS-Date defined as: 01 WS-Date. 05 Run-Year Pic 99. 05 Run-Month Pic 99. 05 Run-Day Pic 99. 65
Printing Date of Run • WS-Date fields typically reformatted for printing • Moved to report-item fields listed in month, day, year order with slashes between them • How to do this? • Later, we will have more advanced formats! 66
Date with Four-Digit Year • Newer compilers provide access to full fourdigit year • Use intrinsic function CURRENT-DATE with MOVE in place of ACCEPT – Change Run-Year in WS-Date to Pic 9(4) Move Function Current-Date To WS-Date – If date is January 25, 2003 then 2003 will be stored in Run-Year – You should use this intrinsic function. 67
Report Design Guidelines 1. Include heading to identify report 2. Include date (on header), page number (on trailer, when assigned) 3. Include column headings to identify fields printed 4. Place most significant fields where they are most visible 5. Edit numeric fields for readability 68
Report Design Guidelines 6. Include totals at end of page or report 7. Use *’s to identify level of total EXAMPLE Dept Total is $33, 266. 25* Final total is $167, 267. 53** We will do this a little differently later. 8. Include page footings at end of each page, report footings at end of report 9. I call these trailers. Whatever…. 69
Displaying Output Interactively • No COBOL standard for interactive processing • Two techniques used by compilers to add interactivity – ACCEPT, DISPLAY enhancements with options to define appearance of data on screen – SCREEN SECTION added in DATA DIVISON to define screen’s format 70
ACCEPT, DISPLAY Enhancements EXAMPLE Display "Enter date of birth" At Line 13 Column 1 With Blank Screen Foreground-Color 1 Background-Color 7 • Displays prompt at specific position on screen after clearing screen, using one color for characters and another for background 71
SCREEN SECTION • Specify formats, options for screen • Follows WORKING-STORAGE • Define each screen with 01 level entry followed by subordinate entries • DISPLAY screen-name displays all prompts described in entries for screen • ACCEPT screen-name captures all data typed by user for that screen 72
SCREEN SECTION EXAMPLE 01 Screen-1. 05 Blank Screen. 05 Line 3 Column 1 Value 'City'. 05 Column 17 Pic X(13) To City-In. • Display Screen-1 blanks screen and displays City in first column of line 3 • Accept Screen-1 moves 13 characters user enters starting in line 3, column 17 to City-In 73
COBOL 2002+ Changes • • Will be able to use report-items in arithmetic operations Nice change Will be able to combine or concatenate nonnumeric literals in MOVE statement Very nice. Move 'Stu-Name Credits' & ' Tuition Scholarship' To Column-Heading-Line 74
Chapter Summary Numeric MOVE Rules • • • Decimal alignment maintained Integer and decimal portion digits moved from decimal point out Nonfilled high-, low-order positions replaced with zeros Truncation of high-, low-order digits occurs if receiving field not large enough 75
Chapter Summary • Nonnumeric MOVE Rules • Movement from left to right • Low-order nonfilled positions replaced with spaces • Low-order characters truncated if receiving field not large enough 76
Chapter Summary • Format of receiving field determines type of MOVE operation • See Table 6. 3 in text for summary of edit symbols 77
Chapter Summary • Rules for Printing Output – Use Printer Spacing Chart to determine print positions to be used – Define all printed output in WORKINGSTORAGE so VALUE clause can be used – Use appropriate editing symbols in reportitems 78
Chapter Summary • WRITE statement clauses – Move records to print area in FILE SECTION with WRITE … FROM – Use BEFORE or AFTER ADVANCING to control spacing between lines – Use AFTER ADVANCING PAGE to go to beginning of next page 79
Chapter Summary • Interactive Input/Output – Use SCREEN SECTION or enhanced ACCEPT, DISPLAY verbs to make screen displays more user friendly – Options available to • • Describe exact line, column for item Add color, sounds Highlight, blink, underline, reverse video item Clear all or part of screen 80
Copyright © 2003 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express written permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein. 81
- Slides: 81