9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community...

26
9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin- Stout (Emeritus) John Wiley & Sons, Inc. 11th edition

Transcript of 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community...

Page 1: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-1

COBOL for the 21st Century

Nancy Stern Hofstra University

Robert A. Stern Nassau Community College

James P. Ley University of Wisconsin-Stout (Emeritus)

John Wiley & Sons, Inc.11th edition

Page 2: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-2

Iteration: Beyond the Basic PERFORM

Chapter 9

Page 3: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-3

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 4: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-4

Nested PERFORM

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

Perform 200-Paragraph .

.

200-Paragraph.

Perform 500-ParagraphNested PERFORM

Page 5: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-5

Nested In-Line PERFORM

• In-line PERFORMs can include nested in-line PERFORMs or PERFORMs with paragraph-name

Perform. . .

Perform . . .

End-Perform. . .

End-Perform

Page 6: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-6

PERFORM UNTIL

PERFORM [paragraph-name-1]

UNTIL condition-1

• Repeats statements in paragraph until condition is true

• Called iteration or loop

Format

Page 7: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-7

Loop Example

• Write Hello-Record-Out 3 times

Move Zeros To Count

Perform Until Count = 3

Write Hello-Record-Out

Add 1 To Count

End-Perform

Page 8: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-8

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 9: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-9

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 10: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-10

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

Page 11: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-11

Nested PERFORMs

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

• A loop within another loop is called a nested loop

Page 12: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-12

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

Outer loop

Inner loop

Page 13: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-13

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

Page 14: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-14

PERFORM VARYING

PERFORM VARYING identifier-1 identifier-2 identifier-3

FROM BYinteger-1 integer-2

UNTIL condition-1

statement-1 …

END-PERFORM

Format

Page 15: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-15

PERFORM VARYING

• 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

Page 16: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-16

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

Page 17: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-17

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 18: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-18

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

Page 19: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-19

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 20: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-20

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

Page 21: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-21

Print Times Tables

Perform 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

Outer loop

Inner loop

Page 22: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-22

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 23: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-23

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 24: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-24

TEST AFTER Example

Perform With Test After

Until Opt-Num >=1 And <= 5

Display 'Select option (1-5)'

Accept Opt-Num

End-Perform

Example

Page 25: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-25

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 26: 9-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

9-26

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.

The original presentation has been modified.