Sie sind auf Seite 1von 67

Structured COBOL Programming

Nancy Stern Hofstra University Robert A. Stern


Copyright @ 2000 John Wiley & Sons, In. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express permission of the copyright owner is unlawful. Request for further information should be addressed to the permissions Department , John Wily & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.

Nassau Community College


PowerPoint Presentation: Richard H. Baum, Ph.D.
DeVry Institute of Technology

9th Edition

CHAPTER 4 Coding Complete COBOL Programs


A Closer Look at the PROCEDURE DIVISION and the PERFORM Statement

OBJECTIVES
To familiarize you with the methods used to:
1. Access input and output files.

2. Read data from an input file.


3. Perform simple move operations.
Structured COBOL Programming, Stern & Stern, 9th Edition

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.

Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
A REVIEW OF THE FIRST THREE DIVISIONS

THE FORMAT OF THE PROCEDURE DIVISION


Paragraphs that Serve as Modules Statements within Paragraphs The Sequence of Instructions in a Program The Top-Down Approach for Coding Paragraphs
Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
STATEMENTS TYPICALLY CODED IN THE MAIN MODULE OF BATCH PROGRAMS:
OPEN Statement

PERFORM UNTIL ... END-PERFORM Statement:


A Structured Programming Technique

Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
READ Statement More on PERFORM Statements End-of-Job Processing:
The CLOSE and STOP RUN Statements

Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
STATEMENTS TYPICALLY CODED FOR PROCESSING INPUT RECORDS AND PRODUCING OUTPUT RECORDS
Simplified MOVE Statement WRITE Statement

Looking Ahead Review of Comments in COBOL Year 2000-Compliant Date Fields


Structured COBOL Programming, Stern & Stern, 9th Edition

A REVIEW OF THE FIRST THREE DIVISIONS


The IDENTIFICATION and ENVIRONMENT DIVISIONs supply information about the nature of the program and the specific equipment and files.

The FILE SECTION of the DATA DIVISION defines the input and output records.

Structured COBOL Programming, Stern & Stern, 9th Edition

A REVIEW OF THE FIRST THREE DIVISIONS


The WORKING-STORAGE SECTION of the DATA DIVISION is used for defining any areas not part of input and output files. The instructions in the PROCEDURE DIVISION read and process the data and produce the output information.

Structured COBOL Programming, Stern & Stern, 9th Edition

THE FORMAT OF THE PROCEDURE DIVISION


PARAGRAPHS THAT SERVE AS MODULES

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.

Structured COBOL Programming, Stern & Stern, 9th Edition

THE FORMAT OF THE PROCEDURE DIVISION


Paragraph-names, like the PROCEDURE DIVISION entry itself, are coded in Area A.
All other entries in the PROCEDURE DIVISION are coded in Area B.

Paragraph-names, like the PROCEDURE DIVISION entry, end with a period.

Structured COBOL Programming, Stern & Stern, 9th Edition

THE FORMAT OF THE PROCEDURE DIVISION


PARAGRAPHS THAT SERVE AS MODULES

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

Paragraphs That Serve as Modules


We will use descriptive paragraph-names along with a numeric prefix such as 200PROCESS-RTN to identify the type of paragraph.

A paragraph with a prefix of 200- is located after paragraph 100--XXX and before paragraph 300-- YYY.

Structured COBOL Programming, Stern & Stern, 9th Edition

Statements within Paragraphs


Each paragraph in a COBOL program consists of statements, where a statement begins with a verb such as READ, MOVE, or WRITE, or a condition such as IF A = B ....

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

Statements within Paragraphs


With COBOL 85 only the last statement in a paragraph ends with a period.
With COBOL 74, each statement typically ends with a period.

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

THE SEQUENCE OF INSTRUCTIONS IN A PROGRAM


Instructions are typically executed in sequence unless a PERFORM statement is encountered. A PERFORM UNTIL ... END-PERFORM is a loop that repeatedly executes the included statements until the condition specified in the UNTIL clause is met. A PERFORM paragraph-name is an instruction that temporarily transfers control to another paragraph.
Structured COBOL Programming, Stern & Stern, 9th Edition

The Top-Down Approach for Coding Paragraphs


Well-designed programs are written using a top-down approach. This means that the main module is coded first
subsequent modules are coded from the major level to the detail level.

Endeavor to code the more general paragraphs first and end with the most detailed ones.
Structured COBOL Programming, Stern & Stern, 9th Edition

STATEMENTS TYPICALLY CODED IN THE MAIN MODULE OF BATCH PROGRAMS

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

Structured COBOL Programming, Stern & Stern, 9th Edition

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.

3. Lowercase entries are user-defined words.


4. Braces { } denote that one of the enclosed items is required.

Structured COBOL Programming, Stern & Stern, 9th Edition

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.

Structured COBOL Programming, Stern & Stern, 9th Edition

OPEN STATEMENT
FUNCTIONS OF THE OPEN STATEMENT

1. Indicates which files will be input and which will be output.


2. Makes the files available for processing. 3. Performs header label routines if label records are STANDARD.

Structured COBOL Programming, Stern & Stern, 9th Edition

OPEN STATEMENT
DEBUGGING TIPS: CODING GUIDELINES

2. Indent each line within an OPEN sentence.


This makes a program more readable.

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.

Structured COBOL Programming, Stern & Stern, 9th Edition

PERFORM UNTIL . . . END-PERFORM Statement: A Structured Programming Technique


The basic instruction format of the PERFORM UNTIL ... END- PERFORM statement is as follows:

PERFORM
UNTIL condition-1
. . .

[END-PERFORM]* COBOL 85 only


Structured COBOL Programming, Stern & Stern, 9th Edition

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.

2. The FD entry, describing the file and its format.


Structured COBOL Programming, Stern & Stern, 9th Edition

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.

Structured COBOL Programming, Stern & Stern, 9th Edition

QUESTIONS?

Structured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
1. The PROCEDURE DIVISION is divided into ______ each of which contains ______ .

Solution: modules, routines or paragraphs; sentences or instructions or statements


Structured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
2. Statements are executed in the order ______ unless a ______ occurs.

Solution: in which they appear; PERFORM


Structured COBOL Programming, Stern & Stern, 9th Edition

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

Solution: READ . . . AT END . . . NOT AT END . . . END-READ.


Structured COBOL Programming, Stern & Stern, 9th Edition

SELF-TEST
8. The NOT AT END clause of a READ statement in COBOL 85 is executed when _____ .

Solution: a record has been successfully read


Structured COBOL Programming, Stern & Stern, 9th Edition

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.

Solution: scope terminators


Structured COBOL Programming, Stern & Stern, 9th Edition

End-of-Job Processing: The CLOSE and STOP RUN Statements

The CLOSE Statement


Files must be accessed or activated by an OPEN statement before data may be read or written.
Similarly, a CLOSE statement is coded at the end of the job after all records have been processed to release these files and deactivate the devices.
Structured COBOL Programming, Stern & Stern, 9th Edition

End-of-Job Processing: The CLOSE and STOP RUN Statements

The format of the CLOSE is:


CLOSE file-name-1 . . .

Structured COBOL Programming, Stern & Stern, 9th Edition

End-of-Job Processing: The CLOSE and STOP RUN Statements

The STOP RUN Statement


The STOP RUN instruction tells the computer to terminate the program.
All programs should include a STOP RUN statement to end the run. The STOP RUN is usually the last instruction in the main module.
Structured COBOL Programming, Stern & Stern, 9th Edition

STATEMENTS TYPICALLY CODED FOR PROCESSING INPUT RECORDS AND PRODUCING OUTPUT RECORDS

Structured COBOL Programming, Stern & Stern, 9th Edition

Simplified MOVE Statement


A simple MOVE statement has the following basic instruction format: MOVE identifier-1 TO identifier-2 Fields in main memory may be moved to other fields with the use of the MOVE instruction.

The word ``identifier'' means ``data-name''.


Structured COBOL Programming, Stern & Stern, 9th Edition

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.

A simple WRITE statement has the following format:

WRITE record-name-1
Structured COBOL Programming, Stern & Stern, 9th Edition

WRITE Statement
Format
WRITE record-name-1

Note that although files are read, we write records.


The record-name appears on the 01 level and is generally subdivided into fields. The record description specifies the format of the output.

With each WRITE instruction, we tell the computer to write data that is in the output area.
Structured COBOL Programming, Stern & Stern, 9th Edition

LOOKING AHEAD: AN INTRODUCTION TO ARITHMETIC and CONDITIONAL verbs

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

MULTIPLY {identifier-1} {literal-1} BY identifier-2 DIVIDE {identifier-1} {literal-1} INTO identifier-2


Structured COBOL Programming, Stern & Stern, 9th Edition

The basic instruction format for a conditional IF is as follows: Format IF condition-1


[THEN] imperative-statement-1. . . [ELSE imperative-statement-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

The simple conditions that can be tested are as follows: (identifier-1)

= (or IS EQUAL TO)


< (or IS LESS THAN)

identifier-2
literal-1

> (or IS GREATER THAN)

Structured COBOL Programming, Stern & Stern, 9th Edition

REVIEW OF COMMENTS IN COBOL

Structured COBOL Programming, Stern & Stern, 9th Edition

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.

Structured COBOL Programming, Stern & Stern, 9th Edition

Coding Guidelines for PROCEDURE DIVISION Entries


1. Each clause should be on a separate line indented for readability. For example:

READ ... AT END ... NOT AT END ... END-READ.


Structured COBOL Programming, Stern & Stern, 9th Edition

Coding Guidelines for PROCEDURE DIVISION Entries


2. Each paragraph-name should begin with a sequence number that helps to pinpoint the location of the paragraph:
a descriptive name should follow this number (e.g., 100-MAIN-MODULE, 200-CALC-RTN).

3. The last statement in a paragraph should always end with a period.

Structured COBOL Programming, Stern & Stern, 9th Edition

COBOL 2000+ CHANGES


You will be able to code comments on a line, even those with instructions:
*> will be used to add a comment to a line. After *> appears, characters to the end of the line will not be compiled.

Structured COBOL Programming, Stern & Stern, 9th Edition

YEAR 2000-COMPLIANT DATE FIELDS


Older, legacy programs, using only twodigit for the year, will no longer accurately depict the date beginning in the year 2000.

Problems associated with dates after this are referred to as the Year 2000 Problem or Y2K Problem.

Structured COBOL Programming, Stern & Stern, 9th Edition

YEAR 2000-COMPLIANT DATE FIELDS


Fixes for the Y2K problem are not all that difficult:
They are just costly and timeconsuming.

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 SLIDES END HERE


CHAPTER SUMMARY COMES NEXT

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

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY COBOL 85

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

CHAPTER SUMMARY B. All statements are coded in Area B.


We recommend coding one statement per line.

C. Instructions are executed in the order in which they appear unless a PERFORM statement executes a loop or transfers control.

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY D. When the main module's

PERFORM UNTIL ... END-PERFORM


is encountered, the loop specified is executed repeatedly until there are no more input records.

Structured COBOL Programming, Stern & Stern, 9th Edition

Das könnte Ihnen auch gefallen