Beruflich Dokumente
Kultur Dokumente
9th Edition
OBJECTIVES
To familiarize you with the methods used to:
1. Access input and output files.
OBJECTIVES
4. Write information onto an output file. 5. Accomplish end-of-job operations. 6. Execute paragraphs from a main module and then return control to that main module.
CONTENTS
A REVIEW OF THE FIRST THREE DIVISIONS
CONTENTS
STATEMENTS TYPICALLY CODED IN THE MAIN MODULE OF BATCH PROGRAMS:
OPEN Statement
CONTENTS
READ Statement More on PERFORM Statements End-of-Job Processing:
The CLOSE and STOP RUN Statements
CONTENTS
STATEMENTS TYPICALLY CODED FOR PROCESSING INPUT RECORDS AND PRODUCING OUTPUT RECORDS
Simplified MOVE Statement WRITE Statement
The FILE SECTION of the DATA DIVISION defines the input and output records.
The PROCEDURE DIVISION is divided into paragraphs. Each paragraph is an independent module or routine that includes a series of instructions designed to perform a specific set of operations.
Rules for forming paragraph-names are the same as rules for forming data-names except that a paragraph-name may have all digits.
Paragraph-names must be unique, meaning that two paragraphs may not have the same name. Similarly, a data-name cannot also serve as a paragraph-name.
Structured COBOL Programming, Stern & Stern, 9th Edition
A paragraph with a prefix of 200- is located after paragraph 100--XXX and before paragraph 300-- YYY.
As noted, all COBOL statements are coded in Area B whereas paragraph-names are coded in Area A.
Statements that end with a period are called sentences.
Structured COBOL Programming, Stern & Stern, 9th Edition
Although statements can be written across the coding sheet in paragraph form, we recommend that each statement be coded on an individual line.
This makes programs much easier to read and debug.
Structured COBOL Programming, Stern & Stern, 9th Edition
Endeavor to code the more general paragraphs first and end with the most detailed ones.
Structured COBOL Programming, Stern & Stern, 9th Edition
OPEN Statement
The OPEN statement accesses the files in a program and indicates which are input and which are output. It has the following instruction format: Format
OPEN INPUT file-name-1 . . .
OUTPUT file-name-2 . . .
OPEN STATEMENT
A REVIEW OF INSTRUCTION FORMAT SPECIFICATIONS
1. Uppercase words are COBOL reserved words. 2. Underlined words are required in the statement or option specified.
OPEN STATEMENT
A REVIEW OF INSTRUCTION FORMAT SPECIFICATIONS
5. Brackets [ ] denote that the enclosed item is optional. 6. Punctuation, when included in the format, is required.
7. The use of three dots or ellipses (. . .) indicates that additional entries of the same type (a file-name in this case) may be repeated if desired.
OPEN STATEMENT
FUNCTIONS OF THE OPEN STATEMENT
OPEN STATEMENT
DEBUGGING TIPS: CODING GUIDELINES
For the OPEN sentence, we typically indent so that the words INPUT and OUTPUT are aligned. For other entries we indent four spaces.
Structured COBOL Programming, Stern & Stern, 9th Edition
OPEN STATEMENT
DEBUGGING TIPS: CODING GUIDELINES
AN EXAMPLE:
OPEN INPUT
OLD-MASTER-IN
TRANS-FILE
OUTPUT NEW-MASTER-OUT
ERROR-LIST.
Structured COBOL Programming, Stern & Stern, 9th Edition
OPEN STATEMENT
DEBUGGING TIPS: CODING GUIDELINES
For output disk files, the ASSIGN clause of a SELECT statement often specifies the name the file is to be saved as:
SELECT SALES-FILE ASSIGN TO DISK DATA100.
PERFORM
UNTIL condition-1
. . .
READ Statement
Typically, after an input file has been opened, the PERFORM ... END-PERFORM loop, which begins with a READ, is executed.
A READ statement transmits data from the input device, assigned in the ENVIRONMENT DIVISION, to the input storage area, defined in the FILE SECTION of the DATA DIVISION.
Structured COBOL Programming, Stern & Stern, 9th Edition
READ Statement
The following is a partial instruction format for a READ statement: Format
READ file-name-1
AT END statement-1 . . . [NOT AT END statement-2 . . .] [END-READ]* COBOL 85 ONLY
Structured COBOL Programming, Stern & Stern, 9th Edition
READ Statement
The file-name specified in the READ statement appears in three previous places in the program:
1. The SELECT statement, indicating the filename and the device or implementor-name assigned to the file.
If a file is stored on a disk, for example, a READ operation transmits data from the disk to the input area.
READ Statement
3. The OPEN statement, accessing the file and activating the device.
The primary function of the READ statement is to transmit one data record to the input area reserved for that file.
Each time a READ statement is executed, one record is read into primary storage - not the entire file.
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP
Code the AT END and NOT AT END clause on separate lines and indent them for readability.
If an error occurs, you will be able to more easily identify the problem because the line number of the error is specified.
QUESTIONS?
SELF-TEST
1. The PROCEDURE DIVISION is divided into ______ each of which contains ______ .
SELF-TEST
2. Statements are executed in the order ______ unless a ______ occurs.
SELF-TEST
3. Before a file may be read, it must be ________ .
Solution: opened
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
4. The PERFORM UNTIL (condition) ... END- PERFORM executes _____ . When the condition specified is met, control returns to the _______ .
Solution: all the instructions within the PERFORM ... END-PERFORM loop; statement directly following the PERFORM ... END-PERFORM loop
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
5. In the statement PERFORM . . . UNTIL EOF = 1, EOF should be initialized at _____ .
Write the required WORKING-STORAGE entries for defining and initializing EOF.
Solution: 0 --- Actually any other value but 1: WORKING-STORAGE SECTION. 01 WS-STORED-AREAS. 05 EOF PIC 9 VALUE 0.
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
6. In a COBOL 85 PERFORM UNTIL . . . END-PERFORM in-line main processing loop, the first instruction within the loop is typically a _____ statement.
Solution: READ
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
7. The basic format for a READ within a PERFORM UNTIL loop for COBOL 85 is _____ .
SELF-TEST
8. The NOT AT END clause of a READ statement in COBOL 85 is executed when _____ .
SELF-TEST
9. Typically the NOT AT END clause in COBOL 85 includes a _____ statement.
Solution: PERFORM
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
10. In COBOL 85, END-PERFORM and END-READ are called _____ because they terminate the range of the PERFORM and READ statements, respectively.
STATEMENTS TYPICALLY CODED FOR PROCESSING INPUT RECORDS AND PRODUCING OUTPUT RECORDS
WRITE Statement
The WRITE instruction takes data in the output area defined in the DATA DIVISION and transmits it to the device specified in the ENVIRONMENT DIVISION.
WRITE record-name-1
Structured COBOL Programming, Stern & Stern, 9th Edition
WRITE Statement
Format
WRITE record-name-1
With each WRITE instruction, we tell the computer to write data that is in the output area.
Structured COBOL Programming, Stern & Stern, 9th Edition
The four basic arithmetic verbs have the following simple formats::
ADD {identifier-1} {literal-1} TO identifier-2 SUBTRACT {identifier-1} {literal-1} FROM identifier-2
[END-IF]
*Note that the ELSE clause is optional. Numerous statements can follow each IF or ELSE clause.
Structured COBOL Programming, Stern & Stern, 9th Edition
identifier-2
literal-1
COMMENTS IN COBOL An asterisk (*) in column 7 (the continuation position) of any line makes the entire line a comment Use comments freely to make your program user-friendly and easier to understand.
Problems associated with dates after this are referred to as the Year 2000 Problem or Y2K Problem.
In addition to program source changes, all files on which they operate will also need to be modified.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
Most programs illustrated or assigned as homework in this text will use the following structure (lowercase entries are user-defined names):
PROCEDURE DIVISION. paragraph-name-1. OPEN INPUT file-name-1 OUTPUT file-name-2 PERFORM UNTIL ARE-THERE-MORERECORDS = 'NO
READ file-name-1 AT END MOVE 'NO ' TO ARETHERE-MORE-RECORDS NOT AT END PERFORM paragraph-name-2 END-READ END-PERFORM CLOSE file-name-1 file-name-2 STOP RUN.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
A. Paragraph-names are coded in Area A and end with a period.
Rules for forming paragraph-names are the same as for data-names except that a paragraph-name can have all digits. We use a prefix such as 100-, 200- , 300-, along with a descriptive name such as HEADING- RTN or MAIN-MODULE. A paragraph with a prefix of 200- is located after a paragraph with prefix 100and before a paragraph with prefix 300-.
Structured COBOL Programming, Stern & Stern, 9th Edition
C. Instructions are executed in the order in which they appear unless a PERFORM statement executes a loop or transfers control.