chapter 4 repetitive execution. 2 types of repetition there are two basic types of repetition: 1)...

22
Chapter 4 Repetitive Execution

Post on 19-Dec-2015

252 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 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

Chapter 4

Repetitive Execution

Page 2: 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

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.

Page 3: 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

3

Counter-Controlled DO Loops

do controlled-variable = initial-value, limit, step-size statement-sequenceend doWhere initial-value, limit and step-size are integer expressions with

step-size nonzero; step-size may be omitted for value 1Example

do Number = 1, 9 print *, Number, Number**2end do 1 1 2 4 3 9 4 16

Page 4: 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

4

Counter-Controlled DO Loops

5 256 367 498 649 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.

Page 5: 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

5

Counter-Controlled DO Loops

Example read *, Number do I=1, Number Sum=Sum+I end doThere 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 end do

Page 6: 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

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.

Page 7: 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

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-sequence1

if (logical-expression) exit statement-sequence2

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.

Page 8: 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

8

General repetition structure

Statement-sequence1

Logical-expression

Statement-sequence2

false

true

Page 9: 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

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 + … + Numberis greater than Limit, and what is the value of this sum?

STEPS:1) enter the value of limit2) initialize Number and sum to 0.

Page 10: 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

10

General DO Loops

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

Page 11: 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

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 :: Responsedo read *, Response if (Response == 0) exitend 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.

Page 12: 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

12

Input Loops

do statement-sequence if (logical-expression) exit end do

Page 13: 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

13

The Do-cycle construct

Cycle statement works similar to the exit statement.

For example: Assume that we wanted to process only temperatures of Oo 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.

Page 14: 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

14

Named Do Constructs

Fortran 90 permits naming for do constructs too. For example,name: do …

. . .

end do nameWe 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 InnerLoopend do OuterLoop

Page 15: 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

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.

Page 16: 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

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.

Page 17: 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

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 readNumTimes Number of failure-time readingsSum Sum of failure timesMeanFailureTime Mean time to failure

Page 18: 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

18

Applications

Algorithm for mean-time-to-failure problemInput: A collection of failure timesOutput: The mean time to failure and the number of failure

times

1) Initialize NumTimes to 0 and Sum to 0.02) 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.

Page 19: 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

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 !

Page 20: 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

20

Applications

2) Factorial of any Number:Factorial of any given number (say Limit) is given by: Factorial = 1*2*3*…*Limitand called as Limit!

Problem: Compute the factorial of any given number by user.

Check the source code Factorial.f !

Page 21: 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

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 !

Page 22: 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

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.