1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration ...
-
Upload
barbra-weaver -
Category
Documents
-
view
214 -
download
0
Transcript of 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration ...
![Page 1: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/1.jpg)
1
Chapter 9
![Page 2: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/2.jpg)
To familiarize you with Simple PERFORM How PERFORM statements are used for
iteration Options available with PERFORM
2
![Page 3: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/3.jpg)
Simple PERFORM Reviewed Iteration Using Other Types of PERFORMs Using Nested PERFORM VARYING
Statements PERFORM WITH TEST AFTER option
3
Some new stuff
![Page 4: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/4.jpg)
PERFORM [paragraph-name-1]
Executes all instructions in named paragraph
Then transfers control to instruction following PERFORM
4
Format
![Page 5: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/5.jpg)
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
5
![Page 6: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/6.jpg)
PERFORM...
END-PERFORM Use when only a few statements are to be
executed Modularize with PERFORM paragraph-name
when many statements required
6
Statements to be executed
Format
![Page 7: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/7.jpg)
PERFORM may be one instruction in range of another PERFORM
Perform 200-Paragraph .
.
200-Paragraph. Perform 500-Paragraph
7
Nested PERFORM
![Page 8: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/8.jpg)
In-line PERFORMs can include nested in-line PERFORMs or PERFORMs with paragraph-name
Perform. . .
Perform . . .
End-Perform. . .
End-Perform
8
![Page 9: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/9.jpg)
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
9
Format
![Page 10: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/10.jpg)
GO TO paragraph-name-1
Does not return control to following statement like PERFORM
Transfers control permanently to another paragraph
10
Format
![Page 11: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/11.jpg)
Sequence◦ instructions executed in order in which they
appear IF-THEN-ELSE or selection
◦ instructions executed depending on value of condition
Iteration or looping◦ series of instructions executed repeatedly◦ either in-line or in different module
12
![Page 12: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/12.jpg)
PERFORM [paragraph-name-1]UNTIL condition-1
Repeats statements in paragraph until condition is true
13
Format
![Page 13: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/13.jpg)
No paragraph name follows PERFORM Instead statements to be repeated placed
between PERFORM UNTIL … END-PERFORM
14
![Page 14: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/14.jpg)
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
Stuff you know already
15
![Page 15: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/15.jpg)
Count initialized to zero so not equal to 3 when condition checked first time
Hello displayed on screen and Count incremented to 1
Move Zeros To CountPerform Until Count = 3
Display 'Hello'Add 1 To Count
End-Perform
16
![Page 16: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/16.jpg)
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
17
![Page 17: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/17.jpg)
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
18
Example Paragraph executed 0 times
![Page 18: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/18.jpg)
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-Perform
19
Example
Changes Y so condition eventually met
![Page 19: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/19.jpg)
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
22
![Page 20: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/20.jpg)
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 How-Many Times
23
![Page 21: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/21.jpg)
Also used with in-line loop Loop below executes MULTIPLY statement 3
times
Move 2 To NumPerform 3 Times Multiply 2 By NumEnd-Perform
Num equals 16 when loop ends
24
![Page 22: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/22.jpg)
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
25
![Page 23: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/23.jpg)
Sum even integers from 2 through 10
Move 0 To TotalMove 2 To CountPerform Until Count > 10Add Count To TotalAdd 2 To Count
End-PerformDisplay 'Total=', Total
26
Initialize field to be tested
Test field until it reaches desired value
Change field tested so condition eventually met
Result: Total = 30
![Page 24: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/24.jpg)
Perform 5 TimesPerform 10 Times
Read record from file and add its amount to group
totalEnd-Read
End-PerformPerform Print-Group-Total
End-Perform
27
Outer loop
Inner loop
![Page 25: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/25.jpg)
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
28
![Page 26: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/26.jpg)
PERFORM VARYING identifier-1 identifier-2 identifier-3
FROM BYinteger-1 integer-2
UNTIL condition-1
statement-1 …
END-PERFORM
29
Format
![Page 27: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/27.jpg)
Repeatedly executes statements in loop while varying value of a field
First identifier-1 is given FROM value Condition then tested Executes statements in loop if condition not
met Then adds BY value to identifier-1 and
repeats condition test
30
![Page 28: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/28.jpg)
Perform Varying Ctr From 1 By 1Until Ctr > 5Display 'Ctr = ', Ctr
End-Perform
Sets Ctr to 1, since Ctr > 5 not true, executes DISPLAY statement
Increments Ctr by 1, tests condition again Like the for next loop in C with a step increment
31
![Page 29: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/29.jpg)
Finds sum of odd numbers from 1 to 25Move 0 To TotalPerform Varying Ctr From 1 By 2
Until Ctr > 25Add Ctr To Total
End-PerformDisplay 'Total = ', Total
32
Output: Total = 169
![Page 30: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/30.jpg)
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
33
![Page 31: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/31.jpg)
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
34
![Page 32: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/32.jpg)
Perform Varying N1 From 1 By 1Until N1 > 9Perform Varying N2 From 1 By 1
Until N2 > 9Compute Product = N1 * N2Display N, ' * ' M ' = ',
ProductEnd-Perform
End-Perform
35
Outer loop
Inner loop
![Page 33: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/33.jpg)
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
36
![Page 34: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/34.jpg)
PERFORM [paragraph-name-1] BEFORE
[WITH TEST ] AFTER
UNTIL condition-1
37
Format
![Page 35: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/35.jpg)
Perform With Test AfterUntil Opt-Num >=1 And <= 5Display 'Select option (1-5)'Accept Opt-Num
End-Perform
38
Example
![Page 36: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/36.jpg)
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
39
![Page 37: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/37.jpg)
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
40
![Page 38: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/38.jpg)
Formats of PERFORM Statement◦ PERFORM VARYING
Automatically initializes and changes value of loop counter
Nested PERFORMS (PERFORM statements within PERFORM statements) allowed
41
![Page 39: 1 Chapter 9. To familiarize you with Simple PERFORM How PERFORM statements are used for iteration Options available with PERFORM 2.](https://reader035.fdocuments.us/reader035/viewer/2022062518/5697bf751a28abf838c8056c/html5/thumbnails/39.jpg)
In-line PERFORMs permitted with all PERFORM options◦ Code does not need to be in separate paragraph◦ Terminated with END-PERFORM
42