You are on page 1of 19

IF Construct

 Conditionally executes one block of statements


depending on the evaluation of a logical
expression
IF (expr) THEN
block
[ELSE IF (expr) THEN
block] ...
[ELSE
block]
END IF
 expr is a logical expression
CP 260 Lectures - Fortran 2
IF Construct – single decision point
 The First (lowest) level IF has single statement to
be implemented ONLY when the logical expression
is TRUE.
 It has only single output
 It takes the following format

IF (expr) statements to be implemented

Example 1
IF (age>50) write(*,*) ‘ Not Too Old to be an
Undergraduate Student’
CP 260 Lectures - Fortran 3
IF Construct – single decision point
 The second level IF has multiple statements to be
implemented ONLY when the logical expression is TRUE
 It can have single or multiple outputs
 It takes the following format
IF (expr) THEN
statements to be implemented
END IF

Example 2
IF (age>50) THEN Two
write(*,*) ‘ Too Old to be an Employee’ statements,
one output
time_to_retire=60-age
END IF
CP 260 Lectures - Fortran 4
IF Construct – single decision point
 The second level IF has multiple statements to be
implemented ONLY when the logical expression is TRUE
 It can have single or multiple outputs
 It takes the following format
IF (expr) THEN
statements to be implemented
END IF

Example 3
IF (age>50) THEN
write(*,*) ‘ Too Old to be an Employee’ Three
statements,
x=60-age % time_to_retire two output
write(*,*), ‘You have’, x, ‘years before retirement’
END IF
CP 260 Lectures - Fortran 5
IF Construct – single decision point
 Third level IF has multiple statements to be implemented
when the logical expression is TRUE and multiple statements
to be implemented when logical expression is FALSE
IF (expr) THEN
statements to be implemented
ELSE
statements to be implemented
END IF

Example 4
IF (age>50) THEN
write(*,*) ‘ Too Old to be an Employee’
time_to_retire=60-age
ELSE
write(*,*)’You are too young to retire’
END IF CP 260 Lectures - Fortran 6
IF Construct – multiple decision points
 The fourth level IF has multiple decisions /
selections, with a combination of IF & ELSEIF

IF (expr) THEN
statements to be implemented
ELSE IF (expr) THEN
statements to be implemented
ELSE IF (expr) THEN
statements to be implemented
ELSE
statements to be implemented
END IF
CP 260 Lectures - Fortran 7
140 Read(*,*) marks
Statement If (marks>70) then
Label
Print*, ’Marks = ‘, marks, ‘A’
Else if (marks>60) then
Print*, ’Marks = ‘, marks, ‘B+’
Elseif(marks>50) then
Print*, ’Marks= ‘, marks, ‘B’
Elseif(marks>40) then
Print*, ’Marks= ‘, marks, ’C’
Else
Print*, ’Marks= ‘, marks, ’D’
Endif
Write(*,*) ‘ Are there more marks?
Read(*,*) resp Jump to
If(resp==‘Y’) goto 140 Statement
Labeled 140
CP 260 Lectures - Fortran 8
Logical Operators
 construct logical expressions with these operators
if ( (x<y) .AND. (y<z)) then
r = sqrt(y**2 – x**2)
end if

Operator Example Meaning


.AND. a .AND. b expression is true if both a and b are true
.OR. a . OR. b expression is true if either a, b, or both, are
true
.EQV. a . EQV. b expression is true if both a and b are true,
or both are false
.NEQV. a . NEQV. b expression is true if either a or b is true,
but false if both are true
.NOT. .NOT. b expression is true if b is false and false if b
is true
CP 260 Lectures - Fortran 9
Example 5
IF Statement – Roots of a Quadratic equation
 Start
 Enter coefficients a, b and c of quadratic equation
 Compute the value of determinant: d=(b*b)-4*a*c
 Check whether the value of d is greater than or equal to
zero,
 If d is greater or equal to zero, this gives REAL roots;
 Calculate 1st root; x1=(-b+sqrt(d))/2*a
 Calculate 2nd root; x2=(-b-sqrt(d))/2*a

 If d is less than zero, this gives COMPLEX roots;


 Calculate 1st complex root: x1=(-b+sqrt(-1*d))/2*a
 Calculate 2nd complex root: x2=(-b-sqrt(-1*d))/2*a

 Display roots x1 and x2


 Stop

CP 260 Lectures - Fortran 10


IF Statement – Roots of a Quadratic equation

CP 260 Lectures - Fortran 11


IF Statement – Roots of a
Quadratic equation

CP 260 Lectures - Fortran 12


Example 6: Pipe flow Entry length
 A processing plant has four (4) different sizes of pipes that can
be used for distributing water in the factory. Internal diameters
of these pipes are shown in Table 1. The possible water (at 30oC)
mass flow rate that can flow through the pipes is as shown in
Table 1. The designer would like to establish a diameter/
flowrate combination that gives the shortest Entrance Length,
Le, which is calculated using equations (1) or (2).
𝐿𝑒
≈ 0.06𝑅𝑒𝑑 𝑓𝑜𝑟 𝐿𝑎𝑚𝑖𝑛𝑎𝑟 𝑓𝑙𝑜𝑤 (1)
𝑑
𝐿𝑒 1/6
≈ 4.4𝑅𝑒𝑑 𝑓𝑜𝑟 𝑇𝑢𝑟𝑏𝑢𝑙𝑒𝑛𝑡 𝑓𝑙𝑜𝑤 (2)
𝑑
where Red = Reynolds number based on the piper diameter:
𝑢𝑑𝜌
𝑅𝑒𝑑 = (3)
𝜇
where u=fluid velocity, d=piper internal diameter, ρ=fluid density,
and µ=fluid dynamic viscosity
CP 260 Lectures - Fortran 13
Example 6: Pipe flow Entry length
S/N Available pipes internal Required Water flow
diameter [mm] rates [kg/s]
1 12.7 0.05
2 25.4 0.10
3 38.1 0.20
4 50.4 0.30
5 0.50
6 1.00

Assuming the transition region follows the turbulent flow equation (Eq. 2);
write a Fortran90 program that:
a) Calculates the fluid velocity, Reynolds number and Entry length for
each of the pipes for all flow rates
b) Creates a Table showing all the pipe diameters, water flow rates, fluid
velocities, Reynolds numbers, and corresponding Entry length.
c) The Table in (b) should be written in a File.
d) From the Table in (b) above, what is the shortest Le? What is the
diameter/flowrate combination which gives this value?

CP 260 Lectures - Fortran 14


Example 6: Pipe flow Entry length
Program entry_length
real:: Re(10,10), Le(10,10),den, d_visc,d(10)
real:: pi, Temp,Q(10),u(10,10)
integer:: i, j
character:: fluid*20,rem(10,10)*10
parameter (pi=3.14159265359)
open(12,file='Entry_length.txt',access='append')
write(*,*) 'Enter name of fluid'
read(*,*) fluid
write(*,*) 'Enter number of pipe diameters available'
read(*,*) m
write(*,*) 'Enter number of fluid flow rates available'
read(*,*) n
write(*,*) 'Enter fluid temperature [Centigrade]'
read(*,*) Temp
write(*,'(2x,a,f5.1,2x,a)') 'Enter fluid dynamic viscosity [kg/ms] at ', Temp,
'Centigrade'
read(*,*) d_visc
write(*,'(2x,a,f5.1,2x,a)') 'Enter fluid density [kg/m3] at ', Temp, 'Centigrade‘
read(*,*) den CP 260 Lectures - Fortran 15
Do i=1,m
write(*,*) 'Enter pipe diameter [mm]', i
read(*,*) d(i)
end do
Do j=1,n
write(*,*) 'Enter fluid flow rate [kg/s]', j
read(*,*) Q(j)
end do
Do i=1,m
do j=1,n
u(i,j)=4*Q(j)/(pi*den*(d(i)/1000)**2)
Re(i,j)=u(i,j)*(d(i)/1000)*den/d_visc
if (Re(i,j)<2100) then
Le(i,j)=0.06*d(i)*Re(i,j)
rem(i,j)='Laminar'
elseif (Re(i,j)<10000) then
Le(i,j)=4.4*d(i)*Re(i,j)**(1./6.)
rem(i,j)='Transition'
else
Le(i,j)=4.4*d(i)*Re(i,j)**(1./6.)
rem(i,j)='Turbulent'
endif
end do
end do CP 260 Lectures - Fortran 16
write(12,'(x,a,x,a)')'Fluid Type:',fluid
write(12,*)
write(12,'(x,a,x,f5.1)')'Fluid Temperature [Centigrade]:',temp
write(12,*)
write(12,'(x,a,x,f10.2)')'Density of the Fluid at the given Temperature [kg/m3]:',den
write(12,*)
write(12,'(x,a,x,f10.6)')'Dynamic Viscosity of the Fluid at the given Temperature
[kg/ms]:',d_visc
write(12,*)
write(12,*)
write(12,*)'Diameter Flow rate Velocity Reynolds Number Entry Length Remarks'
write(12,*)' [mm] [kg/s] [m/s] [] [mm]'
write(12,*)'________________________________________________________________'
do i=1,m
write(12,20) d(i)
do j=1,n
write(12,30) Q(j),u(i,j),Re(i,j),Le(i,j),rem(i,j)
end do
end do
write(12,*)
write(12,*)
20 format(4x,f5.2)
30 format (12x,f5.2,2x,f10.2,6x,f10.2,5x,f10.2,6x,a)
end CP 260 Lectures - Fortran 17
Output
Fluid Type: WATER
Fluid Temperature [Centigrade]: 30.0
Density of the Fluid at the given Temperature [kg/m3]: 995.71
Dynamic Viscosity of the Fluid at the given Temperature
[kg/ms]: .000798

CP 260 Lectures - Fortran 18


Output Diameter Flow rate Velocity Reynolds Number Entry Length Remarks
[mm]
_____
[kg/s]
______
[m/s] []
______ ___________ ________
[mm]
________
12.7
0.05 0.4 6281.65 240.03 Transition
0.1 0.79 12563.29 269.43 Turbulent
0.2 1.59 25126.59 302.42 Turbulent
0.3 2.38 37689.88 323.56 Turbulent
0.5 3.96 62816.46 352.32 Turbulent
1 7.93 125632.9 395.46 Turbulent
25.4
0.05 0.1 3140.82 427.69 Transition
0.1 0.2 6281.65 480.06 Transition
0.2 0.4 12563.29 538.85 Turbulent
0.3 0.59 18844.94 576.53 Turbulent
0.5 0.99 31408.23 627.76 Turbulent
1 1.98 62816.46 704.64 Turbulent
38.1
0.05 0.04 2093.88 4786.61 Laminar
0.1 0.09 4187.76 673.04 Transition
0.2 0.18 8375.53 755.46 Transition
0.3 0.26 12563.29 808.28 Turbulent
0.5 0.44 20938.82 880.11 Turbulent
1 0.88 41877.64 987.89 Turbulent
50.4
0.05 0.03 1582.88 4786.61 Laminar
0.1 0.05 3165.75 849.76 Transition
0.2 0.1 6331.5 953.82 Transition
0.3 0.15 9497.25 1020.51 Transition
0.5 0.25 15828.75 1111.2 Turbulent
1 0.5 31657.5 1247.28 Turbulent

CP 260 Lectures - Fortran 19

You might also like