Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.

Slides:



Advertisements
Similar presentations
 Control structures  Algorithm & flowchart  If statements  While statements.
Advertisements

 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Chapter 5: Control Structures II (Repetition)
CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
27 March, 2000 CS1001 Lecture 9 Lab 2 Complete Lecture 8 DO Loops –Counter-controlled DO loop –Depreciation example -- details –Nested loops –Examples.
Chapter 5: Control Structures II (Repetition)
CHAPTER 4 REPETITIVE EXECUTION. Counter-Controlled DO Loops DO Number = 1, 9 PRINT *, Number, Number**2 END DO DO Number = 9, 1, -1 PRINT *, Number, Number**2.
Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
The University of Texas – Pan American
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Programming Logic and Design Fifth Edition, Comprehensive
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
C++ An Introduction to Computing, 3rd ed. 1 Repetition Chapter 7.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
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,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Repetition Statements while and do while loops
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Overview Go over parts of quiz? Another iteration structure for loop.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
CSE123 - Lecture 4 Structured Programming- Loops.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
REPETITION CONTROL STRUCTURE
Repetition and Loop Statements
Outline Altering flow of control Boolean expressions
MSIS 655 Advanced Business Applications Programming
Structured Program
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
CHAPTER 4 Iterative Structure.
EPSII 59:006 Spring 2004.
REPETITION Why Repetition?
Presentation transcript:

Chapter 4 Repetitive Execution

2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the loop is executed once for each value of some control variable in a specified range of values. 2) Repetition controlled by a logical expression; The decision to continue or to terminate repetition is determined by the value of some logical expression.

3 Counter-Controlled DO Loops do controlled-variable = initial-value, limit, step-size statement-sequence end do Where initial-value, limit and step-size are integer expressions with step-size nonzero; step-size may be omitted for value 1 Example do Number = 1, 9 print *, Number, Number**2 end do

4 Counter-Controlled DO Loops Step size in a do loop can be negative (i.e., do I=100, 1, -5) The initial values of the control variable, the limit, and the step size are determined before repetition begins and cannot be changed during the execution of do loop. The initial value, limit, and step size in a do construct may be variables or expressions.

5 Counter-Controlled DO Loops Example read *, Number do I=1, Number Sum=Sum+I end do There may be a second, third or more do loops within the first do loop called nested do loops. Example do M=1, Last_M do N=1, Last_N product = M*N print *, M, ” ”,N, ” ”, Product end do

6 Example & Exercise Homeworks Ellis’s Book  Do examples 6.1 & 6.2 on pages 140 & 142.  Do exercises 6.1 on pages

7 General DO Loops For some problems, the number of iterations can not be determined in advance and a more general repetition structure is required. Repetition in such loops is usually controlled by some logical expression. Fortran 90 provides a do-exit construct that can be used to implement such repetition structures. do statement-sequence 1 if (logical-expression) exit statement-sequence 2 end do The statements that make up the body of the loop are executed repeatedly until the logical-expression in the if statement becomes true.

8 General repetition structure Statement-sequence 1 Logical- expression Statement-sequence 2 false true

9 General DO Loops ATTENTION: If the logical-expression becomes false, an infinite loop results Example for a general loop controlled by a logical expression For a given value of Limit, what is the smallest positive integer Number for which the sum … + Number is greater than Limit, and what is the value of this sum? STEPS: 1) enter the value of limit 2) initialize Number and sum to 0.

10 General DO Loops 3) do if (Sum > Limit) exit !Terminate repetition ! Otherwise continue with the following: Number = Number + 1 Sum = Sum + Number end do 4) Display Number and Sum

11 Input Loops  In case of reading and processing a set of data values, it may not be possible to know beforehand how many data values must be processed. For this reason one of the easiest ways to implement input loops is by using a test-in-the-middle loop integer :: Response do read *, Response if (Response == 0) exit end do Sometimes, however it is convenient to use a posttest or test-at- the-bottom loop in which the termination test is made after the body of the loop is executed.

12 Input Loops do statement-sequence if (logical-expression) exit end do

13 The Do-cycle construct Cycle statement works similar to the exit statement. For example: Assume that we wanted to process only temperatures of O o C or above if (Celsius < 0.0) print *, ”Temperature must be 0 or above” cycle end if cycle statement is very similar to the exit statement except that instead of transferring control to the statement after the end do statement it transfers control back to the start of the loop in exactly the same way as if it, in fact, transferred control to the end do statement.

14 Named Do Constructs Fortran 90 permits naming for do constructs too. For example, name: do …. end do name We know that this is especially useful in the case of nested do loops. OuterLoop: do M = 1, Last_M InnnerLoop: do N = 1, Last_N Product = M * N print *, M, ” ”, N, ” ”, Product end do InnerLoop end do OuterLoop

15 Applications 1) Mean Time to failure: Suppose that they want you to evaluate the reliability of a particular component for a future space probe to Mars. As part of this evaluation, an engineer at the laboratory has tested several of these components and recorded the time at which each failed. Using this data write a program to compute the mean time to failure. Solution: Specification: The input for this problem is a collection of failure times, and output is the average or mean of these times. Design: One initial algorithm for solving the problem may be in the following form. 1) Initialize a counter and a running sum to zero. 2) Repeatedly read a new data value, count it, and add it to the running sum.

16 Applications 3) After all the data has been processed, calculate the mean value and display the counter and the mean value. Clearly, step2 requires an input loop 2) Repeat the following steps a) Attempt to read a failure time. b) If the end of data is encountered, terminate repetition; otherwise continue with the following. c) Increment the counter by 1. d) Add the failure time to the running sum. In case of steps c and d bypassed, the sum would be 0.

17 Applications Therefore we must check the sum because division by zero is not permitted. To guard against this error, we will check that the counter is nonzero before performing this division: 3) If the counter is nonzero, do the following: a) Divide the sum by the count to obtain the mean failure time. b) Display the count and the mean failure time. else Display a no data message. Variables of the problem: Failure time Current failure time read NumTimes Number of failure-time readings Sum Sum of failure times MeanFailureTime Mean time to failure

18 Applications Algorithm for mean-time-to-failure problem Input: A collection of failure times Output: The mean time to failure and the number of failure times 1) Initialize NumTimes to 0 and Sum to 0.0 2) Repeat the following: a) Attempt to read a FailureTime. b) If the end of data is encountered, terminate repetition; otherwise, continue with the following. c) Increment NumTimes by 1. d) Add FailureTime to the running Sum.

19 Applications 3) If NumTimes not equal to 0 do the following: a) Calculate MeanFailureTime = Sum / NumTimes. b) Display NumTimes and MeanFailureTime. else Display a no data message.  Check the source code Mean_Time_to_Fail.f !

20 Applications 2) Factorial of any Number: Factorial of any given number (say Limit) is given by: Factorial = 1*2*3*…*Limit and called as Limit! Problem: Compute the factorial of any given number by user.  Check the source code Factorial.f !

21 Applications 3) Power Series: Calculate the sum of the power series 1 + 1/2 + 1/4 + 1/8 + 1/16 + … until the next term is smaller than 1.0e-10.  Check the source code Power.f !

22 Exercise & Study Homeworks Ellis’s Book  Do example 6.4 on page 148.  Do exercises 6.2 on page 156.  Read summary on pages  Study chapter 6.