Sie sind auf Seite 1von 38

Do Loops

In some situations it is necessary to have an ordering built into the


decision as to which choice to take, because there is an overlap
between some of the possible decision criteria.
EXAMPLE 1 : Consider that you are a basketball addict, but
especially a Efes-Pilsen fan, then the decision as to what to do on
Saturday afternoon might look like this,

if it is the basketball season and the Efes are at home then


->go to Abdi Ipekci
else if it is the basketball season and the game is on TV then
->watch the game on TV
else if it is the basketball season then
->go to any nearby basketball game
else
->rent a basketball video and watch it at home

EXAMPLE 2 : Consider that you are a soccer fan, and are only
interested in watching football matches in which they are
playing (whether at home or away), then your Saturday
evening decision plan might be rather different :
if it is the football season and your team is playing at home then

-> go to football field and support your team


else if it is the football season and your team is playing away then
->go to wherever they are playing and support the team

else

->watch some of your teams old match videos at home

Depending on the previous example the following alternatives can be


selected as appropriate case - structure :
Case 1 : It is it is the football season and your team is playing at home
decision: go to football field and and support the team
Case 2 : it is the football season and your team is playing away
decision: go to wherever they are playing and support the team
Case 3 : any other situation
decision: watch some of your old match videos at home
It is obvious from the given example that, case construct is not as
general as else if construct ; but it is useful for implementing
some selection structures and provides better program
comprehensibility and reliability.

select case (case_expression)


case (case_selector)
block_of_statements
...
case default
block_of_statements
end select

Case expression:
either integer or character expression
Case selector:
case_value
case_expression = = case_value
low_value:
low_value <= case_expression
:high_value
case_expression <= high_value
low_value:high_value
low_value <= case_expression .and.
case_expression <= high_value

Both if and case constructs provide a means


of selecting one from a set of block of
statements
The decision criteria must not overlap in case
construct
In if construct logical expressions determine
the selection but in case construct an integer
or a character expression determines the
selection!

program exercise1
real ,parameter :: r=1e-6
real :: x
integer :: z
print *,"please type a number"
read *,x
z=x/r
select case (z)
case (1:)
print *,"your number is positive"
case (0)
print *,"your number is zero"
case (:-1)
print *,"your number is negative"
end select
end program exercise1

In many cases, we have to repeat certain


sections of a program
Many numerical methods involve
repetition/iteration
We have to have construct to repeat a group
of statements, to end the repetition, or to
restart it when certain condition is fulfilled.

A repetitive structure or loop makes


possible the repeated execution of one or
more statements called body of the loop
The repetition of a block of statements
several times is called a loop.

10

There are two basic types of repetition :

repetition controlled by a counter in


which the body of the loop is executed once for each
value of some control variable in a specified range of
values.

repetition controlled by a logical expression in


which the decision to continue or to terminate the
repetition, is determined by the value of some
logical expression.

11

Repeat the following 3 steps 21 times


considering 5oC intervals from 0oC to 100oC
without the need for any data to be read at all :
1.step : read the initial and last Celsius
Temperature

2.step : calculate the corresponding Fahrenheit


temperature for 5oC intervals
3.step : print both temperatures

12

1) Counter controlled:

do i= 0,100,5
print *, "Input temperature in Celc"
read*, Temp
end do

2) Logic controlled:

do
if( devammi == "hayir" ) then
exit
end if
print *, "Input temperature in Celc"
read*, Temp
end do

13

Various types of do loops:


do count = initial, final, inc
do count = initial, final
(inc = 1)
do

Iteration count:

How many times we will go through the loop?

Maximum iteration count: (final-initial)/inc + 1


do variable (count) is used to determine how
many times the block of statements between
do and enddo statements is to be executed.

14

do count=initial, final, inc


In this form, the loop is executed for count taking the
value initial the first time, initial+inc the next time and so
on with the value of count incremented by inc for each
loop.
1.

2. do count =initial, final


In this form, inc is absent but taken as 1, (e.g. initial+1)
and goes on similarly until count takes the value final
3. do (will be discussed later)

15

do count = initial, final, inc

block of statements

end do

loop

count : is integer variable called as "do

variable"

initial, final : are integer expressions


inc : it is optional (integer),step value

default value = 1 when omited

16

do
do
do
do
do
do
do
do

statement
iteration count
i = 1,10
10
j = 20, 50, 5
7
p = 7, 19, 4
4
q = 4, 5, 6
1
x = -20,20, 6
7
n = 25, 0, -5
6
m = 20, -20, -6
7

do variable values
1,2,3,4,5,6,7,8,9,10
20,25,30,35,40,45,50
7,11,15,19
4
-20,-14,-8,-2,4,10,16
25,20,15,10,5,0
20,14,8,2,-4,-10,-16

Maximum iteration count: (final-initial+inc)/inc

17

do number = 1, 10, 2
print *, number, number **2
end do
As OUTPUT following results will be displayed:
1
1
3
9
5
25
7
49
9
81

18

The body of a do loop may contain another do


loop. In this case,the second do loop is said to be
nested within the first do loop.

EXAMPLE :

do m = 1, 4
do n = 1, 3
product = m * n
print *, m, n, product
end do
end do

19

program Multiplication_tables
! A program to print multiplication tables from 2 to 12 times
integer :: i,j !Variable declarations
!Outer loop defines which "times table"
do i=2,12
print *, i, "times table"
do j=1,12
print *, i , "times",j, "is" ,i*j
end do
end do
end program Multiplication_tables

20

21

program CountDoExam2
integer :: outCount, outInit, outLimit, outInc, inCount, inInit, inLimit, inInc
outInit=0
outLimit=5
outInc=1
inInit =2
inLimit=4
inInc=2
do outCount = outInit, outLimit, outInc
print *, "process no :", outCount
do inCount = inInit, inLimit, inInc
print *, "outCount=", outCount, " inCount =", inCount , " inCount times
outCount =", inCount * outCount
end do
end do
end program CountDoExam2

22

In all the examples we have discussed up to now, we had


known how many times the block of statements will be
executed in a do loop (i.e. number of iteration count is
known)

But sometimes it is not possible to determine this number in


advance. e.g. a math. calculation will be terminated when some
value becomes less than a predetermined value

In this case the third form of do statement can be used


together with new statements exit and cycle which causes
either to terminate or start from the beginning of a do loop.

23

do
.
.
.
if (condition) then
exit
end if
.
.
.
end do
.

24

25

Example

program logic_cont_do
implicit none
real :: total, limit, num
integer :: step
step = 1
limit = 0.5
total = 5.5
num = 2
print *, " starting total is : ", total
print *, " lower limit is : ", limit
do
if( total < limit ) then
exit
end if
print *, "step =" , step, " total =", total
total = total - num
step=step+1
end do
end program logic_cont_do

26

[F Version 1.0970328p Copyright (c)IMAGINE1 INC 1996 & ]


[
(c)SALFORD SOFTWARE LTD 1996 & ]
[
(c)THE NUMERICAL ALGORITHMS GROUP 1996]

Imagine1 F Processor: Running 'logic_cont_do'...

starting total is :
lower limit is :
step = 1 total =
step = 2 total =
step = 3 total =

5.500000
0.500000
5.500000
3.500000
1.500000

Program completed.
Press Enter to continue.

27

As mentioned before, exit statement causes repetition of


a loop to terminate by transferring control to the
statement following the end do.
On the other hand, sometimes it is necessary to
terminate only the current repetition and then jump
ahead to the next one.
F provides the cycle statement for this purpose.

28

do
.
.
.
if (condition) then
cycle
end if
.
.
.
end do
.

29

PROBLEM : Suppose that you wanted to convert only


temperatures of 0oc and above degrees
do
.
.
if ( Celsius < 0.0 ) then
print *, given temperature must be 0 or above
else
cycle
end if
.
.
end do

30

Especially in nested doloops its very difficult


to control the transfer of the program
There may be situations when it is required to
exit from all of the nested do loops; or from some
of the do loops
For this reason it is strongly recommended to
use named do constructs by preceding the do
statement by a selected name

31

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
32

!
!
!
!

do
.
do
.
do
.
exit
.
do
.
end do
This one (1) ?
.
end do
or this one (2) ?
.
end do
or this one (3) ?
.
end do
or this one (4) ?

33

outer:
inner:

do
.
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
34

There are rare situations in which the statements used before are
inconvenient or make programming very difficult. Thus, two
additional statements exist to help us in these exceptional
situations. These are,
STOP : This statement terminates the execution without the need
to find a way of reaching the end statement of the main program
unit. This word stop causes execution of the program to be
terminated immediately.
RETURN : This statement causes a return from a procedure
without the need to find a way of reaching the end statement of
the procedure. This word return causes execution of the
procedure to be terminated immediately and control transferred
back to the program unit which called or referenced the procedure.

35

stop statement
simply terminates execution
return statement
causes execution of the procedure to be
terminated immediately and control
transferred back to the program unit which
called or referenced the procedure

36

program can_pressure
! This program calculates the pressure inside the can
real :: T, pressure
T=15.0
do
T=T+1
pressure=(0.00105*(T**2))+(0.0042*T)+1.352
if (pressure>3.2) then
exit
end if
print *,"The pressure inside the can is",pressure, &
atm. at", T," degree C"
end do
end program can_pressure

37

Imagine1 F Processor: Running 'can_pressure'...


The pressure inside the can is 1.688000 atm at 16.000000 degree C
The pressure inside the can is 1.726850 atm at 17.000000 degree C
The pressure inside the can is 1.767800 atm at 18.000000 degree C
The pressure inside the can is 1.810850 atm at 19.000000 degree C
The pressure inside the can is 1.856000 atm at 20.000000 degree C
The pressure inside the can is 1.903250 atm at 21.000000 degree C
The pressure inside the can is 1.952600 atm at 22.000000 degree C
The pressure inside the can is 2.004050 atm at 23.000000 degree C
The pressure inside the can is 2.057600 atm at 24.000000 degree C
The pressure inside the can is 2.113250 atm at 25.000000 degree C
The pressure inside the can is 2.171000 atm at 26.000000 degree C
The pressure inside the can is 2.230850 atm at 27.000000 degree C
The pressure inside the can is 2.292800 atm at 28.000000 degree C
The pressure inside the can is 2.356850 atm at 29.000000 degree C
The pressure inside the can is 2.423000 atm at 30.000000 degree C
The pressure inside the can is 2.491250 atm at 31.000000 degree C
The pressure inside the can is 2.561600 atm at 32.000000 degree C
The pressure inside the can is 2.634050 atm at 33.000000 degree C
The pressure inside the can is 2.708600 atm at 34.000000 degree C
The pressure inside the can is 2.785250 atm at 35.000000 degree C
The pressure inside the can is 2.864000 atm at 36.000000 degree C
The pressure inside the can is 2.944850 atm at 37.000000 degree C
The pressure inside the can is 3.027800 atm at 38.000000 degree C
The pressure inside the can is 3.112850 atm at 39.000000 degree C
The pressure inside the can is 3.200000 atm at 40.000000 degree C
Program completed.
Press Enter to continue.

38

Das könnte Ihnen auch gefallen