ch09_97
TRANSCRIPT
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 1/38
Structured COBOL Programming, Stern & Stern, 9th Edition
PowerPoint Presentation:
Richard H. Baum, Ph.D.DeVry Institute of Technology
9th Edition
Structured COBOLProgramming
Nancy Stern
Hofstra University
Robert A. Stern
Nassau Community
College
“Copyright @ 2000 John Wiley & Sons, In. All rightsreserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976
United States Copyright Act without the expresspermission of the copyright owner is unlawful.Request for further information should be addressedto the permissions Department , John Wily & Sons,Inc. The purchaser may make back-up copies forhis/her own use only and not for distribution orresale. The Publisher assumes no responsibility forerrors, omissions, or damages, caused by the use of these programs or from the use of the informationcontained herein.”
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 2/38
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER 9Iteration: Beyond theBasic PERFORM
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 3/38
Structured COBOL Programming, Stern & Stern, 9th Edition
OBJECTIVES
• To familiarize you with:
1. The simple PERFORM.
2. How PERFORM statements are usedfor iteration.
3. The various options available with thePERFORM statement.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 4/38
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 aSimple PERFORM
– The Use and Misuse of GO TO Statements
– The EXIT Statement
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 5/38
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 VARYINGStatements
• The PERFORM WITH TEST AFTER Option
(COBOL 85)
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 6/38
Structured COBOL Programming, Stern & Stern, 9th Edition
The Simple PERFORMReviewed
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 7/38
Structured COBOL Programming, Stern & Stern, 9th Edition
The Basic Formats
• There are two formats of the basicPERFORM:
1. In-Line PERFORMPERFORM
Statements to
be executedEND-PERFORM
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 8/38
Structured COBOL Programming, Stern & Stern, 9th Edition
The Basic Formats
• An in-line PERFORM begins with the wordPERFORM, is followed by statements tobe executed, and ends with an END-
PERFORM scope terminator.
• All instructions within the PERFORM ...END-PERFORM are executed in sequence.
• Periods are optional after any scopeterminator except at the end of aparagraph, when they are required.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 9/38
Structured COBOL Programming, Stern & Stern, 9th Edition
The Basic Formats
2. PERFORM Paragraph-Name
PERFORM [paragraph-name-1]
• The PERFORM paragraph-namestatement will:
1. Execute all instructions in the named
paragraph.2. Transfer control to the next instruction in
sequence, after the PERFORM paragraph-name.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 10/38
Structured COBOL Programming, Stern & Stern, 9th Edition
The Basic FormatsPseudocode
• Pseudocode for In-line PERFORM
PERFORM
. Statements
. to be performed
. go hereEND-PERFORM
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 11/38
Structured COBOL Programming, Stern & Stern, 9th Edition
The Basic FormatsPseudocode
• Pseudocode for a PERFORM paragraph-name
PERFORM Paragraph.
.
PARAGRAPH
.
. {Statements to be performed go here
.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 12/38
Structured COBOL Programming, Stern & Stern, 9th Edition
Modularizing Programs UsingPERFORM 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.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 13/38
Structured COBOL Programming, Stern & Stern, 9th Edition
Modularizing Programs UsingPERFORM Statements
Version 2: Modular ApproachIF AMT1-IN < AMT2-INPERFORM 300-OK-RTN
ELSEPERFORM 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.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 14/38
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} paragraph-
name-2
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 15/38
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 paragraph-
name-1 until the end of paragraph-name-2 is reached.
• Control is then transferred to the
statement directly following thePERFORM.
Th U d Mi f
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 16/38
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 returnscontrol to the statement following thePERFORM, the GO TO permanentlytransfers control to another paragraph.
• However, in well-designed, structuredprograms it is best to avoid the use of GOTO as much as possible.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 17/38
Structured COBOL Programming, Stern & Stern, 9th Edition
The EXIT Statement
• EXIT is a COBOL reserved word thatperforms no operation.
•
It is used to allow execution to pass overother statements or to transfer controlback to the statement following theoriginal PERFORM.
• It is used, when necessary, as an endpoint in a paragraph being performed.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 18/38
Structured COBOL Programming, Stern & Stern, 9th Edition
ITERATION USINGOTHER TYPES OFPERFORMs
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 19/38
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} paragraph-
name-2]
UNTIL condition-1
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 20/38
Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM UNTIL . . . A Review
• With COBOL 85, if an in-line PERFORM isused, no paragraph-name is coded.
– The statements within the PERFORM end withan END-PERFORM scope terminator.
• Any simple or compound condition can bespecified in a PERFORM UNTIL ...
• As with a simple PERFORM, COBOL 85permits an in-line PERFORM UNTIL …
– this format also uses END-PERFORM as ascope terminator.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 21/38
Structured COBOL Programming, Stern & Stern, 9th Edition
Coding a Loop with aPERFORM
• 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 betested reaches the desired value
(e.g., PERFORM UNTIL COUNTER1 = 5 orPERFORM UNTIL COUNTER1 >= 5).
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 22/38
Structured COBOL Programming, Stern & Stern, 9th Edition
Coding a Loop with aPERFORM
3. Include, as one of the instructions withinthe PERFORM UNTIL, a statement thatincreases (or decreases) the value of the
field being tested (e.g., ADD 1 TOCOUNTER1).
– The contents of the counter is used to control
the number of times that the loop isperformed.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 23/38
Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM TIMES
• Format
PERFORM paragraph-name-1
[{THROUGH} {THRU} paragraph-name-2]
{integer-} {identifier-1} TIMES
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 24/38
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.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 25/38
Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM TIMES
• The THRU clause is optional with anyPERFORM paragraph-name statement.
•
When using the integer option with thePERFORM TIMES format, only the actualnumber is acceptable.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 26/38
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP
• Use PERFORM TIMES rather thanPERFORM UNTIL if you know in advancethe specific number of times a paragraphis to be executed.
• If the number of times a paragraph is tobe executed is variable use a PERFORMUNTIL.
• It is also best to use a PERFORM UNTIL if the number of times a paragraph is beingexecuted needs to be used for output.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 27/38
Structured COBOL Programming, Stern & Stern, 9th Edition
PERFORM VARYING
• The last format for a PERFORM statementis the most comprehensive:
PERFORM paragraph-name-1
[{THROUGH} {THRU} paragraph-name-2]
VARYING identifier-1 FROM
{identifier-2}{integer-1} BY
{identifier-3} {integer-2}
UNTIL condition-1
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 28/38
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.
O S
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 29/38
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 isused, no paragraph-name is coded,
– Statements within the PERFORM end with anEND-PERFORM scope terminator.
THE PERFORM WITH TEST
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 30/38
Structured COBOL Programming, Stern & Stern, 9th Edition
THE PERFORM WITH TEST AFTER OPTION (COBOL 85)
• The WITH TEST AFTER clause can also beused with the VARYING option of thePERFORM.
– The format is:PERFORM WITH TEST AFTER VARYING ....
• Note that PERFORM UNTIL (with no WITH
TEST clause) and PERFORM WITH TESTBEFORE UNTIL do the same thing.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 31/38
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SLIDES END HERE
CHAPTER SUMMARY COMES NEXT
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 32/38
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).
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 33/38
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
–
After execution of the instructions withineither the PERFORM ... END-PERFORM or inthe PERFORM paragraph-name, control istransferred 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 paragraphswill be performed indefinitely.
CHAPTER SUMMARY
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 34/38
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 namedparagraph(s) will not be executed at all.
– With COBOL 85, the WITH TEST AFTER clausecan be used to test the condition after the
paragraph has been executed once.
3. The PERFORM TIMES statement
• A numeric identifier or an integer canprecede the word TIMES; with COBOL 85,an arithmetic expression can be used aswell.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 35/38
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
4. The PERFORM VARYING statementa. The counter or loop control field must be
defined in the DATA DIVISION, typically inWORKING-STORAGE. An initial VALUE for the
loop control field is not required.
b. The PERFORM VARYING automatically doesthe following:
(1) Initializes the counter with the valuespecified in the FROM clause.
(2) Tests the counter for the condition specifiedin the UNTIL clause.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 36/38
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
(3) Continues with the statement directlyfollowing the PERFORM if the conditionspecified in the UNTIL clause is satisfied.
(4) Executes the named paragraph(s) if the
condition specified in the UNTIL clause is notmet.
(5) After execution of the named paragraph(s),increases (or decreases) the counter by the
value of the integer or identifier specified inthe VARYING clause.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 37/38
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 werecommend that you avoid this option.
2. PERFORM statements within PERFORM
statements are permissible. These arecalled nested PERFORMs.
8/4/2019 ch09_97
http://slidepdf.com/reader/full/ch0997 38/38
St t d COBOL P i St & St 9th Editi
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 necessaryto code separate paragraphs.
– The PERFORM is terminated with an END-PERFORM.