FORTRAN 90

advertisement
FORTRAN 90
Lecturer: Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.
Control Constructs
The CASE Construct
The CASE construct is similar to IF. It allows selection between a number of
situations or cases, based on a selector. In such cases it is more convenient than IF.
The general form of CASE is
SELECT CASE (expression)
CASE (value)
block1
CASE (value)
block2
CASE DEFAULT
block 3
END SELECT
The result of expression may be of type character, logical or integer; value
must be of the same type as the result of expression and can be any combination
of:
- A single integer, character, or logical depending on type.
- min: any value from a minimum value upwards.
- :max any value from a maximum value downwards.
- min : max any value between the two limits.
CASE DEFAULT is optional and covers all other possible values of the
expression not already covered by other CASE statements.
EX.
program fluid_flow
real:: v,r,m,d,re
read(*,*) v,r,m,d
ϭ
re=(r*v*d)/m
select case (INT(re
case ( :2200)
write(*,*) "THE FLOW IS LAMINAR ","RENOLD NO.=",RE
case (2201:4000)
write(*,*) "THE FLOW IS TRANSITION ","RENOLD NO.=",RE
case default
write(*,*) "THE FLOW IS TURBULENT ","RENOLD NO.=",RE
end select
end
EX.
Implicit none
Integer:: y,x
Read(*,*) y
Select case (y>0)
Case(.true.)
X=y**2
Write(*,*) 'x=',x
Case (.false.)
X=-y
Write(*,*) 'x=',x
End select
End
EX.
program monthh
implicit none
integer:: month,year
read(*,*) month,year
Select case (month)
Case(9,4,6,11)
Write(*,*)"number of days=30
Case (1,3,5,7:8,10,12)
Write(*,*)"number of days=31
Case (2)
Ϯ
If (mod(year,4)==0)then
Write(*,*)"number of days=29
else
Write(*,*)"number of days=28
Endif
Case default
Write(*,*) month, "is not a number of month
End select
end
GOTO
The GOTO statement can be used to transfer control to another statement, it has
the form:
GOTO label
The GOTO statement simply transfers control to the statement with the
corresponding label. For example:
...
IF( x<10 ) GOTO 10
...
10 STOP
The GOTO statement should be avoided where ever possible, programs
containing such statements are notoriously hard to follow and maintain.
ϯ
Download