ENG 213.1 · Computer Programming for Engineers
Our notes
1 min readRelational operators
| Modern | Old form | Meaning |
|---|---|---|
| == | .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
-
Best pick Introduction to Python Programming, 4.5 Chained decisions
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 4.5 Chained decisions, CC BY-NC-SA 4.0 · licence
-
Conditional (computer programming)
(opens en.wikipedia.org in a new tab)
Wikipedia, Conditional (computer programming), CC BY-SA 4.0 · licence
-
Switch statement
(opens en.wikipedia.org in a new tab)
Wikipedia, Switch statement, CC BY-SA 4.0 · licence
-
OpenStax, Introduction to Python Programming (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Python Programming (whole free textbook), CC BY-NC-SA 4.0 · licence
Watch
-
FORTRAN TUTORIAL 7 (SELECT CASE)
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
Conditional Statements in FORTRAN - If and If-then-Else | Tutorial - 8
8 min · ≈ 36 MB · about ₦20
(opens youtube.com in a new tab)
-
FORTRAN 09: Making Decisions with the Select/Case Statement
17 min · ≈ 77 MB · about ₦43
(opens youtube.com in a new tab)
-
Fortran Conditional Statements | Modern Fortran Course - Lesson 1.3
10 min · ≈ 45 MB · about ₦25
(opens youtube.com in a new tab)
-
Fortran Programming Tutorials (Revised) : 030 : More on Select Case, nested if
17 min · ≈ 77 MB · about ₦43
(opens youtube.com in a new tab)
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.