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
32
Embed
1 Chapter 5 – The Procedure Division File handling statements –OPEN statement Initiates processing for a file Input Output Each file opened must have been.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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
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.
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
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
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
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
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
9
CONDITION?
STATEMENT2 STATEMENT1
STATEMENT3
CONDITION?
STATEMENT1
STATEMENT2
Figure 5.2 The IF Statement
(a) With ELSE Option (b) Without ELSE Option
FALSEFALSE
TRUETRUE
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
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
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 …
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
14
• The COMPUTE statement
– Combines multiple arithmetic operations– Ex.) COMPUTE A = a * b + c– +,-,*,/,**– Order:
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
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
17
• The ADD statement
• The SUBTRACT statement
• The MULTIPLY statement
• The DIVIDE statement
• Assumed decimal point
• The COMPUTE statement for multiple operations
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
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
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
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
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
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.
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
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