Download presentation

Presentation is loading. Please wait.

1
Chapter 4 Repetitive Execution

2
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
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 1 1 2 4 3 9 4 16

4
4 Counter-Controlled DO Loops 5 25 6 36 7 49 8 64 9 81 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
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
6 Example & Exercise Homeworks Ellis’s Book Do examples 6.1 & 6.2 on pages 140 & 142. Do exercises 6.1 on pages 145-146.

7
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
8 General repetition structure Statement-sequence 1 Logical- expression Statement-sequence 2 false true

9
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 1 + 2 + 3 + … + 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
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
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
12 Input Loops do statement-sequence if (logical-expression) exit end do

13
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
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
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
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
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
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
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
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
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
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 156-157. Study chapter 6.

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google