Control
Construct
Loop Construct
Loop
Construct
same
sequence of statements needs to be executed repeatedly
a
particular computation may have to be done for many different inputs
number
of repetitions may not be fixed but may depend on the values of the variables
loop
construct makes this possible
Example
distance
between points
several
pairs of points for which distance is to computed
start
the program repeatedly, each time giving a different pair as input
preferable
to start the program only once
program
should keep reading input and computing the distance until all required
distances have been computed
Example
prime
numbers
given a
number n, we want to know whether it is prime or not
simple way
is to try dividing by all numbers m, 1 < m < n
if any of
these divides n , n is not prime else it is
same
computation for all numbers m
Loop
Construct
uncontrolled
loop
a block of
statements is repeatedly executed
exit statement
used to stop the loop
controlled
loop
a block of
statements is executed a specified number of times depending on values of
variables
each execution of the block is an iteration
Uncontrolled
Loop
general
form of uncontrolled loop
do
statements
if ( condition1 ) exit
statements
if ( condition2 ) exit
statements
end do
Uncontrolled
Loop
statements
enclosed between do and end do are executed repeatedly
if
condition1 or 2 is satisfied exit statement is executed which terminates
the loop
after
exiting the loop, the statement following end do is executed
exit
statement may occur anywhere between do and end do and any number
of times ( at least once)
Example
of Uncontrolled Loop
distance
between pair of points
repeatedly
read coordinates of points and compute distance
stop when
all four coordinates are 0.0
do
read * , a , b , c, d
if
(abs(a)+abs(b)+abs(c)+abs(d) == 0.0) exit
! compute distance
end do
Another
Example
distance
between point and line
do
read *, a, b, c, p, q
if
(abs(a)+abs(b)+abs(c) == 0.0) exit
if
(abs(a)+abs(b) == 0.0) then
print *, error in input
cycle ! previously it was
stop
endif
!compute distance
end do
Cycle
Statement
cycle
statement
current
iteration is stopped
a new
iteration is started
statements
between cycle and end do are not executed
first
statement after do is executed next
useful
when incorrect data is encountered
Controlled
Loop
controlled
loop executed a fixed number of times
number
of iterations may depend on values of variables
general
form
do var = initial, final, increment
statements
end do
Controlled
Loop
var
is called control variable, must be integer
initial,final,increment
are integer expressions
statements
must not modify value of var
initially,
var = initial and after every iteration increment is added to var
loop
is terminated when value of var exceeds final ( assuming positive increment)
increment must have non-zero value
Controlled
Loop
number
of iterations (niter) =
(final-initial+increment)/increment
niter
is computed when the do statement is first encountered
if niter <= 0 , loop is not executed
if
niter > 0, loop is executed, niter is decremented by 1, var is incremented
loop
terminates when niter <= 0
Controlled
Loop
niter
is computed based on the values of
initial, final, increment when do statement is first
encountered
even
if these values change inside the loop niter is not re-computed
var
is incremented by the value of increment when do was first
encountered
values of initial, final, increment should not be
changed inside the loop
Example
j = 1
n = 10
do
i = 1, n, j
n = n+1
j = j+2
end do
! how many times is the loop
executed
print * i, j, n ! what will be printed?
value
of i may not defined after termination
control
variable not used outside the loop
Example
of Controlled Loop
testing
if an integer n >= 2 is prime
prime = .true.
do
i = 2, n-1
! if increment is not given it is assumed to
be 1
if
( modulo(n,i) == 0 ) then
! modulo is an intrinsic function giving the
remainder
prime = .false.
exit
endif
end do
Example
increment
is optional in a controlled loop
assumed to
be 1 if not specified
exit
statement may be used
loop is
terminated and statement after end do is executed
cycle
statement can also be used
current
iteration is terminated, niter is decremented, var is incremented and new iteration started
Example
improvements
are possible in the algorithm
enough to
check up to sqrt(n)
check for
only odd numbers
if ( modulo(n,2) == 0 .and. n > 2) then
prime = .false.
else
do
i = 3, nint(sqrt(real(n))),2
..
end do
endif
Nesting
of Control Constructs
as
with the if construct, loop constructs may be nested
loops
may occur inside other loops or inside an if construct
inner
loop must be completely contained in an outer loop or the if, elseif
or else block of if construct
do
and end do statements are paired like brackets
Naming
of Control Constructs
constructs
like if and do may be given a name
exit
and cycle applied to specified loop
outer : do
inner : do
if ( condition1 ) exit
if ( condition2) exit outer
end do inner
end do outer
! if
condition1 is true inner loop is exited
! if
condition2 is true outer loop is exited
Naming
of Control Constructs
if
a name is given to an if statement then the corresponding elseif,
else and endif statements must be given the same name
test : if ( condition) then
.
elseif ( condition2) then test
else test
.
endif test
the same applies for do and end do and select
case , case and end select
Summary
loop
constructs allow a sequence of statements to be executed repeatedly
the
number of repetitions can depend on values of variables
two
types of loop constructs
uncontrolled
loops
controlled
loops
exit
statement used in uncontrolled loops