Iteration: Beyond the Basic PERFORM

Slides:



Advertisements
Similar presentations
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)
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 6 - Visual Basic Schneider
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
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 Iteration: Beyond the Basic PERFORM Chapter 9.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Chapter 4 – C Program Control
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Control Structures II Chapter 3
Chapter 3: Decisions and Loops
Python: Control Structures
Programming Logic and Design Fourth Edition, Comprehensive
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
JavaScript: Control Statements.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Designing and Debugging Batch and Interactive COBOL Programs
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Outline Altering flow of control Boolean expressions
Loops A loop is a repetition control structure.
The University of Texas – Pan American
Iteration: Beyond the Basic PERFORM
3 Control Statements:.
LOOPS BY: LAUREN & ROMEO.
Chapter 6: Repetition Statements
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Introduction to Repetition Structures
Chapter 6 - VB.Net by Schneider
CHAPTER 4 Iterative Structure.
Chapter 5: Control Structures II (Repetition)
Java Programming Loops
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Iteration: Beyond the Basic PERFORM Chapter 9 Iteration: Beyond the Basic PERFORM

Simple PERFORM PERFORM [paragraph-name-1] Executes all instructions in named paragraph Then transfers control to instruction following PERFORM Format 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

Nested PERFORM PERFORM may be one of instructions in range of another PERFORM Perform 200-Paragraph . 200-Paragraph. Perform 500-Paragraph Nested PERFORM

Nested In-Line PERFORM In-line PERFORMs can include nested in-line PERFORMs or PERFORMs with paragraph-name Perform . . . End-Perform

Control Structures Sequence IF-THEN-ELSE or selection 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

PERFORM UNTIL PERFORM [paragraph-name-1] UNTIL condition-1 Repeats statements in paragraph until condition is true Called iteration or loop Format Coding 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

Coding 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

Loop Example Display the message 'Hello' 3 times 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 Count Perform Until Count = 3 Display 'Hello' Add 1 To Count End-Perform 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

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 X Perform 300-Process-Rtn Until X > 5 Example Paragraph executed 0 times

Avoid Loop Errors Consider this loop Move Zeros To Count Perform Until Count = 5 Display Out-Message End-Perform Error occurs because no instruction included to change Count from zero DISPLAY executed over and over again because condition never met 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

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 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-MANY PERFORM 300-PRINT-RTN HOW-MANY TIMES Also used with in-line loop Loop below executes MULTIPLY statement 3 times MOVE 2 TO NUM PERFORM 3 TIMES MULTIPLY 2 BY NUM END-PERFORM NUM equals 16 when loop ends

Nested PERFORMs One of statements in PERFORM loop may be another PERFORM loop A loop within another loop is called a nested loop

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

Nested PERFORM Pseudocode Perform 5 Times Perform 10 Times Read record from file and add its amount to group total End-Read End-Perform Perform Print-Group-Total Outer loop Inner loop 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

PERFORM VARYING PERFORM VARYING identifier-1 identifier-2 identifier-3 Format PERFORM VARYING identifier-1 identifier-2 identifier-3 FROM BY integer-1 integer-2 UNTIL condition-1 statement-1 … END-PERFORM 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

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

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)