Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIS 1753 Introduction to Computer Programming 1. Computer Program A set of instructions that enables a computer to process data Also called software Two.

Similar presentations


Presentation on theme: "BIS 1753 Introduction to Computer Programming 1. Computer Program A set of instructions that enables a computer to process data Also called software Two."— Presentation transcript:

1 BIS 1753 Introduction to Computer Programming 1

2 Computer Program A set of instructions that enables a computer to process data Also called software Two types of computer software –Systems software - controls overall operations of computer –Application software - perform specific tasks 2

3 Application Software Written by application programmers May provide quick solution to one-time problem –Generate ad hoc report, summarize data Or may be run on regularly scheduled basis –Payroll system, Billing system 3

4 Customized Application Software Written for specific users like retail establishments, manufacturing firms, banks, and more Necessary if user has special requirements 4

5 Programming Languages Symbolic language (like COBOL) English-like languages used to write programs Easier than writing programs in machine language Must be translated or compiled into machine language to run on computer 5

6 Programming Languages Machine language Only language computer understands All programs executed on computer must be in machine language Machine language programs difficult to write 6

7 Programming Languages Compiler Translates source code (such as COBOL) into object code (roughly, executable code) Checks for syntax errors in the source code 7

8 Nature of COBOL Business-oriented language Standard language English-like language Relatively easy to understand 8

9 History of COBOL Developed in 1959 as standard language to meet needs of business Committee to develop language convened by Department of Defense Included representatives from academia, business, computer manufacturers 9

10 Standard Versions of COBOL 1960s 1968 1974 1985  wide variations in COBOL compilers  first COBOL standard set by American National Standards Institute (ANSI)  second ANSI standard to make COBOL more efficient, standardized  this ANSI standard incorporated structured programming techniques 10

11 Current and Future Standards 1985 currently the most widely used 2008 is next standard –Approval expected in 2008 or so –Information on 2008 COBOL standard at http://www.ansi.org http://www.ansi.org 11

12 Future of COBOL Likely to remain important language Older COBOL programs need to be updated Still used by many for new program development 12

13 Use of COBOL About 200 billion lines of COBOL source code in use 5 billion new lines added each year Used by 42.7% of application programmers in medium to large U.S. companies 13

14 Overview of the Four Divisions Every COBOL program contains up to four separate divisions in the following order: IDENTIFICATION DIVISION ENVIRONMENT DIVISION DATA DIVISION PROCEDURE DIVISION 14

15 Overview of the Four Divisions IDENTIFICATION DIVISION –Identifies program to operating system –Provides documentation about program ENVIRONMENT DIVISION –Defines file-names –Describes devices used to store them –Not included in fully interactive programs 15

16 Overview of the Four Divisions DATA DIVISION –Describes input and output format of data in files –Defines any constants and work areas PROCEDURE DIVISION –Contains instructions to read input, process it and create output 16

17 17

18 18

19 BIS 1753 Introduction to Structured Program Design in COBOL 19

20 Basic COBOL Program Structure Originally, each COBOL instruction coded on single line of 80 characters Positions on line reserved for special purposes Rigid column rules dropped in 2008 OpenCOBOL supports free-format COBOL 20

21 Coding Rules (fixed format) Columns 1-6 and 73-80 optional and rarely used today Column 7 for continuation, comment, starting new page Columns 8-72 for COBOL program statements 21

22 Coding Rules (fixed format) Column 7 * (asterisk) designates entire line as comment / (slash) forces page break when printing source listing - (dash) to indicate continuation of nonnumeric literal 22

23 Coding Rules (free format) *> at beginning of line indicates comment 23

24 Margin Rules Columns 8-72 divided into two areas –Area A - columns 8, 9, 10, 11 –Area B - columns 12-72 Division, section and paragraph-names must all begin in Area A –First letter of name must begin in column 8, 9, 10 or 11 –Entry may extend into Area B 24

25 Margin Rules All other statements, clauses, and sentences begin anywhere in Area B (column 12, 13, 14, etc.) –Select entries in ENVIRONMENT DIVISION –Data description entries in DATA DIVISION –All PROCEDURE DIVISION instructions 25

26 Rules for User-Defined Words 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 26

27 Coding Guidelines 1. Separate divisions by blank comment line, page eject symbol or blank line 2.Code a single statement per line 3.Code paragraph-names on line by themselves 4.Be liberal in use of comments. Box lengthy comments using asterisks. 27

28 Coding Guidelines 5. Code SELECT statements in logical order (input files first, then output files) although order not required 6. Use separate lines for SELECT, ASSIGN, ORGANIZATION clauses for readability 7.Avoid use of device-specific file- names 28

29 COBOL 2008 Changes Coding rules for Areas A and B will be recommended not required. PROGRAM-ID will be only paragraph in IDENTIFICATION DIVISION. All others can be specified as comments. Length of user-defined words will be increased from 30 to 60 characters. 29

30 30

31 BIS 1753 IDENTIFICATION and ENVIRONMENT Divisions 31

32 IDENTIFICATION DIVISION Provides identifying information about program Divided into paragraphs PROGRAM-ID only required paragraph Other paragraphs optional 32

33 IDENTIFICATION DIVISION Syntax IDENTIFICATION DIVISION. PROGRAM-ID. program-name. [AUTHOR. [comment-entry] …] [other optional paragraphs] 33

34 Syntax Symbology Uppercase words are COBOL reserved words Lowercase words are user-defined entries IDENTIFICATION DIVISION. PROGRAM-ID. program-name. –DIVISION is reserved word –program-name is user-defined data-name Example 34

35 Syntax Symbology Underlined words are required Punctuation if specified is required IDENTIFICATION DIVISION. PROGRAM-ID. program-name. –IDENTIFICATION, DIVISION required –PROGRAM-ID is required paragraph –Periods required after division header, paragraph name and program-name Example 35

36 Syntax Symbology Brackets [ ] mean item is optional, braces { } mean one of enclosed items required Ellipses (...) mean entry may be repeated IDENTIFICATION DIVISION. PROGRAM-ID. program-name. [AUTHOR. [comment-entry] …] –AUTHOR paragraph optional –If included it may have any number of comment entries Example 36

37 ENVIRONMENT DIVISION Describes files and computer devices used to process them Required by programs that process files This division is machine-dependent since devices differ from computer to computer Only division that may change if program run on different computer 37

38 Sections of Environment Division CONFIGURATION SECTION –Describes computer used to compile/execute program –Optional and recommended that you omit it INPUT-OUTPUT SECTION –Describes input and output files –Required for all programs using files 38

39 INPUT-OUTPUT SECTION Format INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT internal-file-name-1 ASSIGN TO externale-file-name-1 [ORGANIZATION IS LINE SEQUENTIAL]. 1 1 Use this clause for all PC files so each line treated as separate record. 39

40 SELECT Statement file-names File-names are user-defined words –Words chosen by programmer to represent some element of program 40

41 41

42 42

43 BIS 1753 DATA Division 43

44 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) 44

45 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 45

46 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, or 77 level (independent data item) 46

47 WORKING-STORAGE SECTION Rules for user-defined data-names apply Elementary items: –Must include PICTURE clause –May be assigned initial value with VALUE clause 47

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

49 PICTURE (PIC) clauses Specify type of data stored in field Indicate size of field 49

50 Types of data fields Alphanumeric (x- prefix) Any character - letters, digits, special characters For an address like 123 N. Main St. Numeric (n- prefix) Only digits Used for input and calculations Numeric edited (ne-prefix) Digits and special characters Used for output 50

51 Data Types in PICTURE clause X for alphanumeric 9 V for numeric Z, $. and more for numeric edited 51

52 Size of Data Fields Denote size of field by: Number of formatting characters used in PICTURE 01 Cust-Rec-In. 05Cust-ID-InPicture XXXX. 05Amt-InPicture 99999. 52

53 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). 53

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

55 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 55

56 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 56

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

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

59 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 59

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

61 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 61

62 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 Example 62

63 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 63

64 BIS 1753 PROCEDURE Division (Interactive I/O) 64

65 PROCEDURE DIVISION Contains instructions for initialization activities processing activities termination activities 65

66 PROCEDURE DIVISION Interactive processing instructions –Accept input from keyboard –Display output on screen 66

67 Paragraphs PROCEDURE DIVISION divided into paragraphs Each is independent module or routine Made up of series of instructions to perform specific set of operations 67

68 Rules for Paragraph-Names Coded in Area A, followed by period Follow rules for forming data-names except may be all digits –1010, 1020, 1030, etc. are valid paragraph names Must be unique 68

69 Procedure Division Statements All statements coded in Area B Statement begins with verb (Accept, Display) Last statement in paragraph ends with period Sentence - series of statements ending with period 69

70 Interactive Program Statements DISPLAY to prompt for input ACCEPT to store input in WORKING- STORAGE areas Various statements to process input DISPLAY to show output 70

71 ACCEPT statement 71

72 DISPLAY statement 72

73 COMPUTE statement 73

74 STOP RUN statement Terminates the program Usually last instruction in main paragraph Execution continues with next paragraph if STOP RUN is omitted 74

75 Comments in COBOL Start with asterisk (*) in column 7 (fixed format) Start with *> in column 1 (free format) Use as reminders and explanations of processing performed by program Use to describe program in IDENTIFICATION DIVISION Use to describe each paragraph in PROCEDURE DIVISION 75

76 BIS 1753 Move statement 76

77 Move statement MOVE identifier-1 TO identifier-2 … literal-1 Move 0 To Total-1, Total-2, Total-3 Move may have multiple receiving fields Full Format 77

78 Numeric MOVE Rules Decimal alignment always maintained Digits in integer part moved right to left starting at decimal point Digits in decimal part moved left to right starting at decimal point 78

79 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 79

80 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 80

81 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 81

82 BIS 1753 Full-screen interactive I/O 82

83 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 DIVISION to define screen’s format 83

84 ACCEPT, DISPLAY Enhancements 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 EXAMPLE 84

85 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 85

86 SCREEN SECTION 01Screen-1. 05Blank Screen. 05Line 3 Column 1 Value 'City'. 05Column 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 EXAMPLE 86

87 87

88 BIS 1753 File Input/Output (I/O) 88

89 COBOL Data Organization File - group of related records –Customer file made up of customer records Record - group of related fields –All fields related to one customer Field - group of characters forming a meaningful unit or basic fact –Characters in a name or digits in an amount 89

90 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 90

91 Types of Files to Define Input files Master files Transaction files Output files New Master files Report files 91

92 File Description Entries Each file must be described with an FD (File Descriptor) entry One FD entry for each SELECT statement in ENVIRONMENT DIVISION FD followed by File-name Optional clauses to describe file and format of its records 92

93 File Description Format FD file-name RECORD IS OMITTED LABEL RECORDS ARE STANDARD RECORD CONTAINS integer-1 CHARACTERS BLOCK CONTAINS integer-2 RECORDS. 93

94 BIS 1753 PROCEDURE Division (File I/O) 94

95 PROCEDURE DIVISION Interactive processing instructions –Accept input from keyboard –Display output on screen Batch processing instructions –Access files and read them –Write output to files 95

96 Batch Processing File I/O Statements to: Initialization activities: open files, generate headings Processing activities: read records, process, generate detail output Termination activities: generate footers, close files 96

97 Batch Program Instructions OPEN - Open files to be processed PERFORM UNTIL –Loop to repeatedly READ and process input records, and WRITE results to output file CLOSE – Close files when done processing STOP RUN – Terminate program execution 97

98 Environment/Data Divisions For each input or output file: Select statement: binds internal file name to external file name fd entry (file description entry) associated record 98

99 OPEN Statement Accesses and makes files available for processing Identifies whether files will be used for input or output 99

100 OPEN Statement INPUT file-name-1 … OPEN OUTPUT file-name-2 … File-names used must appear in SELECT statement File must be accessed with OPEN before reading from or writing to it FORMAT 100

101 PERFORM statement PERFORM paragraph-name To execute instructions in separate paragraph one time Control returns to statement following PERFORM FORMAT 101

102 PERFORM/UNTIL Statement PERFORM paragraph-name UNTIL condition Step 1: Evaluate condition Step 2: If condition evaluates to false, perform paragraph one time FORMAT 102

103 In-Line PERFORM PERFORM UNTIL condition statement(s) END-PERFORM Step 1: Evaluate condition Step 2: If condition evaluates to false, execute in-line statements one time FORMAT 103

104 READ Statement Reads record from file opened for input Makes one record available at a time, not entire file (the record is the unit of access) Makes data available in input file’s associated record 104

105 READ Statement READ file-name-1 AT END statement-1 … [NOT AT END statement-2 …] [END-READ] File-name appears in SELECT statement, FD entry and OPEN AT END tests if there are more records FORMAT 105

106 READ Statement If no more records –Executes statement(s) after AT END –Typically statement(s) to cause loop containing READ to end If more records –Reads in next record –Executes statement(s) after NOT AT END –Typically statement(s) to process record just read 106

107 WRITE statement WRITE record-name-1 Transmits data from associated record to output file record-name-1 must be an associated record of a file opened for output FORMAT 107

108 CLOSE statement CLOSE file-name-1... Close specified file(s) Indicates files no longer needed for processing Releases files and deactivates devices FORMAT 108

109 STOP RUN Terminates the program Usually last instruction in main paragraph Execution “falls through” to next paragraph if STOP RUN is omitted 109

110 110

111 111

112 BIS 1753 Report Design Guidelines 112

113 Report Design Guidelines 1.Include heading to identify report 2.Include date, page number 3.Include column headings to identify fields printed 4.Place most significant fields where they are most visible 5.Edit numeric fields for readability 113

114 Report Design Guidelines 6.Include totals at end of page or report 7.Include page footings at end of each page, report footings at end of report 114

115 WRITE Statement WRITE record-name-1 [FROM identifier-1] AFTER integer-1LINE BEFORE identifier-2LINES –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 FORMAT ADVANCING 115

116 Defining Print Records Define each type of output line as separate 01-level record in WORKING- STORAGE May include 01-level records for heading, detail, total, footing lines, etc. Establishes separate storage area for each record All constants and blanks may be preassigned with VALUE clauses 116

117 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 117

118 BIS 1753 Tables 118

119 Defining a Table with the OCCURS clause 01 xTempTable. 05 nTemp occurs 24 times pic S9(3). - Indicates repeated occurrence an element with same format - Defines series of related elements with same format as a table 119

120 Accessing Elements in Table Identifier nTemp is table name Use nTemp along with a subscript to access fields or elements within array Subscript indicates which of the 24 elements to access StatementOutput Display nTemp (2) 2 AM value Display nTemp (23) 11 PM value 120

121 Valid Subscripts Integer literal, data item, or expression Valid values are 1 to number of elements in table 121

122 Processing Elements in Table PERFORM VARYING perfectly suited to process the elements in a table perform 210-process varying nSubscript from 1 by 1 until nSubscript > 24. 210-process. add nTemp(nSubscript) to nTotalTemp. 122

123 In-line PERFORM VARYING perform varying nSubscript from 1 by 1 until nSubscript > 24 add nTemp (nSubscript) To nTotalTemp end-perform. 123

124 Elementary Items with OCCURS Occurs clause may be used with elementary data item 01 xTotalsTable. 05 nTotal occurs 12 times pic 9(5)V99. Defines xTotalstable as 84-character array (12 x 7) of 12 elementary items 124

125 Group Items with OCCURS OCCURS may be used with group item 01 xTaxTable. 05 xGroup occurs 20 times. 10 xCity pic X(6). 10 nTaxRatepic V999. xCity and nTaxRate each occur 20 times in group item xGroup. 125

126 Initializing Elements Two ways to use VALUE clause to initialize all elements to zero 01 xTable-1. 05 nTotal occurs 50 times pic 9(5) value zero. 01 xTable-2 value zero. 05 nTotal occurs 50 times pic 9(5). 126

127 Initializing Elements Can also initialize each element to different value 01xDayNames value 'SUNMONTUEWEDTHUFRISAT'. 05 xDay occurs 7 times pic x(3). Defines table with 7 three-character elements. xDay(1) = SUN, xDay(2) = MON, etc. 127

128 SEARCH Statement SEARCH identifier-1 [AT END imperative-statement-1] WHEN condition-1 imperative- statement-2... CONTINUE [END-SEARCH] Use in place of PERFORM VARYING to search table Format 128

129 SEARCH Statement Identifier used after SEARCH is table name specified in OCCURS entry Condition compares search argument to table argument WHEN clause indicates action to take when condition is met AT END clause specifies action to take if table searched but no match found 129

130 INDEXED BY clause Special field called index must be used with SEARCH Similar to subscript but defined along with table as part of OCCURS 05xTableEntries occurs 1000 times indexed By nIndex. Compiler automatically supplies appropriate pic clause for nIndex 130

131 Index with SEARCH Must initialize index before SEARCH SEARCH performs table look-up, automatically incrementing index Internally, computer can use faster method to access table entries with an index than with a subscript, even when SEARCH not used Both can have values from 1 to number of table elements 131

132 Modifying Index PERFORM … VARYING can modify subscript or index SET is only other statement that can modify index TO SET index-name-1 UP BY integer-1 DOWN BY Format 132

133 Sequential Search Each entry (usually starting with first) checked in order until Condition is met Table completely searched 133

134 Sequential Search Best used when Entries not in order by table argument value (not in numerical or alphabetical order) Entries can be organized so first values are ones searched for most frequently, minimizing search time 134

135 Binary Search Most efficient type of look-up when table entries in sequence by some table field On average, takes fewer comparisons to find match than serial search Called binary search because each comparison eliminates half of entries under consideration 135

136 Binary Search Statement SEARCH ALL identifier-1 [AT END imperative-statement-1] WHEN data-name-1 = identifier-2 literal-1 condition-1 arithmetic- expression-1 imperative-statement-2 CONTINUE [END-SEARCH] Format (partial) 136

137 SEARCH ALL Limitations Condition in WHEN can test only for equality between table and search argument Condition following WHEN may be compound –Only ANDs permitted, not Ors –Each relational test can test only for equality 137

138 SEARCH ALL Limitations Only one WHEN clause can be used VARYING option may not be used Table argument and its index must appear to left of equal sign –Valid: When nCustomerNo (X1) = nCustNoIn –Invalid: When nCustNoIn = nCustomerNo (X1) 138

139 Key Field Must include clause to indicate which table entry serves as key field Must specify whether KEY is –ASCENDING KEY - entries in sequence, increasing in value –DESCENDING KEY - entries in sequence, decreasing in value 139

140 140

141 141

142 BIS 1753 Random access files 142

143 Disk File Organization File is collection of records Three major ways records stored or organized on disk - Sequential File Organization - Relative File Organization - Indexed File Organization 143

144 Sequential File Organization Records stored in order they are written to file Must be accessed in sequence - to access 50th record in file, must read past first 49 Typically sorted into sequence by a key field 144

145 Relative File Organization When records created, key field used to compute a disk address where record is written To randomly access records –User enters key field –Disk address computed from key field –Record then accessed directly No index needed 145

146 Indexed File Organization Consists of two files –Data file - records in sequence –Index file - contains value of Each key field Disk address of record with that corresponding key field For random access, look up key field in index file to find address Then access record in data file directly 146

147 Relative Files Use the value of the unique identifying key value as the record number Records may be accessed either randomly or sequentially 147

148 SELECT for Relative Files RELATIVE KEY clause –Optional if ACCESS is SEQUENTIAL –Otherwise, required ACCESS IS DYNAMIC allows both sequential and random access in same program FILE STATUS field used same way as with indexed files 148

149 FD for Relative Files RELATIVE KEY not part of record –In separate WORKING-STORAGE entry If key is a three digit field and SELECT clause is Relative Key is R-Key Entry in WORKING-STORAGE is 01R-KeyPic 9(3). 149

150 Creating Relative Files May not use a standard text editor Program must be written to create the relative file Relative files are platform-dependent; must create file on the platform on which it will be accessed 150

151 Reading Relative Files (Random access) Move desired key value to RELATIVE KEY data item, then execute READ statement (Random access) May get invalid key condition (Sequential access) READ (Sequential access) May get at end condition 151

152 Relative Keys Sometimes key field not feasible to use as relative key For example, a five digit Trans-No with values from 00001 to 99999 with only 1000 actual records would be wasteful –99999 record locations would need to be allocated but only a small portion used 152

153 Creating an Indexed File Records written in sequence by key field as for sequential disk file Once index file created, records can be accessed randomly 153

154 SELECT Statement ORGANIZATION INDEXED –Indicates index file to be created along with data file –Index file must be established to be able to randomly access file later ACCESS MODE SEQUENTIAL –Records written in sequence by key field –Optional since SEQUENTIAL is default mode 154

155 SELECT Statement RECORD KEY clause –Names key field within disk record used to form index –Must be in same physical location in each record (usually first field) –Value must be unique for each record –Best to use numeric field as key 155

156 WRITE … INVALID KEY INVALID KEY clause required when writing indexed records to handle I/O errors –Key field not in sequence –Key field same as one already in file If error detected with WRITE –Record not written –Statement(s) following INVALID KEY executed 156

157 WRITE … INVALID KEY WRITE record-name-1 [FROM identifier-1] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-WRITE] Statement(s) following NOT INVALID KEY executed if WRITE is successful Format 157

158 READ … INVALID KEY To locate record with key field equal to value stored in record key Move Trans-No To Master-No Read Indexed-File Invalid Key Perform 600-Err-Rtn Not Invalid Key Perform 500-OK-Rtn End-Read 158

159 Debugging Tips Must run program to create indexed file –Cannot be created using text editor To test an update program, always run index file creation program first May not be able to DISPLAY or print indexed records on your system directly –Move data to standard sequential record first 159

160 ALTERNATE RECORD KEY Clause to enable file to be accessed randomly using more than one key field –May want to access accounts receivable records by account number or name Add to SELECT statement after RECORD KEY clause to establish multiple key fields for indexing 160

161 ALTERNATE RECORD KEY [ALTERNATE RECORD KEY IS data-name-2 [WITH DUPLICATES] ] … Multiple ALTERNATE keys allowed Need not be unique Access records by RECORD KEY or any ALTERNATE RECORD KEYs Format 161

162 START Statement To begin processing indexed file sequentially starting from any record location –Print file beginning with customer record with Acct-No = 025 –Print all customers with Cst-Last-Name beginning with letter 'S' 162

163 START Statement START file-name-1 IS = KEY IS > data-name-1 IS NOT < IS >= [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-START] Format 163

164 ACCESS IS DYNAMIC Mode used to access indexed file both randomly and sequentially in single program For example, update selected records, then print control listing of entire indexed file –Random access used for updating –Sequential access used for printing report 164

165 ACCESS IS DYNAMIC Mode required for reading records in sequence by ALTERNATE RECORD KEY Also required when records accessed by both RECORD KEY and ALTERNATE RECORD KEY 165

166 READ … NEXT RECORD To perform sequential read of indexed file when ACCESS MODE IS DYNAMIC To sequentially read from file by its ALTERNATE RECORD KEY To begin reading sequentially from some point other than beginning of file 166

167 FILE STATUS Clause To determine exact type of input or output error that occurred when accessing a file Included in SELECT statement for a file as last clause SELECT … [FILE STATUS IS data-name] Format 167

168 FILE STATUS Clause Data-name must appear in WORKING- STORAGE as two-position alphanumeric field Select Indexed-Pay-File … File Status Is WS-Status. … Working-Storage Section. 01WS-StatusPic X(2). Example 168


Download ppt "BIS 1753 Introduction to Computer Programming 1. Computer Program A set of instructions that enables a computer to process data Also called software Two."

Similar presentations


Ads by Google