9-1 iteration: beyond the basic perform chapter 9

58
9-1 Iteration: Beyond the Basic PERFORM Chapter 9

Upload: kristopher-sims

Post on 04-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-1

Iteration: Beyond the Basic PERFORM

Chapter 9

Page 2: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-2

Simple PERFORM

PERFORM [paragraph-name-1]

• Executes all instructions in named paragraph

• Then transfers control to instruction following PERFORM

Format

Page 3: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-3

Simple PERFORM

• Use to execute a paragraph from different points in a program

• Use to modularize program– Write each set of related instructions as

separate module or paragraph– Use PERFORM paragraph-name to

execute each module as needed

Page 4: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-4

0000-MAIN-MODULE. all first modules should start with 000-something. PERFORM 1000-INITIALIZATION PERFORM 2000-PROCESS-RENTALS UNTIL F-MORE-RECORDS = "N" PERFORM 3000-TERMINATION STOP RUN. ***************************************************************** * FUNCTION: OPENS FILES, PRINTS HEADINGS, DOES PRIMING READ OF * CUSTOMER FILE * CALLS: 100-GET-DATE-AND-TIME * 1200-PRINT-HEADINGS * CALLED BY: N/A ***************************************************************** 1000-INITIALIZATION.

OPEN INPUT CUSTOMER-FILE OUTPUT PRINT-FILE

PERFORM 1100-GET-DATE-AND-TIME

PERFORM 1200-PRINT-HEADINGS

READ CUSTOMER-FILE AT END MOVE "N" TO F-MORE-RECORDS NOT AT END ADD 1 TO A-TOTAL-RECORDS-READ END-READ.

Page 5: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-5

In-Line PERFORM

PERFORM...

END-PERFORM• Use when only a few statements are to

be executed• Modularize with PERFORM paragraph-

name when many statements required

Statements to be executed

Format

Page 6: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-6

Nested PERFORM

• PERFORM may be one of instructions in range of another PERFORM

… Perform 200-Paragraph …. .

.

200-Paragraph. … Perform 500-Paragraph

Nested PERFORM

Page 7: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-7

0000-MAIN-MODULE. all first modules should start with 000-something. PERFORM 1000-INITIALIZATION PERFORM 2000-PROCESS-RENTALS UNTIL F-MORE-RECORDS = "N" PERFORM 3000-TERMINATION STOP RUN. ***************************************************************** * FUNCTION: OPENS FILES, PRINTS HEADINGS, DOES PRIMING READ OF * CUSTOMER FILE * CALLS: 100-GET-DATE-AND-TIME * 1200-PRINT-HEADINGS * CALLED BY: N/A ***************************************************************** 1000-INITIALIZATION.

OPEN INPUT CUSTOMER-FILE OUTPUT PRINT-FILE

PERFORM 1100-GET-DATE-AND-TIME

PERFORM 1200-PRINT-HEADINGS

READ CUSTOMER-FILE AT END MOVE "N" TO F-MORE-RECORDS NOT AT END ADD 1 TO A-TOTAL-RECORDS-READ END-READ.

Nested performs

Page 8: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-8

Nested In-Line PERFORM• In-line PERFORMs can include nested in-line PERFORMs or

PERFORMs with paragraph-name

Perform. . .Perform . . .End-Perform. . .

End-PerformBig thing to remember:

CAREFULLY indent (always!!!)Always use scope terminators!!!

Page 9: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-9

Executing Group of Paragraphs

PERFORM paragraph-name-1

THROUGH

THRUparagraph-name-2

• Use expanded format to execute all statements, including other paragraphs, from paragraph-name-1 through paragraph-name-2

Format

Page 10: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-10

GO TO Statement

GO TO paragraph-name-1

• Does not return control to following statement like PERFORM

• Transfers control permanently to another paragraph

Format

Page 11: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-11

PERFORM … THRU, GO TO,AND EXIT STATEMENTS

• Both still permitted but not used a lot in new COBOL programs

• Provide less control, greater risk of logic errors than using PERFORMs

• EXIT statement used with these as end point in PERFORM …THRU paragraph also should be avoided

Page 12: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-12

Perform 400-do-it thru 499-do-it-exit. …. ….400-do-it.

….perform 800-who-cares.….if a > b

Go to 420-do-moreelse

move c to d.420-do-more.

if some-conditiongo to 499-do-it-exit.

430-do-still-more.….

499-do-it-exit.exit.

The old way: but such code still clearly lives and breathes!!!

Notice the performs;Note also the falling into paragraphs; potential bouncing around within a performed group, etc….Note: this code still works. Handle with care!!!

Page 13: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-13

PERFORM UNTIL (traditional)

PERFORM [paragraph-name-1]

UNTIL condition-1

• Repeats statements in paragraph until condition is true

• Called iteration or loop

Format

Page 14: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-14

In-Line PERFORM UNTIL

• No paragraph name follows PERFORM

• Instead statements to be repeated placed between PERFORM UNTIL … END-PERFORM

Page 15: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-15

PERFORM para UNTIL(traditional PERFORM)

• A paragraph name is included in the PERFORM

• Instead statements to be repeatedly executed in the PERFORMed paragraph until some condition becomes true.

• a MainStay of modern structured programming.

Page 16: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-16

0000-MAIN-MODULE.. PERFORM 1000-INITIALIZATION

PERFORM 2000-PROCESS-RENTALS UNTIL F-MORE-RECORDS = “N”

PERFORM 3000-TERMINATION

Page 17: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-17

Looping PrinciplesCoding a Loop with PERFORM

• Often want to perform some action a certain number of times

• Use a field as a counter to count number of times action is repeated

• Set field to zero initially, then increment it by 1 each time action repeated

• When field equals number of times action is to be repeated, condition is met and loop ends

Page 18: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-18

ALL LOOPS MUST:• Have their loop control variable / condition

initialized prior to entering loop or as part of the loop control itself.

• Controlling variable’s value / values must be altered in each iteration of the loop

• There must be a test to determine when to exit the loop.

• Most loops have a single pre-test and multiple post-tests.– Implies one may never enter the loop, but once in,

control will be checked EACH iteration looking to terminate the loop.

Page 19: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-19

Loop Example

• Display the message 'Hello' 3 times

Move Zeros To Count

Perform Until Count = 3

Display 'Hello'

Add 1 To Count

End-Perform

Note: initialization outslide of loop, pre-test, body of loop, adjusting control variable, post-test…

Page 20: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-20

Loop Example

• Count initialized to zero so not equal to 3 when condition checked first time (pre-test)

• Hello displayed on screen and Count incremented to 1 (variable adjusted)

Move Zeros To Count

Perform Until Count = 3

Display 'Hello'

Add 1 To Count

End-Perform

Repeated post-tests, increments, and executions.

Page 21: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-21

Loop Example

• Condition checked again and still not met (1 not equal to 3) so in-line statements repeated again

• ‘Hello’ displayed second time and Count incremented to 2

Move Zeros To CountPerform Until Count = 3

Display 'Hello'Add 1 To Count

End-Perform

Page 22: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-22

Loop Example

• Count not equal to 3 so ‘Hello’ displayed third time and Count incremented to 3

• Condition met when checked again so loop ends

Move Zeros To Count

Perform Until Count = 3

Display 'Hello'

Add 1 To Count

End-Perform

Page 23: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-23

Coding a Loop

• Precede loop by instruction to initialize field to be tested

• Include PERFORM UNTIL …that repeats until field tested reaches desired value

• Include instruction in loop to change value of field tested so that condition is eventually met

Page 24: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-24

Condition Tested First

• Condition tested before paragraph or in-line statements executed even once

• If condition met on first test, paragraph or statements executed zero times

Move 6 To XPerform 300-Process-Rtn

Until X > 5

ExampleParagraph executed 0 times

Showing pre-test.

Page 25: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-25

Ending PERFORM UNTIL• Loop stops when condition is true• One of instructions in loop should change

identifier used in condition

Move 0 To YPerform Until Y > 10

. . .Add 1 To Y

End-PerformIn-line PERFORM MUST have scope terminator!

Example

Changes Y so condition eventually met

Page 26: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-26

Avoid Loop Errors (infinite loop)

• Consider this loopMove Zeros To CountPerform Until Count = 5

Display Out-MessageEnd-Perform

• Error occurs because no instruction included to change Count from zero

• DISPLAY executed over and over again because condition never met

Page 27: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-27

Avoid Loop Errors

• Loop that executes repeatedly without end called infinite loop

• On mainframe program automatically terminated after fixed period of time

• On PCs press interrupt keys (e.g., Escape key, Ctrl + Break keys) to terminate program

• May see program freeze or scroll ‘forever…’

Page 28: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-28

Alternative Loop

Move 1 To Count

Perform Until Count > 3

Display 'Hello'

Add 1 To Count

End-Perform

• Initialization value for Count now 1

• Condition uses '>' instead of '='

Page 29: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-29

Alternative Loop

• Loop still displays Hello 3 times when Count = 1, 2 and 3

• When Count = 4, loop condition met

• Testing for '>' or '>=' less prone to infinite loops than testing for '='

• If value of Count exceeds 3 but is never exactly equal to 3, loop will still terminate

Page 30: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-30

PERFORM … TIMES

• Executes a sequence of steps a fixed number of times

• No counter needed

• Loop below executes paragraph 300-Print-Rtn 5 times

Perform 300-Print-Rtn 5 Times

Page 31: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-31

PERFORM … TIMES

• May use field whose value represents number of times to repeat loop

• Field must be numeric, containing only positive integers or 0

• Loop below performs 300-Print-Rtn ten times

Move 10 To How-ManyPerform 300-Print-Rtn (perform para)

How-Many Times

Page 32: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-32

PERFORM … TIMES

• Also used with in-line loop• Loop below executes MULTIPLY

statement 3 times

Move 2 To NumPerform 3 Times (in-line perform) Multiply 2 By NumEnd-Perform

• Num equals 16 when loop ends

Page 33: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-33

Loop Example

Sum even integers from 2 through 10

• Initialize a field to first number to be added (2)

• Increment field by 2 so it equals even numbers (2, 4, 6, 8, 10)

• Use this field's value to– Test in condition– Add to a total field to find sum

Page 34: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-34

Code for Loop Example• Sum even integers from 2 through 10

Move 0 To Total

Move 2 To Count

Perform Until Count > 10

Add Count To Total

Add 2 To Count

End-Perform

Display 'Total=', Total

Initialize field to be tested

Test field until it reaches desired value

Change field tested so condition eventually met

Result: Total = 30

How many times does loop iterate? What is value of Count when loop terminates??? (test questions.)

Page 35: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-35

Nested PERFORMs

• One of statements in PERFORM loop may be another PERFORM loop

• A loop within another loop is called a nested loop

Page 36: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-36

Nested PERFORM Example

• Assume 50 records will be read in as 5 groups of 10 records

• Amount fields of each group are to be added and a total printed

• Five totals, one for each group of 10 records will be printed

Page 37: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-37

Nested PERFORM Pseudocode

Perform 5 TimesPerform 10 Times

Read record from file and add its amount to group total

End-ReadEnd-PerformPerform Print-Group-Total

End-Perform Used a lot for ‘control breaks’ (chapter 10).

Outer loop

Inner loop

Page 38: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-38

Nested PERFORM Example

Outer loop repeats these steps 5 times– Performs inner loop to read in 10 records– Prints group total

Inner loop repeated 50 Times or 10 times each time outer loop is repeated

Notice that step to print group total is not part or inner loop– Executed only 5 times or once each time outer

loop executed

Page 39: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-39

TIMES vs UNTIL• Use PERFORM … TIMES if you know in

advance the number of times loop statements are to be executed

• Use PERFORM … UNTIL if number of times loop repeated is needed for output or calculations

• Very VERY oftentimes, you do NOT know the number of times you will need to loop, though…

Page 40: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-40

PERFORM VARYING

PERFORM VARYING identifier-1 identifier-2 identifier-3

FROM BYinteger-1 integer-2

UNTIL condition-1

statement-1 …

END-PERFORMThis guy does it all! (super for array processing!)

Format

Page 41: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-41

PERFORM VARYING• Repeatedly executes statements in loop while

varying value of a field• First identifier-1 is given FROM value

– That is, initialization!• Condition then tested (pre-test first time)• Executes statements in loop if condition not

met• Then adds BY value to identifier-1 (that is,

increments/adjusts control variable) and repeats condition test (post test – may be executed many times.)

Page 42: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-42

PERFORM VARYING Example

Perform Varying Ctr From 1 By 1

Until Ctr > 5

Display 'Ctr = ', Ctr

End-Perform

• Sets Ctr to 1, since Ctr > 5 not true, executes DISPLAY statement

• Increments Ctr by 1, tests condition again• How many times does loop iterate? Value of Ctr

when loop terminates?

Page 43: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-43

PERFORM VARYING Execution

CTR Condition Output

1 1 > 5 false Ctr = 1

2 2 > 5 false Ctr = 2

3 3 > 5 false Ctr = 3

4 4 > 5 false Ctr = 4

5 5 > 5 false Ctr = 5

6 6 > 5 true (loop ends)

Page 44: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-44

PERFORM VARYING Examples

• Finds sum of odd numbers from 1 to 25

Move 0 To Total

Perform Varying Ctr From 1 By 2

Until Ctr > 25

Add Ctr To Total

End-Perform

Display 'Total = ', Total Output: Total = 169

Value of control variable at end? Number of iterations?

Page 45: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-45

PERFORM VARYING Examples

• Statements to be repeated may also be in separate paragraph

Perform 300-Process-Rtn

Varying Ctr From 1 By 1

Until Ctr > 20

• Executes 300-Process-Rtn 20 Times

Page 46: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-46

Nested PERFORM VARYING

• May include a PERFORM VARYING loop as one of statements in another PERFORM VARYING loop

• Each time outer loop is repeated, inner loop is executed until its condition is met

• Following example prints the times tables for numbers 1 to 9

• Nested performs and perform…varyings are used extensively in array processing.

Page 47: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-47

Print Times TablesPerform Varying N1 From 1 By 1

Until N1 > 9

Perform Varying N2 From 1 By 1

Until N2 > 9

Compute Product = N1 * N2

Display N, ' * ' M ' = ', Product

End-Perform

End-Perform

Notice indentation and alignment!

Outer loop

Inner loop

Page 48: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-48

Print Times Tables Execution

N1 N2 Output 1 1 1 * 1 = 1 1 2 1 * 2 = 2 ... ... ... 1 9 1 * 9 = 9 2 1 2 * 1 = 2 2 2 2 * 2 = 4 ... ... ... 2 9 2 * 9 = 18

Outer loop first time

Inner loop repeats 9 times

Outer loop second time

Inner loop repeats 9 times

Page 49: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-49

Print Times Tables Execution

• Outer loop repeated seven more times

• Each time, statements in inner loop are repeated 9 times– N2 initialized to 1 and incremented by 1

each time through inner loop until N2 > 9

• Outer loop ends after printing 9's table

Page 50: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-50

PERFORM UNTIL loop

• Condition tested before statements in loop executed first time

• If condition met on first test, statements not executed at all

• Can specify that condition be tested after instructions executed first time

• Then instructions always executed at least once

Page 51: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-51

PERFORM WITH TEST AFTER

PERFORM [paragraph-name-1] BEFORE

[WITH TEST ] AFTER

UNTIL condition-1

Same as accustomed Perform, but with

no pre-test!

Format

Page 52: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-52

TEST AFTER Example

Perform With Test After

Until Opt-Num >=1 And <= 5

Display 'Select option (1-5)'

Accept Opt-Num

End-Perform

See where this might be used?

Example

Page 53: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-53

TEST AFTER Example

• Condition is not checked before loop begins

• DISPLAY and ACCEPT for user to enter Opt-Num always executed at least once

• Checks Opt-Num after user types in value for first time

• If Opt-Num not a value from 1 to 5, loop is repeated

Page 54: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-54

Chapter Summary

• Formats of PERFORM Statement– Simple PERFORM

• In-Line PERFORM

PERFORM … END-PERFORM• PERFORM paragraph-name-1

[THRU paragraph-name-2] • Causes execution of instructions in named

paragraph(s)• After paragraph executed, control returned to

statement after PERFORM

Page 55: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-55

Chapter Summary

• Formats of PERFORM Statement– PERFORM UNTIL repeats instructions until

a condition is met• Condition may be tested before or after

instructions are executed

– PERFORM … TIMES• Use when you know exact number of times

loop statements are to be executed

Page 56: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-56

Chapter Summary

• Formats of PERFORM Statement– PERFORM VARYING

• Automatically initializes and changes value of loop counter

• Nested PERFORMS (PERFORM statements within PERFORM statements) allowed

Page 57: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-57

Chapter Summary

• In-line PERFORMs permitted with all PERFORM options– Code does not need to be in separate

paragraph– Terminated with END-PERFORM

Page 58: 9-1 Iteration: Beyond the Basic PERFORM Chapter 9

9-58

Copyright © 2003 John Wiley & Sons, Inc. 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 written permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & 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.