Beruflich Dokumente
Kultur Dokumente
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
else
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
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
10
11
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
Iteration count:
14
15
block of statements
end do
loop
variable"
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
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
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
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
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
28
do
.
.
.
if (condition) then
cycle
end if
.
.
.
end do
.
29
30
31
name: do
...
end do name
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
38