Professional Documents
Culture Documents
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
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?