Vous êtes sur la page 1sur 38

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
Structured COBOL Programming, Stern & Stern, 9th Edition

9th Edition

CHAPTER 9 Iteration: Beyond the Basic PERFORM

Structured COBOL Programming, Stern & Stern, 9th Edition

OBJECTIVES
To familiarize you with:

1. The simple PERFORM.


2. How PERFORM statements are used for iteration. 3. The various options available with the PERFORM statement.

Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
The Simple PERFORM Reviewed
The Basic Formats Modularizing Programs Using PERFORM Statements Nested PERFORM: A PERFORM within a PERFORM Executing a Group of Paragraphs with a Simple PERFORM The Use and Misuse of GO TO Statements The EXIT Statement
Structured COBOL Programming, Stern & Stern, 9th Edition

CONTENTS
Iteration Using Other Types of PERFORMs
PERFORM UNTIL(A Review)

Coding a Loop with a PERFORM


PERFORM...TIMES Examples of Loops

PERFORM VARYING

Using Nested PERFORM VARYING Statements The PERFORM WITH TEST AFTER Option (COBOL 85)
Structured COBOL Programming, Stern & Stern, 9th Edition

The Simple PERFORM Reviewed

Structured COBOL Programming, Stern & Stern, 9th Edition

The Basic Formats


There are two formats of the basic PERFORM: 1. In-Line PERFORM
PERFORM Statements to be executed

END-PERFORM

Structured COBOL Programming, Stern & Stern, 9th Edition

The Basic Formats


An in-line PERFORM begins with the word PERFORM, is followed by statements to be executed, and ends with an ENDPERFORM scope terminator.

All instructions within the PERFORM ... END-PERFORM are executed in sequence.
Periods are optional after any scope terminator except at the end of a paragraph, when they are required.
Structured COBOL Programming, Stern & Stern, 9th Edition

The Basic Formats


2. PERFORM Paragraph-Name

PERFORM [paragraph-name-1]
The PERFORM paragraph-name statement will:
1. Execute all instructions in the named paragraph.

2. Transfer control to the next instruction in sequence, after the PERFORM paragraphname.
Structured COBOL Programming, Stern & Stern, 9th Edition

The Basic Formats Pseudocode


Pseudocode for In-line PERFORM

PERFORM
. Statements

.
.

to be performed
go here

END-PERFORM
Structured COBOL Programming, Stern & Stern, 9th Edition

The Basic Formats Pseudocode


Pseudocode for a PERFORM paragraphname PERFORM Paragraph
. .

PARAGRAPH
. . {Statements to be performed go here

Structured COBOL Programming, Stern & Stern, 9th Edition

Modularizing Programs Using PERFORM Statements


Version 1: Nonmodular Approach IF AMT1-IN < AMT2-IN ADD AMT1-IN TO TOTAL-1 ADD AMT2-IN TO TOTAL-2 ADD 1 TO OK-REC-CTR ELSE ADD AMT2-IN TO TOTAL-3 ADD 1 TO ERR-REC-CTR END-IF.
Structured COBOL Programming, Stern & Stern, 9th Edition

Modularizing Programs Using PERFORM Statements


Version 2: Modular Approach IF AMT1-IN < AMT2-IN PERFORM 300-OK-RTN ELSE PERFORM 400-ERR-RTN END-IF. 300-OK-RTN. ADD AMT1-IN TO TOTAL-1 ADD AMT2-IN TO TOTAL-2 ADD 1 TO OK-REC-CTR. 400-ERR-RTN. ADD AMT2-IN TO TOTAL-3 ADD 1 TO ERR-REC-CTR.
Structured COBOL Programming, Stern & Stern, 9th Edition

Executing a Group of Paragraphs with a Simple PERFORM


Expanded format for the PERFORM paragraph-name statement:
PERFORM paragraph-name-1 {THROUGH} {THRU} paragraphname-2

Structured COBOL Programming, Stern & Stern, 9th Edition

Executing a Group of Paragraphs with a Simple PERFORM


The PERFORM paragraph-name executes all statements beginning at paragraphname-1 until the end of paragraph-name2 is reached.
Control is then transferred to the statement directly following the PERFORM.

Structured COBOL Programming, Stern & Stern, 9th Edition

The Use and Misuse of GO TO Statements


Format:

GO TO paragraph-name-1
Unlike the PERFORM, which returns control to the statement following the PERFORM, the GO TO permanently transfers control to another paragraph. However, in well-designed, structured programs it is best to avoid the use of GO TO as much as possible.
Structured COBOL Programming, Stern & Stern, 9th Edition

The EXIT Statement


EXIT is a COBOL reserved word that performs no operation. It is used to allow execution to pass over other statements or to transfer control back to the statement following the original PERFORM. It is used, when necessary, as an end point in a paragraph being performed.
Structured COBOL Programming, Stern & Stern, 9th Edition

ITERATION USING OTHER TYPES OF PERFORMs

Structured COBOL Programming, Stern & Stern, 9th Edition

PERFORM UNTIL . . . A Review


The format of a PERFORM UNTIL ... is:

Format 2
PERFORM [paragraph-name-1] [{THROUGH} {THRU} paragraphname-2] UNTIL condition-1

Structured COBOL Programming, Stern & Stern, 9th Edition

PERFORM UNTIL . . . A Review


With COBOL 85, if an in-line PERFORM is used, no paragraph-name is coded.
The statements within the PERFORM end with an END-PERFORM scope terminator.

Any simple or compound condition can be specified in a PERFORM UNTIL ... As with a simple PERFORM, COBOL 85 permits an in-line PERFORM UNTIL
this format also uses END-PERFORM as a scope terminator.
Structured COBOL Programming, Stern & Stern, 9th Edition

Coding a Loop with a PERFORM


The loop should:

1. Be preceded by an instruction that initializes the field to be tested (e.g., MOVE 0 TO COUNTER1).
2. Include a PERFORM UNTIL ... that is executed repeatedly UNTIL the field to be tested reaches the desired value
(e.g., PERFORM UNTIL COUNTER1 = 5 or PERFORM UNTIL COUNTER1 >= 5).
Structured COBOL Programming, Stern & Stern, 9th Edition

Coding a Loop with a PERFORM


3. Include, as one of the instructions within the PERFORM UNTIL, a statement that increases (or decreases) the value of the field being tested (e.g., ADD 1 TO COUNTER1).
The contents of the counter is used to control the number of times that the loop is performed.

Structured COBOL Programming, Stern & Stern, 9th Edition

PERFORM TIMES
Format

PERFORM paragraph-name-1 [{THROUGH} {THRU} paragraphname-2] {integer-} {identifier-1} TIMES

Structured COBOL Programming, Stern & Stern, 9th Edition

PERFORM TIMES
When using the TIMES format --

PERFORM paragraph-name-1 identifier-1 TIMES:


(1) the identifier must be specified in the DATA DIVISION; (2) it must have a numeric PICTURE clause; and (3) it must contain only integers or zeros.
Structured COBOL Programming, Stern & Stern, 9th Edition

PERFORM TIMES
The THRU clause is optional with any PERFORM paragraph-name statement. When using the integer option with the PERFORM TIMES format, only the actual number is acceptable.

Structured COBOL Programming, Stern & Stern, 9th Edition

DEBUGGING TIP
Use PERFORM TIMES rather than PERFORM UNTIL if you know in advance the specific number of times a paragraph is to be executed. If the number of times a paragraph is to be executed is variable use a PERFORM UNTIL. It is also best to use a PERFORM UNTIL if the number of times a paragraph is being executed needs to be used for output.
Structured COBOL Programming, Stern & Stern, 9th Edition

PERFORM VARYING
The last format for a PERFORM statement is the most comprehensive:

PERFORM paragraph-name-1 [{THROUGH} {THRU} paragraphname-2] VARYING identifier-1 FROM {identifier-2}{integer-1} BY {identifier-3} {integer-2} UNTIL condition-1
Structured COBOL Programming, Stern & Stern, 9th Edition

USING NESTED PERFORM VARYING Statements


Rules for using a Nested PERFORM VARYING 1. The innermost PERFORM VARYING loop is executed first. 2. The next outer PERFORM VARYING loop in sequence is then executed.

Structured COBOL Programming, Stern & Stern, 9th Edition

THE PERFORM WITH TEST AFTER OPTION (COBOL 85)


Format

PERFORM paragraph-name-1
[WITH TEST {BEFORE} {AFTER}]

UNTIL condition-1
With COBOL 85, if an in-line PERFORM is used, no paragraph-name is coded,
Statements within the PERFORM end with an END-PERFORM scope terminator.
Structured COBOL Programming, Stern & Stern, 9th Edition

THE PERFORM WITH TEST AFTER OPTION (COBOL 85)


The WITH TEST AFTER clause can also be used with the VARYING option of the PERFORM.
The format is: PERFORM WITH TEST AFTER VARYING ....

Note that PERFORM UNTIL (with no WITH TEST clause) and PERFORM WITH TEST BEFORE UNTIL do the same thing.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SLIDES END HERE


CHAPTER SUMMARY COMES NEXT

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
A. Formats of the PERFORM Statement

1. Simple PERFORM statements:


a. In-line PERFORM: PERFORM ... END-PERFORM b. PERFORM paragraph-name-1 [THRU paragraph-name-2]
Cause execution of the instructions at the named paragraph(s).
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
After execution of the instructions within either the PERFORM ... END-PERFORM or in the PERFORM paragraph-name, control is transferred to the statement directly following the PERFORM.

2. The PERFORM UNTIL statement


a. The identifier(s) used in the UNTIL clause must be altered within the paragraph(s) being performed; otherwise, the paragraphs will be performed indefinitely.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
b. If the condition in the UNTIL clause is met at the time of execution, then the named paragraph(s) will not be executed at all. With COBOL 85, the WITH TEST AFTER clause can be used to test the condition after the paragraph has been executed once.

3. The PERFORM TIMES statement


A numeric identifier or an integer can precede the word TIMES; with COBOL 85, an arithmetic expression can be used as well.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
4. The PERFORM VARYING statement
a. The counter or loop control field must be defined in the DATA DIVISION, typically in WORKING-STORAGE. An initial VALUE for the loop control field is not required.

b. The PERFORM VARYING automatically does the following:


(1) Initializes the counter with the value specified in the FROM clause. (2) Tests the counter for the condition specified in the UNTIL clause.
Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
(3) Continues with the statement directly following the PERFORM if the condition specified in the UNTIL clause is satisfied. (4) Executes the named paragraph(s) if the condition specified in the UNTIL clause is not met. (5) After execution of the named paragraph(s), increases (or decreases) the counter by the value of the integer or identifier specified in the VARYING clause.

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
B. Additional Considerations

1. The THRU option can be included with all versions of the PERFORM but we recommend that you avoid this option.
2. PERFORM statements within PERFORM statements are permissible. These are called nested PERFORMs.

Structured COBOL Programming, Stern & Stern, 9th Edition

CHAPTER SUMMARY
3. EXIT is a reserved word that can be used to indicate the end point of paragraph(s) being performed.
EXIT must be the only entry in a paragraph when it is used with COBOL 74.

4. In-line PERFORMs are permitted in COBOL 85 with all PERFORM options; with in-line PERFORMs it is not necessary to code separate paragraphs.
The PERFORM is terminated with an ENDPERFORM.
Structured COBOL Programming, Stern & Stern, 9th Edition

Vous aimerez peut-être aussi