Download presentation
Presentation is loading. Please wait.
Published byLynne Fleming Modified over 9 years ago
1
57 3-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout - Modified by Bob Roggio for pedagogical reasons only… John Wiley & Sons, Inc. PowerPoint Winifred J. Rex Presentation Bowling Green State University 10th edition
2
57 3-2 The DATA DIVISION Chapter 3
3
57 3-3 Chapter Objectives To familiarize you with Systems design considerations Ways in which data is organized Rules for forming data-names Defining input and output files in DATA DIVISION Defining storage for work fields and constants
4
57 3-4 Interactive and Batch Programs Fully Interactive Programs –Use ACCEPT to get input from keyboard –Use DISPLAY to display output on screen Full Batch Programs –Get input from files on disk –Direct output to files on disk Hybrid Programs –Use combination of interactive and batch input and output methods
5
57 3-5 COBOL Data Organization Field - group of characters forming a meaningful unit or basic fact –Characters in a name or digits in an amount Records - group of related fields –All fields related to customer File - group of related records –Customer file made up of customer records
6
57 3-6 Categories of Data Data categories in COBOL are each assigned user-defined name –Files, records, fields in batch programs –Fields keyed in or displayed in interactive programs Data-name or identifier for user-defined name in DATA DIVISION must follow certain rules
7
57 3-7 User-Defined Data-Name Rules 1.1 to 30 characters 2.Letters, digits, hyphens (-) only 3.No embedded blanks 4.At least one alphabetic character 5.May not begin or end with hyphen 6.May not be COBOL reserved word
8
57 3-8 Identify the valid data-names 1. Date-Of-Birth 2. Amount$Out 3. Section 4. -First-Name 5. 98-6 6. Time out 7. r2d2 8. Amount-in See examples, p. 65
9
57 3-9 Data-Name Guidelines 1.Use meaningful data-names that describe contents of field Amount-Due-In instead of A1 2.Use prefixes or suffixes in data-names when appropriate -IN and -OUT for fields (Emp-Salary-IN and Emp- Salary-OUT) -FILE and -RECORD for file and record names (Emp-File and Emp-Record) See our local standards_ two character prefix associated with record name.
10
57 3-10 DATA DIVISION Defines, describes storage for all data Two main sections – FILE SECTION Defines all input and output files, records, fields Required for any program that uses files, typically batch programs – WORKING-STORAGE SECTION Defines constants, end-of-file indicators and work areas Defines fields not part of input or output files Lots of print lines and constant records will go here.
11
57 3-11 Types of Files to Define Input files Master files Transaction files Add, Change, and Delete records to the Master File Output files New Master files Report files
12
57 3-12 The Program Name Old Master File Transaction File Input New Master File Report 1 Master File Register Report 2 Change List Report 3 Error List Sample System Flow Chart Traditional Batch Processing Actual names of files would be used rather than generic names as I have entered here.
13
57 3-13 File Description Entries Each file described with an FD (File Descriptor) sentence One FD for each SELECT statement in ENVIRONMENT DIVISION FD followed by File-name Optional clauses to describe file and format of its records
14
57 3-14 File Description Format FD file-name RECORD IS OMITTED LABEL RECORDS ARE STANDARD RECORD CONTAINS integer-1 CHARACTERS BLOCK CONTAINS integer-2 RECORDS. What does the syntax tell you about these entries? What is required? What is optional?
15
57 3-15 File Description clauses LABEL RECORDS clause Used if labels stored in file Label Records are Standard Label Records are Omitted BLOCKS CONTAINS clause –Indicates blocking factor for disk files Discuss Block contains 40 Records Block 100 Both labels and blocking usually handled by operating systems so clauses will be omitted
16
57 3-16 RECORD CONTAINS clause Indicates size of each record Optional but recommended since it provides check on record size Given this FD FD Sales-File Record Contains 75 Characters. If PICTURE clauses mistakenly add up to 76, compiler will report a syntax error and this is desirable!
17
57 3-17 Defining a Record Each FD followed by record description entries for the file Data grouped in COBOL by levels Record-name defined at the 01 level Considered highest level of data Fields within a record defined at subordinate level with level numbers from 02 to 49
18
57 3-18 Record Description Example 01 Employee-Rec-In. 05 Name-In … 05 Annual-Salary-In … 05 Job-Description-In …Why 05? Fields at 05 level subordinate to 01 level entry All fields at same level (05), independent or not subordinate to each other The order of the fields also indicates the corresponding expected order of fields in the actual data! These must correspond.
19
57 3-19 SELECT, FD, 01 entries & fields SELECT names file, assigns it to hardware device FD describes file 01 names record 02-49 describes fields within record
20
57 3-20 Elementary and Group Items Items defined with a level number are one of two types –Elementary item - field that is not further subdivided Must include a PICTURE clause –Group item - field that is further subdivided Has no PICTURE clause –And it is via the pic clause that you tell them apart. Of course, a group item will also have elementary items beneath it all with higher level numbers too!
21
57 3-21 Elementary and Group Items 01 Employee-Rec-In. 05 Name-In … 10 First-Name-In (Picture clause) 10 Last-Name-In (Picture clause) 05 Annual-Salary-In (Picture clause) Name-In is group item since it is subdivided into first and last name Employee-Rec-In also group item First-Name-In is elementary item since it is not further subdivided
22
57 3-22 Invalid Level Numbers 01 Employee-Rec-In. 05 Name-In … 10 First-Name-In (Picture clause) 12 Last-Name-In (Picture clause) 05 Annual-Salary-In (Picture clause) Last-Name-In level number is invalid since it is not subordinate to First-Name-In yet its level number implies that it IS. Both first and last name are at same level and should have same level number
23
57 3-23 PICTURE (PIC) clauses Specify type of data stored in field numeric, alphanumeric, alphabetic Indicate size of field number of Xs or 9s implies length in characters of the field.
24
57 3-24 Types of data fields Alphabetic Only letters or blanks For name, item description, etc. Alphanumeric Any character - letters, digits, special characters For an address like 123 N. Main St. Numeric Only digits For fields used in arithmetic operations
25
57 3-25 Data Types in PICTURE clause A for alphabetic X for alphanumeric 9 for numeric
26
57 3-26 Size of Data Fields Denote size of field by: Number of A’s, X’s or 9’s used in PICTURE 01 Cust-Rec-In. 05Cust-ID-InPicture XXXX. 05Amt-InPicture 99999. Cust-Rec-In is a group item, but Cust-ID-In is a four position, alphanumeric field, while Amt-In is a five position numeric field.
27
57 3-27 Size of Data Fields My also denote size of field by: A, X or 9 followed by number in parentheses 01 Cust-Rec-In. 05Cust-ID-InPicture X(4). 05Amt-InPicture 9(5). This is the more common approach. Note the syntax errors you may see – p. 79 in your textbook!
28
57 3-28 Defining Fields in Record Must account for all positions defined in record layout Must describe fields in order they appear in record Field names should be unique For fields not used by program –Data-name may be left blank (preferable) – May use reserved word FILLER as data-name Discuss…. –Positions must still be defined using PIC clause
29
57 3-29 01 Employee-Rec-In. 05 Employee-Name-Inpic x(25). 05 pic x(5) 05 Hours-Worked-Inpic 9(5). 05 pic x(45). or 01 Employee-Rec-In. 05 Employee-Name-Inpic x(25). 05 Fillerpic x(5) 05 Hours-Worked-Inpic 9(5). 05 Fillerpic x(45). This is the old way, but you will see a LOT of this in practice! Note: violates the fact that data names must be unique?
30
57 3-30 Implied Decimal Point For fields used in arithmetic operations Symbol V used in PIC clause to denote location of implied decimal point Decimal point itself not stored as part of number To store value 26.79 in field AMT-IN, code entry as 05 Amt-In Pic 99V99. and, reading the value for Amt-In in would require four positions, that is, 1264 would be read into Amt-In and be ‘treated’ as if it were 12.64.
31
57 3-31 Variable and Constant Data p81 Entries in DATA DIVISION reserve storage for data pic xxxxx reserves space for five character positions… Entries in FILE SECTION reserve storage for data from records in input/output files WORKING-STORAGE SECTION reserves storage for keyed input/output and a host of helping fields, such as print record layouts, counters, accumulators, flags, tables, etc.
32
57 3-32 Variable Data Entries in both SECTIONs define variable data; that is, variables whose value may change during the course of program execution. Contents of field change (vary) with each new record read or new value keyed in or values computed. Contents in input records unknown until program begins execution
33
57 3-33 Constant Data Data with a fixed value Value that does not depend on input Examples: Fixed tax rate of.05 Message “INVALID” displayed when a value is in error.05 and “INVALID” are constant values Add 4 to Total. 4 is a numeric constant.
34
57 3-34 Types of Constants Numeric literal Examples:.05 5280 199.99 Constant used for arithmetic operations Nonnumeric (alphanumeric) literal Examples: “INVALID” “Enter your name” Constant used for all operations except arithmetic Figurative constant SPACES ZEROS Reserved word for commonly used values
35
57 3-35 Rules for Numeric Literals 1.1 to 18 digits. 2.+ or - sign may be included to left of first digit. 3.Decimal point permitted within literal. May not follow last digit. Valid numeric literals 23 +2359.4.125 -68734 4.Invalid: 1234.
36
57 3-36 Rules for Nonnumeric Literals Must be enclosed in quotation marks From 1 to 160 characters, including space Any character in COBOL character set except quotation mark Valid Nonnumeric Literals '123 Main St.' '$14.99' '12,342' 'Enter a value from 1 to 10‘ Note: 123.4 is a numeric literal, “123.4” is NOT a number, but rather a non-numeric literal. Cannot use this guy in computations!
37
57 3-37 Nonnumeric Literals Only characters within quotes are moved or displayed May contain all numbers ('125') but not same as numeric literal (125) –Cannot be used in arithmetic operations –Cannot be moved to field with PIC of 9’s Are not data-names –'Amount-In' not same as field defined in DATA DIVISION called Amount-In –former is a non-numeric constant, the second is a variable or data-name for variable data.
38
57 3-38 Figurative Constants - ZERO ZERO, ZEROS or ZEROES means all zeros Move Zeros To Total-Out Fills each position in Total-Out with a zero May be used with both numeric and alphanumeric fields Often use to ‘clear’ or ‘zero out’ a field. Example
39
57 3-39 Figurative Constants - SPACE SPACE or SPACES means all spaces or blanks Move Spaces To Code-Out Fills each position in Code-Out with a space or blank Use only with alphanumeric fields since blank is invalid numeric character Example
40
57 3-40 WORKING-STORAGE SECTION Follows FILE SECTION Begins with heading on line by itself Starts in Area A, ends with period All items must be defined at 01 level or in entries subordinate to 01 level entry
41
57 3-41 WORKING-STORAGE SECTION Rules for user-defined data-names apply Elementary items: –Must include PICTURE clause –May be assigned initial value with VALUE clause
42
57 3-42 Uses of WORKING-STORAGE To define fields used for Keyed input and displayed output for interactive programming, Intermediate arithmetic results Counters and totals End-Of-File Indicators and much more.
43
57 3-43 VALUE clause To define initial value for field If omitted, field’s value undefined when program begins execution May be used only in WORKING- STORAGE SECTION (actually, this is not true. Can use Value clauses as part of 88 level entries in File Section… but later.)
44
57 3-44 VALUE vs MOVE VALUE clause used in DATA DIVISION –Gives initial value before execution begins MOVE used in PROCEDURE DIVISION –Assigns value to field after program begins MOVE may change initial value of field
45
57 3-45 VALUE Clause Contains literal or figurative constant Data type must match PICTURE Numeric literals or ZEROS used with PIC 9 fields 01 WS-Tax-Rate Pic V99 Value.06. 01 WS-Total Pic 999 Value Zeros. The value clause must be compatible with the picture clause. Value 06 would NOT be compatible with a pic of V99; would be okay with pic 99.
46
57 3-46 VALUE Clause Nonnumeric literals, ZEROS or SPACES used with PIC X fields 01 WS-EOF Pic X(3) Value ‘YES’. 01 WS-Descrip Pic X(8) Value Spaces. Please note that many field values will be initialized (with Value clauses) in Working Storage. This is absolutely the norm – not ALL fields, but many.
47
57 3-47 Defining Constants or Literals Define a literal of.28 (28%) for a tax rate Method 1 - code as literal in PROCEDURE DIVISION Multiply.28 by WS-Salary Giving WS-Tax (here,.28 is a numeric literal (numeric constant) Method 2 - store in data item in WORKING- STORAGE SECTION 01 WS-Rate Pic V99 Value.28. Here, WS-RATE is a numeric variable with an initial value.
48
57 3-48 Defining Constants or Literals Method 2 - use data-name in place of literal in PROCEDURE DIVISION Multiply WS-Rate by WS-Salary Giving WS-Tax –Preferable for literals used more than once –Allows meaningful name to be associated with number
49
57 3-49 Continuation of Literals Nonnumeric literals may be up to 160 characters (enclosed in quotes) Long literals may not fit on one typed line Subdividing long literals into separate fields is recommended
50
57 3-50 Continuation of Literals Separate this report title into two fields "Transaction Report for XYZ Corporation" 01 Report-Title. 05 Pic X18) Value 'Transaction Report'. 05 Pic X(20) Value ' for XYZ Corporation'. but you may continue non-numeric literals across lines. But be careful. Above is best way. Example Note the space…
51
57 3-51 Continuation of Literals Long literals may be continued from one line to next, although this is not recommended Rules for continuation of literals from one line to next –Begin literal with quotation mark. –Continue literal to position 72. Do not end with quotation mark. –Place hyphen on next line in position 7. –Continue literal with quotation mark starting anywhere in Area B. –End literal with quotation mark. –You should try this and be familiar with it, even if you elect not to use this form…
52
57 3-52 COBOL 2002+ Changes 1.LABEL RECORDS clause will be phased out entirely 2.VALUE clause will be allowed in FILE SECTION 3.Way to continue nonnumeric literals will change 4.Margins A and B rules will be guidelines rather than requirements 5. Commas and dollar signs will be permissible in numeric literals
53
57 3-53 Chapter Summary Data organized into files, records and fields Variable data changes with each run depending on input values Constant or literal data defined within program, not entered as input
54
57 3-54 Chapter Summary FILE SECTION includes FD entry for each file used by program Record names coded at 01 level Field names coded at 02-49 level Fields must be defined in order they appear in record
55
57 3-55 Chapter Summary Group items are further subdivided Elementary items are not subdivided Elementary items have PICTURE clause Codes X, A, 9, V used to describe data
56
57 3-56 Chapter Summary WORKING-STORAGE SECTION used for storing –Intermediate results, counters –End-of-file indicators –Interactive data accepted as input or displayed VALUE clauses may be used in this section to initialize fields
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.