3-1 Chapter 3. To familiarize you with Ways in which data is organized in COBOL Rules for forming...

48
3- 1 Chapter 3

Transcript of 3-1 Chapter 3. To familiarize you with Ways in which data is organized in COBOL Rules for forming...

Page 1: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

3-1

Chapter 3

Page 2: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

To familiarize you with Ways in which data is organized in COBOL Rules for forming data-names Defining input and output files in DATA

DIVISION Defining storage for work fields and

constants Forming Data-Names The FILE SECTION of the DATA DIVISION Types of Data WORKING-STORAGE SECTION of the DATA

DIVISION

3-2

Page 3: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-3

Page 4: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-4

Page 5: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

1. Date-Of-Birth2. Amount$Out3. Section4. -First-Name5. 98-66. Time out

3-6

Page 6: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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)

3-7

Page 7: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-8

Page 8: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• Input files• Master files• Transaction files -> updates

• Output files• New Master files -> mix the two to get• Report files

3-9

Page 9: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• 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

3-10

Page 10: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

FD file-name RECORD IS OMITTED LABEL RECORDS ARE STANDARD

RECORD CONTAINS integer-1 CHARACTERS BLOCK CONTAINS integer-2 RECORDS .

3-11

Page 11: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

LABEL RECORDS clause◦ Used if labels stored in file (OS will handle this for

us, so we won’t use this clause) BLOCKS CONTAINS clause

◦ Indicates blocking factor for disk files (We won’t be using this clause) Has to do with disks that store data relative to blocks.

Just in case you see it later.

3-12

Page 12: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-13

Page 13: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• Each FD (File Descriptor) 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

3-14

Page 14: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

01 Employee-Rec-In. 05 Name-In … 05 Annual-Salary-In … 05 Job-Description-In …

• Fields at 05 level subordinate to 01 level entry

• All fields at same level (05), independent or not subordinate to each other

• Leave room in numbering in case you need to add in between.

3-15

Page 15: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• REVIEW:• SELECT names file, assigns it to hardware device• FD describes file• 01 names record• 02-49 describes fields within record

3-16

Page 16: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-17

Page 17: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-18

Page 18: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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• Makes no sense because First-Name-In wouldn’t

have a PIC clause and same indentation• Both first and last name are at same level and

should have same level number

3-19

Page 19: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• Specify type of data stored in field• Indicate size of field

3-20

Page 20: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• 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

3-21

Page 21: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• A for alphabetic• X for alphanumeric• 9 for numeric

3-22

Page 22: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

Denote size of field by: Number of A’s, X’s or 9’s used in PICTURE

01 Cust-Rec-In.05 Cust-ID-In Picture XXXX.05 Amt-In Picture 99999.

3-23

Page 23: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

My also denote size of field by: A, X or 9 followed by number in parentheses

01 Cust-Rec-In.05 Cust-ID-In Picture X(4).05 Amt-In Picture 9(5).

3-24

Page 24: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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◦ Positions must still be defined using PIC clause◦ Common for printing/display

3-25

Page 25: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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.

3-26

Page 26: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• Entries in DATA DIVISION reserve storage for data

• Entries in FILE SECTION reserve storage for data from records in input/output files

• WORKING-STORAGE SECTION reserves storage for keyed input/output

3-27

Page 27: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• 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

3-28

Page 28: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

• Numeric literal• Examples: .05 5280 199.99• Constant used for arithmetic operations

• Nonnumeric (alphanumeric) literal• Examples: “INVALID” “Enter your name”

• Figurative constant• SPACES ZEROS• Reserved word for commonly used values

3-29

Page 29: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-30

Page 30: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-31

Page 31: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-32

Example

Page 32: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-33

Example

Page 33: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-34

Page 34: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

Rules for user-defined data-names apply Elementary items:

◦ Must include PICTURE clause◦ May be assigned initial value with VALUE clause

3-35

Page 35: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

To define fields used for Keyed input and displayed output Intermediate arithmetic results Counters and totals End-Of-File Indicators

3-36

Page 36: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

To define initial value for field If omitted, field’s value undefined when

program begins execution May be used only in WORKING-STORAGE

SECTION

3-37

Page 37: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-38

Page 38: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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.

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.

3-39

Page 39: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

Method 2 - store in data item in WORKING-STORAGE SECTION 01 WS-Rate Pic V99 Value .28.

3-40

Page 40: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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 ◦ Reduces likelihood of error since literal value

entered one time◦ Allows meaningful name to be associated with

number

3-41

Page 41: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

Nonnumeric literals may be up to 160 characters

Long literals may not fit on one typed line Subdividing long literals into separate fields

is recommended You may need this when making a long

sentence.

3-42

Page 42: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

Separate this report title into two fields "Transaction Report for XYZ Corporation"

01 Report-Title. 05 Pic X(18) Value 'Transaction Report'. 05 Pic X(20) Value ' for XYZ Corporation'.

3-43

Example

Page 43: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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.◦ Example p. 92

3-44

Page 44: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-45

Page 45: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-46

Page 46: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-47

Page 47: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

Group items are further subdivided Elementary items are not subdivided Elementary items have PICTURE clause Codes X, A, 9, V used to describe data

3-48

Page 48: 3-1 Chapter 3. To familiarize you with  Ways in which data is organized in COBOL  Rules for forming data-names  Defining input and output files in.

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

3-49