UNIHIVE

Study Hub UNIPORT ENG 213.1

ENG 213.1 · Computer Programming for Engineers

Decisions: IF and SELECT CASE

Relational and logical operators, IF, ELSE IF and SELECT CASE, with engineering examples.

Our notes

1 min read

Relational operators

ModernOld formMeaning
==.EQ.equal
/=.NE.not equal
<.LT.less than
<=.LE.less or equal
>.GT.greater
>=.GE.greater or equal

Logical operators: .and., .or., .not.

The IF block

if (condition) then
  statements
else if (condition2) then
  statements
else
  statements
end if
A one-line IF: if (x < 0.0) x = 0.0

Worked example: flow regime

program regime
  implicit none
  real :: rho, v, d, mu, re
  rho = 1000.0
  v = 2.0
  d = 0.05
  mu = 1.0e-3
  re = rho * v * d / mu
  if (re < 2300.0) then
    print *, 'Laminar, Re =', re
  else if (re <= 4000.0) then
    print *, 'Transitional, Re =', re
  else
    print *, 'Turbulent, Re =', re
  end if
end program regime
Re = 1000 × 2 × 0.05 / 0.001 = 100,000, so it prints Turbulent. (The limits 2300 and 4000 are the usual teaching values for pipe flow.)

Worked example: quadratic with checks

if (a == 0.0) then
  print *, 'Not a quadratic'
else
  disc = b**2 - 4.0 * a * c
  if (disc >= 0.0) then
    x1 = (-b + sqrt(disc)) / (2.0 * a)
    x2 = (-b - sqrt(disc)) / (2.0 * a)
    print *, x1, x2
  else
    print *, 'Complex roots'
  end if
end if

SELECT CASE

For choices on an integer, character or logical value:
select case (grade_point)
case (5)
  print *, 'A'
case (4)
  print *, 'B'
case (0:3)
  print *, 'C or below'
case default
  print *, 'Invalid'
end select
(0:3) means 0 to 3. SELECT CASE cannot test real numbers.

Comparing real numbers

Real arithmetic has rounding, so 0.1 + 0.2 == 0.3 may be false. Compare with a tolerance:
if (abs(x - y) < 1.0e-6) then

Exams usually ask

  • Write an IF block for a classification problem (grades, flow regime, safe or unsafe stress).
  • Trace nested IFs for given inputs.

Common mistake

Testing real numbers for exact equality. Use a small tolerance.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All ENG 213.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.