CHAPTER 9
ITERATION: BEYOND THE BASIC PERFORM
CHAPTER OBJECTIVES
Upon
completion of this chapter, the student should be able to:
1. Explain and demonstrate the various
formats of the PERFORM statement used for iteration.
2. Explain the use and misuse of GO TO
statements.
3. Explain the execution of the PERFORM
... THRU statement.
4. Explain how the EXIT can be used to
terminate a PERFORM statement.
5. Explain the execution of the PERFORM
UNTIL statement.
6. Explain the execution of the PERFORM … TIMES
statement.
7. Explain the procedures for looping.
8. Explain the execution of the PERFORM
VARYING statement.
9. Explain the execution of nested PERFORM
VARYING statements.
10. Explain how the WITH TEST AFTER option
can be used to change the execution of the PERFORM UNTIL statement.
LECTURE OUTLINE
I. The Simple PERFORM Reviewed
A. The Basic Formats
1. The simple PERFORM statement is used to
execute a specified routine from one or more points in a program.
2. Format for the basic PERFORM:
PERFORM [paragraph-name-1]
3. The PERFORM paragraph-name statement
will:
a. Execute all instructions in the named
paragraph.
b. Transfer control to the next
instruction in sequence, immediately following the PERFORM.
4. Review the flowcharts and pseudocode which are used in this section to depict the
PERFORM statement.
5. COBOL 85 permits the use of an in-line
PERFORM. The scope terminator,
END-PERFORM, is used to terminate the in-line PERFORM statement.
B. Modularizing Programs Using PERFORM
Statements
1. Programs should be modular in design;
each set of related instructions should be written as a module.
2. The PERFORM is used to execute modules.
C. Nested PERFORM: A PERFORM within a
PERFORM
1. A PERFORM may be nested within another
PERFORM.
2. When a module executed by a PERFORM
statement contains another PERFORM statement, it is called a nested PERFORM.
D. Executing a Group of Paragraphs With a
Simple PERFORM
1. Several sequential paragraphs may be
performed as a unit by using the PERFORM ... THRU.
2. Expanded format for the PERFORM
statement:
PERFORM
paragraph-name-1
[{THROUGH/THRU} paragraph-name-2]
3. The PERFORM statement executes all
statements from the beginning of paragraph-name-1 until the end of
paragraph-name-2.
4. After execution of the named
paragraphs, control is transferred to the statement directly following the
PERFORM.
5. While sometime the best approach for
solving a programming problem, in general, the use of PERFORM ... THRU should
be avoided.
E. Use and Misuse of GO TO Statements
1. The format for a GO TO is:
GO TO paragraph-name-1
2. The GO TO permanently transfers control
to another paragraph.
3. Structured programming discourages the
use of the GO TO because the program logic is harder to follow. Also, the risk of logic errors is higher with
the GO TO than with the PERFORM statement.
4. A GO TO may be used when a PERFORM ...
THRU has been specified as long as control is not transferred outside the
boundaries of the paragraphs being performed.
F. EXIT Statement
1. EXIT is a reserved word that performs
no operation.
2. It may be used within the last
paragraph of the PERFORM ... THRU to pass over other statements and terminate
the original PERFORM.
3. A GO TO is used to transfer control to
the paragraph-name containing the EXIT statement.
4. Unlike COBOL 74, the EXIT in COBOL 85
can be used in addition to other statements within a paragraph.
II. Iteration Using Other Types of PERFORMs
A. PERFORM UNTIL ... (A Review)
1. Iteration means that a series of instructions
in-line or in a different module are executed repeatedly.
2. The PERFORM UNTIL ... is used to
implement the logical structure of iteration.
3. The format of a PERFORM UNTIL ... is:
PERFORM
[paragraph-name-1]
[{THROUGH/THRU} paragraph-name-2]
UNTIL condition-1
4. The condition in the UNTIL clause is
tested before the named paragraph(s) are performed. If the condition tested is met at the time of
execution, the named paragraph(s) will not be executed at all.
5. For an in-line PERFORM (COBOL 85), no
paragraph-name is coded and the statements within the PERFORM end with the
END-PERFORM scope terminator.
6. Somewhere within the paragraph(s) or
series of instructions being performed, the named variable used in the UNTIL
clause must be changed so that the condition is eventually met. Otherwise, the paragraph(s) will be performed
indefinitely.
B. Coding a Loop with a PERFORM
1. A loop is a sequence of steps that is
executed repeatedly until a condition exists.
2. The loop should:
a. Be preceded by an instruction that
initializes the field to be tested.
b. Include a PERFORM UNTIL ... that is
executed repeatedly until the field to be tested reaches the desired value.
c. Include as one of the instructions
within the PERFORM UNTIL ... a statement that increases (or decreases) the
value of the field being tested.
C. PERFORM … TIMES
1. The PERFORM…TIMES instructs the
computer to execute a sequence of steps a fixed number of times.
2. A PERFORM…TIMES does not require the
use of a counter.
3. The format for a PERFORM…TIMES is:
PERFORM
[paragraph-name-1]
[{THROUGH/THRU} paragraph-name-2]
{integer-1/identifier-1} TIMES
4. If an in-line PERFORM is used, no
paragraph-name is coded, and the statements within the PERFORM end with the
END-PERFORM scope terminator.
5. The integer or identifier preceding
TIMES must be specified in the DATA DIVISION with a numeric PICTURE clause and
contain only integers or zero as its value.
6. In some versions of COBOL, an
arithmetic expression may precede the word TIMES.
D. Examples of Loops
1. Loops can be executed using different
options of the PERFORM.
2. Discuss the several examples of looping
illustrated in this section of the text.
3. Use PERFORM TIMES if you know in
advance the number of times a paragraph is to be executed.
4. Use PERFORM UNTIL if the number of
times a paragraph is to be executed is not known in advance.
E. PERFORM
VARYING
1. The PERFORM VARYING is the most
comprehensive form of the PERFORM statement.
2. The format for the PERFORM VARYING
statement is:
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
3. If an in-line PERFORM is used, no
paragraph-name is coded, and the statements within the PERFORM end with an
END-PERFORM.
4. The PERFORM VARYING automatically does
the following:
a. Initializes the counter (identifier-1)
with the value specified in the FROM clause.
b. Tests the counter for the condition
specified in the UNTIL clause.
c. As long as the condition specified in
the UNTIL clause is not satisfied, the named paragraphs are executed.
d. After each execution of the named
paragraphs, the counter specified in the VARYING clause is increased or
decreased by the value specified in the BY clause.
e. Steps b - d are repeated until the
condition specified is satisfied. At
this point, the loop is terminated and processing continues with the statement
following the PERFORM statement.
5. The loop control field (identifier-1):
a. Must be defined in the DATA DIVISION,
typically in WORKING-STORAGE, and have a PICTURE clause large enough to hold
the maximum value that it can assume.
b. May be either increased or decreased by
the PERFORM VARYING.
c. May be used within the named
paragraphs, but its value should not be altered.
III.
Using Nested PERFORM VARYING
Statements
1. When a nested PERFORM VARYING is
executed:
a. The innermost PERFORM VARYING loop is
executed first.
b. The next outer PERFORM VARYING loop in
sequence is executed next.
2. When nested PERFORM VARYING statements
are used in a program, it is sometimes possible to combine them into a single
statement using a PERFORM ... VARYING ... AFTER.
IV. The PERFORM WITH TEST AFTER Option (COBOL
85)
1. Iteration in COBOL using the PERFORM
UNTIL is similar to a DO WHILE in other languages.
2. The WITH TEST AFTER option permits
implementation of the REPEAT UNTIL form of the iteration logical structure.
3. With this option, the instructions to be
performed are executed before the test is made.
This ensures that the sequence of steps to be performed is executed at
least once.
4. The format for the WITH TEST AFTER
clause is:
PERFORM
[paragraph-name-1]
[WITH TEST {BEFORE/AFTER}]
UNTIL condition-1
SOLUTIONS
TO REVIEW QUESTIONS
I. True-False Questions
1. F After
the named paragraphs have been performed, control returns to
the next instruction in
sequence following the PERFORM statement.
2. T
3. F PERFORM
UNTIL X = Y is used for looping. IF X =
Y is used for a decision and
will
cause a section of code to be executed only once, if at all.
4. T
5. T
6. T
7. T
8. T
9. F The
GO TO is an unconditional branch that permanently transfers
control to another
paragraph. The PERFORM is a temporary transfer
of control that will
return to the statement following the PERFORM.
10. F The
counter is automatic.
II. General Questions
1.
MOVE 1 TO TEMP
MOVE 1 TO RESULT
PERFORM 200-FACTORIAL-RTN N TIMES
.
.
.
200-FACTORIAL-RTN.
MULTIPLY TEMP BY RESULT
ADD 1 TO TEMP.
2a. MOVE
ZEROS TO TOTAL
PERFORM 400-LOOP-RTN 20 TIMES
.
.
.
400-LOOP-RTN.
DISPLAY 'QUANTITY?'
ACCEPT QTY
ADD QTY TO TOTAL.
2b. MOVE
ZEROS TO TOTAL
PERFORM 400-LOOP-RTN VARYING COUNTER
FROM 1 BY 1 UNTIL COUNTER > 20
END-PERFORM
.
.
.
400-LOOP-RTN.
DISPLAY 'QUANTITY?'
ACCEPT QTY
ADD QTY TO TOTAL.
3a. MOVE
0 TO SUM
MOVE 1 TO ODD-NUMBER
PERFORM 210-ADD-ROUTINE 500 TIMES
.
.
.
210-ADD-ROUTINE.
ADD ODD-NUMBER TO SUM
ADD 2 TO ODD-NUMBER.
3b. MOVE 0 TO SUM
MOVE 1 TO ODD-NUMBER
PERFORM 210-ADD-ROUTINE
UNTIL ODD-NUMBER > 999
END-PERFORM
.
.
.
210-ADD-ROUTINE.
ADD ODD-NUMBER TO SUM
ADD 2 TO ODD-NUMBER.
3c.
MOVE 0 TO SUM
PERFORM 210-ADD-ROUTINE
VARYING ODD-NUMBER FROM 1 BY 2
UNTIL ODD-NUMBER > 999
.
.
.
210-ADD-ROUTINE.
ADD ODD-NUMBER TO SUM.
4.
MOVE 0 TO SUM
MOVE 2 TO EVEN-NUMBER
PERFORM 210-ADD-ROUTINE
UNTIL EVEN-NUMBER > 100
END-PERFORM
.
.
.
210-ADD-ROUTINE.
ADD EVEN-NUMBER TO SUM
ADD 2 TO EVEN-NUMBER.
5.
MOVE 0 TO WS-TOTAL-NUMBER-OF-HOURS
PERFORM 210-CALCULATE-TOTAL-HOURS-RTN 365
TIMES
.
.
.
210-CALCULATE-TOTAL-HOURS-RTN.
PERFORM 24 TIMES
ADD 1 TO WS-TOTAL-NUMBER-OF-HOURS
END-PERFORM.
6. WORKING-STORAGE SECTION field
definitions:
01 CALCULATION-FIELDS.
05
CURRENT-DAYS-DEPOSIT PIC 9(3).
05
TOTAL-AMOUNT-SAVED PIC
9(5) VALUE ZERO.
05
TOTAL-AMOUNT-SAVED-EDITED PIC
$$$,$$9.
PROCEDURE DIVISION
code:
PERFORM
VARYING CURRENT-DAYS-DEPOSIT
FROM 1 BY 1 UNTIL CURRENT-DAYS-DEPOSIT >
365
ADD CURRENT-DAYS-DEPOSIT TO
TOTAL-AMOUNT-SAVED
END-PERFORM
MOVE
TOTAL-AMOUNT-SAVED TO TOTAL-AMOUNT-SAVED-EDITED
DISPLAY
'TOTAL AMOUNT SAVED IN ONE YEAR = '
TOTAL-AMOUNT-SAVED-EDITED.
7. WORKING-STORAGE SECTION field
definitions:
01 CALCULATION-FIELDS.
05
ANNUAL-SALARY PIC 9(6) VALUE 80000.
05
WEEKLY-SALARY PIC 9(5)V99.
05
WEEKLY-SAVINGS PIC 9(5)V99.
05
SAVINGS-RATE PIC 9(1)V999 VALUE 0.01.
05
WEEK-NUMBER PIC 9(2).
05
TOTAL-SAVINGS PIC
9(7)V99 VALUE ZERO.
05
TOTAL-SAVINGS-EDITED PIC
$$,$$$,$$9.99.
PROCEDURE DIVISION
code:
DIVIDE
ANNUAL-SALARY BY 52 GIVING WEEKLY-SALARY ROUNDED.
PERFORM VARYING WEEK-NUMBER FROM 1 BY 1
UNTIL
WEEK-NUMBER > 52
MULTIPLY WEEKLY-SALARY BY SAVINGS-RATE
GIVING WEEKLY-SAVINGS ROUNDED
ADD WEEKLY-SAVINGS TO TOTAL-SAVINGS
ADD 0.001 TO SAVINGS-RATE
END-PERFORM
MOVE TOTAL-SAVINGS TO TOTAL-SAVINGS-EDITED
DISPLAY 'TOTAL AMOUNT SAVED IN ONE YEAR =
'
TOTAL-SAVINGS-EDITED.
III. Internet/Critical Thinking Questions
1. Procedural
PERFORMs should be chosen over the in-line PERFORMs if one were selected as a standard. While the in-line PERFORM creates efficient,
clear code when the number of lines to be executed is small, the reality is
that many performed segments contain long and/or complicated code. In this case, it is better to use a
procedural PERFORM so that the program can be broken into modules that have a
single, well-defined function.
Given a choice of
PERFORM…TIMES, PERFORM…UNTIL, or PERFORM…VARYING as a standard, the
PERFORM…UNTIL should be selected because it is the most versatile and may be
used to create code that “acts” like a PERFORM…TIMES or a PERFORM … VARYING.
Search Engine: yahoo.com>Computers & Internet
Keywords: COBOL+style
URL: http://home.swbell.net/mck9/cobol/style/style.html
Contents: Follow
links to Control Structures.
2.
Search Engine: yahoo.com>Computers & Internet
Keywords: COBOL+style
URL: http://flashpages.prodigy.net/bettys1/qoc/
Contents: Follow
links to "bad examples", use the one about "go".
Search Engine: yahoo.com>Computers & Internet
Keywords: COBOL+style
URL: http://home.swbell.net/mck9/cobol/style/style.html
Contents: Follow
links to Control Structures and then to GO TO.
SOLUTIONS
TO DEBUGGING EXERCISES
1a. 25 times (Note: Logic error in code; both
the PERFORM ... VARYING and the
400-ADD-RTN increment X by 1).
1b. No.
Presumably the programmer wants 400-ADD-RTN to be performed 50 times.
1c. A program interrupt will occur when an
attempt is made to read beyond the end of the file.
1d. PERFORM 400-ADD-RTN
VARYING X FROM 1 BY 1 UNTIL X > 50
END-PERFORM
.
.
.
400-ADD-RTN.
READ AMT-FILE
AT END
DISPLAY ’OUT OF DATA AFTER’ X
’RECORDS’
NOT AT END
ADD AMT TO TOTAL
END-READ.
2a. The routine 300-LOOP-RTN is executed
until COUNTER = 5. Since COUNTER is initialized to zero before the PERFORM
statement, once the program executes the 300-LOOP-RTN it will go into an
infinite loop. This is because the
COUNTER field is not changed during the execution of 300-LOOP-RTN. The statement ADD 1 TO COUNTER should be
added to 300-LOOP-RTN.
Another problem is
that all of 200-CALC-RTN will be performed even when end of file is
detected. 200-CALC-RTN should be coded
200-CALC-RTN.
READ SALES-FILE
AT END
MOVE ’NO ’ TO ARE-THERE-MORE-RECORDS
NOT AT END
MOVE 0 TO COUNTER
PERFORM 300-LOOP-RTN
UNTIL COUNTER = 5
END-PERFORM
MOVE TOTAL TO TOTAL-OUT
MOVE TOTAL-REC TO PRINT-REC
WRITE PRINT-REC
END-READ.
2b. No, it would not be correct to eliminate
the MOVE 0 TO COUNTER. For each new record read, COUNTER must be initialized to
zero before executing 300-LOOP-RTN. Of
course, additional code must be added to allow COUNTER to reach 5.
2c. COMPUTE TOTAL = (AMT1 + AMT2) *
1.08 - DISCOUNT.