32
1 Chapter 5 – The Procedure Division File handling statements OPEN statement • Initiates processing for a file • Input • Output • Each file opened must have been referenced in the select statement in the environment division • And in a corresponding FD in the data division CLOSE statement • Use when access to a file is no longer necessary • Files should be closed before processing terminates • Usually immediately before the STOP command

1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

Embed Size (px)

Citation preview

Page 1: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

1

Chapter 5 – The Procedure Division• File handling statements

– OPEN statement• Initiates processing for a file• Input• Output• Each file opened must have been referenced in the

select statement in the environment division• And in a corresponding FD in the data division

– CLOSE statement• Use when access to a file is no longer necessary• Files should be closed before processing terminates• Usually immediately before the STOP command

Page 2: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

2

– READ statement

• Transfers data from an open file into memory• AT END clause

– If file is at the end control transfer to this clause

READ file-name

AT END statement

END-READ

• Primimg the READ• Correct positioning

Page 3: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

3

– WRITE• Transfers data from memory to an output

device– Printer, Open File

• ADVANCING (AFTER & BEFORE)– For printer usage, printing blank lines– LINES and PAGE

• Contains a Record name– 01 Entry in the File Section of the Data Division

MOVE SPACES TO OUT-LABEL-REC.

MOVE IN-EMPLOYEE-NAME TO WS-EMPLOYEE-NAME.

MOVE WS-NAME-LINE TO OUT-LABEL-REC.

WRITE OUT-LABEL-REC.

Page 4: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

4

• Data manipulation and program statements– DISPLAY statement– STOP RUN statement

• Terminates the program• Should not be more than one in your program• Typically not the last statement

– MOVE statement• Copies data from one location to another• Syntax

– MOVE 15 TO NUM-1 NUM-2 NUM-3.

• Restrictions on use• Rules for use• Group Moves

Page 5: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

5

Table 5.1 Rules of the MOVE Statement (Elementary Data Items)

SENDING FIELD RECEIVING FIELD

Alphabetic

Alphanumeric

Numeric

Numeric Edited

ALPHABETIC ALPHANUMERIC NUMERIC NUMERIC EDITED

Valid

Invalid

Invalid

Invalid

Valid

Valid

Integers Only

Valid

Invalid

Invalid

Valid

Valid

Invalid

Invalid

Valid

Invalid

Page 6: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

6

Table 5.2 Illustration of the MOVE statement: Alphanumeric Sending Field to Alphanumeric Receiving Field

SENDING FIELD RECEIVING FIELD

PICTURE CONTENTS PICTURE CONTENTS

(a) X(5) A B C D E X(5) A B C D E

(b) X(5) A B C D E X(4) A B C D

(c) X(5) A B C D E X(6) A B C D E F

Page 7: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

7

Table 5.3 Illustration of the MOVE statement: Numeric Sending Field to Numeric Receiving Field

SENDING FIELD RECEIVING FIELD

PICTURE CONTENTS PICTURE CONTENTS

(a) 9(5) 1 2 3 4 5 9(5) 1 2 3 4 5

(b) 9(5) 1 2 3 4 5 9(4) 2 3 4 5

(c) 9(5) 1 2 3 4 5 9(6) 0 1 2 3 4 5

(d) 9(3)V99 1 2 3 v 4 5 9(3) 1 2 3

(e) 9(3)V99 1 2 3 v 4 5 9V99 3 v 4 5

(f) 9(3) 1 2 3 9(3)V99 1 2 3 v 0 0

Page 8: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

8

– PERFORM statement• Transfers control to a procedure (paragraph)• Returns to the next line of code after it was

called• Use of UNTIL

– Checks before it transfers control

– IF statement• ELSE clause• END-IF• Use of indentation

Page 9: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

9

CONDITION?

STATEMENT2 STATEMENT1

STATEMENT3

CONDITION?

STATEMENT1

STATEMENT2

Figure 5.2 The IF Statement

(a) With ELSE Option (b) Without ELSE Option

FALSEFALSE

TRUETRUE

Page 10: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

10

Figure 5.3 The ELSE Clause/II

A=B

MOVE ZERO TO C

MOVE 1 TO C

WRITEDETAIL-LINE

MOVE ZERO TO D

MOVE 1 TO D

FALSE TRUE

IF A = B

MOVE 1 TO C

MOVE 1 TO D

ELSE

MOVE ZERO TO C

MOVE ZERO TO D

END-IF.

WRITE DETAIL-LINE.

(a) IF/ELSE Flowchart (b) COBOL Code

Page 11: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

11

– EVALUATE statement

• Similar to a case statement

EVALUATE TRUE

WHEN STU-CREDITS <= 6

MOVE 25 TO IND-ACTIVITY-FEE

WHEN STU-CREDITS > 6 AND STU-CREDITS <=12

MOVE 50 TO IND-ACTIVITY-FEE

WHEN STU-CREDITS > 12

MOVE 75 TO IND-ACTIVITY-FEE

END-EVALUATE

Page 12: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

12

Arithmetic Verbs

• ROUNDED clause– Rounds the answer an arithmetic statement– If no rounded clause is used it truncates– ADD A B GIVING C ROUNDED

• SIZE ERROR clause– Creates an warning when result of a calculation is

to big for the designated field– You can use this to compensate for larger

numbers.– ON SIZE ERROR PERFORM …

Page 13: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

13

Table 5.4 The ROUNDED Clause

DATA NAME A B C

PICTURE 9V99 9V99 9V9Value before execution 123 456 (immaterial)

Value after execution of:

ADD A B GIVING C 123 456 57

ADD A B GIVING C ROUNDED 123 456 58

Page 14: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

14

• The COMPUTE statement

– Combines multiple arithmetic operations– Ex.) COMPUTE A = a * b + c– +,-,*,/,**– Order:

• Parentheses• Exponentiation• Multiplication, division• Addition, subtraction

Page 15: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

15

Table 5.5 The COMPUTE Instruction

DATA NAME A B C COMMENTSValue before execution 2 3 10 Initial Values

Value after execution of:

COMPUTE C = A + B. 2 3 5 Simple addition

COMPUTE C = A + B * 2. 2 3 8 Multiplication before addition

COMPUTE C = (A + B) * 2. 2 3 10 Parenthesis evaluated first

COMPUTE C = A ** B. 2 3 8 algebraically, c = ab

COMPUTE C = B ** A. 2 3 9 algebraically, c = ba

Page 16: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

16

Table 5.6 The COMPUTE Instruction (continued)

ALGEBRAIC EXPRESSION COBOL COMPUTE

x = a + b COMPUTE X = A + B.

x = a + b COMPUTE X = (A + B) / 2. 2

x = (a + b)c COMPUTE X = (A + B) * C / 2. 2

x = a + b COMPUTE X = (A + B) / (2 * C). 2c

x = a COMPUTE X = A ** .5.

x = a + b COMPUTE X = (A ** 2 + B ** 2) / C ** 2 c2

Page 17: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

17

• The ADD statement

• The SUBTRACT statement

• The MULTIPLY statement

• The DIVIDE statement

• Assumed decimal point

• The COMPUTE statement for multiple operations

Page 18: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

18

Table 5.7 The ADD Instruction

DATA NAME A B C

Value before execution 5 10 30

Value after execution of:

ADD A TO C 5 10 35

ADD A B TO C 5 10 45

ADD A TO B GIVING C 5 10 15

ADD A 18 B GIVING C 5 10 33

ADD A 18 B TO C 5 10 63

ADD 1 TO B C 5 11 31

Page 19: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

19

Table 5.8 The SUBTRACT Instruction

DATA NAME A B C D

Value before execution 5 10 30 100

Value after execution of:

SUBTRACT A FROM C 5 10 25 100

SUBTRACT A B FROM C 5 10 15 100

SUBTRACT A B FROM C GIVING D 5 10 30 15

SUBTRACT 10 FROM C D 5 10 20 90

Page 20: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

20

Table 5.9 The MULTIPLY Instruction

DATA NAME A B C

Value before execution 5 10 30

Value after execution of:

MULTIPLY B BY A GIVING C 5 10 50

MULTIPLY A BY B GIVING C 5 10 50

MULTIPLY A BY B 5 50 15

MULTIPLY B BY A 50 10 30

MULTIPLY A BY 3 GIVING B C 5 15 15

Page 21: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

21

Table 5.10 The DIVIDE Instruction

DATA NAME A B C

Value before execution 5 10 30

Value after execution of:

DIVIDE 2 INTO B. 5 5 30

DIVIDE 2 INTO B GIVING C. 5 10 5

DIVIDE B BY 5 GIVING A 2 10 30

DIVIDE A INTO B C 5 2 6

DIVIDE A INTO B GIVING C 5 10 2

DIVIDE 3 INTO A GIVING B REMAINDER C 5 1 2

Page 22: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

22

DATA NAME A B C

PICTURE 99 99V9 99V99Value before execution 12 345 4712

Value after execution of:

ADD B TO A 46 345 4712

ADD A TO B 12 465 4712

ADD B TO C 12 345 8162

ADD C TO B 12 816 4712

ADD C TO A 59 345 4712

ADD A TO C 12 465 5912

Table 5.11 Arithmetic on Fields with Assumed Decimal Points

Page 23: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

23

Use the COMPUTE Statement for Multiple Arithmetic Operations

MULTIPLY B BY B GIVING B-SQUARED.MULTIPLY 4 BY A GIVING FOUR-A.MULTIPLY FOUR-A BY C GIVING FOUR-A-C.SUBTRACT FOUR-A-C FROM B-SQUARED GIVING RESULT-1.COMPUTE RESULT-2 = RESULT-1 ** .5.SUBTRACT B FROM RESULT-2 GIVING NUMERATOR.MULTIPLY 2 BY A GIVING DENOMINATOR.DIVIDE NUMERATOR BY DENOMINATOR GIVING X.

COMPUTE X = (-B + (B ** 2 - ( 4 * A * C)) ** .5) / ( 2 * A).

Both sets of code apply to the quadratic formula

X = -B + B-4AC 2A

Poor Code

Improved Code

PROGRAMMING TIP

Page 24: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

24

Problems

A B C D

4 8 12 2

ADD 1 TO D B

ADD A B C GIVING C

MULTIPLY A BY B C

COMPUTE D=A+B/2*D

COMPUTE D=(A+B)/(2*D)

Page 25: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

25

Problems

X = a + b + c

X = a2 + b2 + c2

F = p(1+I)n

Page 26: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

26

Problems

Sending field Receiving field

X(4) HOPE X3

X(4) HOPE X5

9(4) 6789 9(5)

999V9 6789 9(3)V99

Page 27: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

27

Figure 5.4a Developing the Procedure Division

PREPARETUITIONREPORT

WRITEHEADING

LINE

READSTUDENT

FILE

PROCESSSTUDENTRECORD

WRITEUNIVERSITY

TOTALS

COMPUTETUITION

COMPUTEUNION FEE

COMPUTEACTIVITY

FEE

COMPUTESCHOLARSHIP

COMPUTEINDIVIDUAL

BILL

INCREMENTUNIVERSITY

TOTALS

WRITEDETAIL

LINE

READSTUDENT

FILE

(a) Hierarchy Chart

Page 28: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

28

Open filesWrite heading line(s)Read STUDENT-FILE at end indicate no more dataDO WHILE data remains Compute tuition = 200 * credits IF union member

Union fee = $25 ELSE

Union fee = $0 ENDIF DO CASE

CASE credits <= 6 Activity fee = 25CASE credits > 6 and < 12 Activity fee = 50CASE credits >= 12 Activity fee = 75

END CASE IF gpa > 2.5

Scholarship = Scholarship amount ELSE (no scholarship)

Scholarship = 0 ENDIF Compute Bill = Tuition + Union fee + Activity fee - Scholarship Increment university totals Write detail line Read STUDENT-FILE at end indicate no more dataENDDOWrite university totalsClose filesStop run

(b) Detailed Pseudocode

Figure 5.4b Developing the Procedure Division

Page 29: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

29

SMITH JB15Y0000230JAMES HR15 0500245BAKER SR09 0500350PART-TIMER JR03Y0000300JONES PL15Y0000280HEAVYWORKER HM18 0000200LEE BL18 0000335CLARK JC06 0000310GROSSMAN SE07 0000215FRANKEL LF10 0000350BENWAY CT03 0250395KERBEL NB04 0000100

STUDENT NAME CREDITS TUITION UNION FEE ACT FEE SCHOLARSHIP TOTAL BILL

SMITH JB 15 003000 025 075 00000 003100JAMES HR 15 003000 000 075 00000 003075BAKER SR 09 001800 000 050 00500 001350PART-TIMER JR 03 000600 025 025 00000 000650JONES PL 15 003000 025 075 00000 003100HEAVYWORKER HM 18 003600 000 075 00000 003675LEE BL 18 003600 000 075 00000 003675CLARK JC 06 001200 000 025 00000 001225GROSSMAN SE 07 001400 000 050 00000 001450FRANKEL LF 10 002000 000 050 00000 002050BENWAY CT 03 000600 000 025 00250 000375KERBEL NB 04 000800 000 025 00000 000825 ------- ---- ----- ------ -------

UNIVERSITY TOTALS 024600 0075 0625 000750 024550

(a) Test Data

(b) Output

Figure 5.6 Test Data and Output

Page 30: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

30

Figure 5.7 Skeleton Outline of a COBOL Program

IDENTIFICATION DIVISION.PROGRAM-ID. PROGNAME.AUTHOR. JOHN DOE.

ENVIRONMENT DIVISION.INPUT-OUTPUT SECTION.FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO ‘A:\CHAPTR05\TUITION.DAT’

ORGANIZATION IS LINE SEQUENTIAL. SELECT PRINT-FILE

ASSIGN TO PRINTER.

DATA DIVISION.FILE SECTION.FD INPUT-FILE RECORD CONTAINS 80 CHARACTERS.01 INPUT-RECORD PIC X(80).

FD PRINT-FILE RECORD CONTAINS 132 CHARACTERS.01 PRINT-LINE PIC X(132).

WORKING-STORAGE SECTION.01 DATA-REMAINS-SWITCH PIC X(2) VALUE SPACES.01 HEADING-LINE.. .01 DETAIL-LINE.. .01 TOTAL-LINE.. .

SELECT statements for input and output files

Controls performed paragraph

Page 31: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

31

Figure 5.7 Skeleton Outline of a COBOL Program(cont.)

PROCEDURE DIVISION.MAINLINE.

OPEN INPUT INPUT-FILE OUTPUT PRINT-FILE. READ INPUT-FILE AT END MOVE ‘NO’ TO DATA-REMAINS-SWITCH END-READ. PERFORM PROCESS-RECORDS UNTIL DATA-REMAINS-SWITCH = ‘NO’. CLOSE INPUT-FILE PRINT-FILE. STOP RUN.

PROCESS-RECORDS.. .

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

Housekeeping consists of opening files and the initial READ

Termination includes closing files and STOP RUN.

Last line of performed paragraph is a second READ

Page 32: 1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been

32

Tuition Billing Program