Title: 4' REPETITIVE EXECUTION
14. REPETITIVE EXECUTION
- PROGRAM REPETITION AND
- THE DO CONSTRUCT
- COUNT - CONTROLLED DO LOOPS
- MORE FLEXIBLE LOOPS
- GIVING NAMES TO DO CONSTRUCTS
-
- Ellis Philips, 1998, p135-161
2PROGRAM REPETITION AND THE DO CONSTRUCT
- A repetition structure or loop makes possible the
repeated execution of one or more statements. - A program loop
- 1.Repeat the following 10 times
- 1.1 Read Celsius temperature
- 1.2 Calculate Fahrenheit equivalent
- 1.3 Print both temperatures
-
3Do loop
- The do construct provides the means for
- controlling the repetition of statements within
- a loop.
- do countinitial,final,inc
- block of statements
- end do
-
4Do loop
-
- do countinitial,final
- block of statements
- end do
-
5Do loop
- do
- block of statements
- end do
-
6COUNT-CONTROLLED DO LOOPSSome examples of do
statements and their effects
- do statement Iteration count do variable values
- do i1,10 10 1,2,3,4,5,6,7,8,9,10
- do j20,50,5 7 20,25,30,35,40,45,50
- do p7,19,4 4 7,11,15,19
- do q4,5,6 1 4
- do r6,5,4 0 (6)
- do x-20,20,6 7 -20,-14,-8,-2,4,10,16
- do n25,0,-5 6 25,20,15,10,5,0
- do m20,-20,-6 7 20,14,8,2,-4,-10,-16
- Maximum iteration count (final-initialinc)/inc
7COUNT-CONTROLLED DO LOOPS
- In a count-controlled do loop
-
- The value of the iteration count is calculated
before the first iteration. -
- The do variable is incremented on each pass
- through the loop.
8Example 6.1 p 141Write a program which first
reads the number of students taking an exam. It
should then read their marks (or scores) and
print the highest andlowest marks followed by
the average mark for the class.
- program Examination
- integer i,number,mark,maximum,minimum,total
- real average
- total0
- maximum-1000
- minimum1000
- print ,How many marks are there ?
- read ,number
- print ,Please type,number,marks on per line
- do i1,number
- read ,mark
- totaltotalmark
- if (markgtmaximum) then
- maximummark
- end if
- if (markltminimum) then
- minimummark
- end if
- end do
9Example 6.2, p142Write a program to print a set
of multiplication tables from 2 times up to 12
times where each table should take the form x
times 1 is x x times 2 is 2x
- program Multiplication_tables
- integer i,j
- do i2,12
- print ,
- print ,i,times table
- do j1,12
- print ,i,times,j,is,ij
- end do
- end do
- end program mutiplication_tables
- Output
- ...
- 4 times table
- 4 times 1 is 4
- 4 times 2 is 8
-
10More flexible loops
- An infinite loop
- do
- .
- if (termltsmall) then
- exit
- end if
- .
- end do
- An fail-safe mechanism
- do count1,max
- .
- if(termltepsilon) then
- exit
- endif
- .
- end do
11Exit and Cycle statements
- Execution of an exit statement in a loop causes
the next statement to be executed to be the one
immediately after the end do statement of the
innermost loop surrounding the exit statement,
unless the exit statement is named, in which case
it will be the statement immediately after the
end do statement having the same name. - The 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 same way as if it, in
fact, transferred control to the end do
statement.
12Examples for the exit statement
- An infinite loop
- do
- .
- if (termltsmall) then
- exit
- end if
- .
- end do
- An fail-safe mechanism
- do count1,max
- .
- if(termltepsilon) then
- exit
- endif
- .
- end do
13Example for the cycle statement
- ! End of data
- case (X)
- exit
- ! Invalid code
- case default
- print ,invalid code please re-enter data
- cycle
- end select
14GIVING NAMES TO DO CONSTRUCTS
- block_name do
- .
- .
- end do block_name
15Existing from a nested do loop
- do
- .
- do
- .
- do
- .
- exit
- .
- do
- .
- end do
- ! This one (1) ?
- .
- end do
- ! or this one (2) ?
- .
- end do
- ! or this one (3) ?
- .
16Named do constructs
- outer do
- .
- inner do
- .
- select case (n)
- case (1)
- exit outer
- case (2)
- exit inner
- case (3)
- cycle outer
- case (4)
- cycle inner
- end select
- .
- end do inner
- .
- end do outer
17Dealing with exceptional situations
- The stop statement causes an immediate
termination of the execution of a program. - The return statement causes an immediate
termination of the execution of a procedure.
18IN-CLASS PROBLEM SESSION - 4
- Objective Learning repetitive execution
Numerical - Methods (Numerical Integration of Functions)
- Assignment
- Write a program to approximate the integral of a
given - function over the interval (a,b) using the
rectangle method - with altitudes chosen at the midpoints of the
subintervals.
19Numerical Integration Method (Nyhoff Leestma,
1992, page 157)
20HOMEWORK - 4
- Objective Learning repetitive execution
Numerical Methods (Infinite - Series)
- Assignment
- The infinite series
- converge to the number e. For a positive integer
k, k! is the product of the - integers from 1 through k 0! is defined to be 1.
The nth partial sum of such - a series is the sum of the first n terms of the
series for example, - is the 4th partial sum. Write a program to
calculate and print the first - 10 partial sums of this series.
-