320 likes | 421 Views
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
E N D
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
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
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.
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
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 Table 5.1 Rules of the MOVE Statement (Elementary Data Items)
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 Table 5.2 Illustration of the MOVE statement: Alphanumeric Sending Field to Alphanumeric 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 Table 5.3 Illustration of the MOVE statement: Numeric Sending Field to Numeric Receiving Field
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
CONDITION? CONDITION? STATEMENT2 STATEMENT1 STATEMENT1 STATEMENT3 STATEMENT2 Figure 5.2 The IF Statement FALSE TRUE FALSE TRUE (a) With ELSE Option (b) Without ELSE Option
A=B MOVE ZERO TO C MOVE 1 TO C MOVE ZERO TO D MOVE 1 TO D WRITEDETAIL-LINE Figure 5.3 The ELSE Clause/II 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
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
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 …
Table 5.4 The ROUNDED Clause DATA NAME A B C PICTURE 9V99 9V99 9V9 Value 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
The COMPUTE statement • Combines multiple arithmetic operations • Ex.) COMPUTE A = a * b + c • +,-,*,/,** • Order: • Parentheses • Exponentiation • Multiplication, division • Addition, subtraction
Table 5.5 The COMPUTE Instruction DATA NAME A B C COMMENTS Value 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
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
The ADD statement The SUBTRACT statement The MULTIPLY statement The DIVIDE statement Assumed decimal point The COMPUTE statement for multiple operations
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
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
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
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
Table 5.11 Arithmetic on Fields with Assumed Decimal Points DATA NAME A B C PICTURE 99 99V9 99V99 Value 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
Poor Code 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 Improved Code PROGRAMMING TIP Use the COMPUTE Statement for Multiple Arithmetic Operations
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)
Problems X = a + b + c X = a2 + b2 + c2 F = p(1+I)n
Problems Sending fieldReceiving field X(4) HOPE X3 X(4) HOPE X5 9(4) 6789 9(5) 999V9 6789 9(3)V99
COMPUTEINDIVIDUALBILL INCREMENTUNIVERSITYTOTALS WRITEDETAILLINE READSTUDENTFILE COMPUTETUITION COMPUTEUNION FEE COMPUTEACTIVITYFEE COMPUTESCHOLARSHIP Figure 5.4a Developing the Procedure Division PREPARETUITIONREPORT WRITEHEADINGLINE READSTUDENTFILE PROCESSSTUDENTRECORD WRITEUNIVERSITYTOTALS (a) Hierarchy Chart
Figure 5.4b Developing the Procedure Division 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 = 25 CASE credits > 6 and < 12 Activity fee = 50 CASE 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.6 Test Data and Output SMITH JB15Y0000230JAMES HR15 0500245BAKER SR09 0500350PART-TIMER JR03Y0000300JONES PL15Y0000280HEAVYWORKER HM18 0000200LEE BL18 0000335CLARK JC06 0000310GROSSMAN SE07 0000215FRANKEL LF10 0000350BENWAY CT03 0250395KERBEL NB04 0000100 (a) Test Data 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 (b) Output
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.. . Figure 5.7 Skeleton Outline of a COBOL Program SELECT statements for input and output files Controls performed paragraph
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. Figure 5.7 Skeleton Outline of a COBOL Program(cont.) 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