Fortran If / Else
The if construct runs a block of statements only when a logical condition is true. Conditions are written in parentheses, and every block construct ends with its own end keyword.
if … then … end if
program simple_if
implicit none
real :: temperature = 31.5
if (temperature > 30.0) then
print '(a)', 'It is hot today.'
print '(a)', 'Remember to drink water.'
end if
print '(a)', 'Weather report finished.'
end program simple_ifIt is hot today.
Remember to drink water.
Weather report finished.
else and else if
Add else for the alternative, and any number of else if branches to test more conditions. The first branch whose condition is true runs, and the rest are skipped.
program grades
implicit none
integer :: scores(5) = [95, 83, 71, 64, 40]
character :: grade
integer :: i
do i = 1, size(scores)
if (scores(i) >= 90) then
grade = 'A'
else if (scores(i) >= 80) then
grade = 'B'
else if (scores(i) >= 70) then
grade = 'C'
else if (scores(i) >= 60) then
grade = 'D'
else
grade = 'F'
end if
print '(a, i3, a, a)', 'Score', scores(i), ' -> grade ', grade
end do
end program gradesScore 95 -> grade A
Score 83 -> grade B
Score 71 -> grade C
Score 64 -> grade D
Score 40 -> grade F
Because the branches are tested in order, the second condition only needs >= 80: any score of 90 or more was already handled.
Note:
else ifcan also be writtenelseif, andend ifasendif. Both spellings are standard.
The one-line if
When there is a single statement to run and no else, the logical if statement is shorter. There is no then and no end if:
program one_line_if
implicit none
integer :: balance = -25
if (balance < 0) print '(a)', 'Warning: account overdrawn'
if (balance < 0) balance = 0
print '(a, i0)', 'Balance now: ', balance
end program one_line_ifWarning: account overdrawn
Balance now: 0
Combining conditions
Use .and., .or. and .not. to build complex conditions. Parentheses make the intent clear:
program leap_years
implicit none
integer :: years(6) = [1900, 2000, 2023, 2024, 2100, 2400]
integer :: i
logical :: leap
do i = 1, size(years)
leap = (mod(years(i), 4) == 0 .and. mod(years(i), 100) /= 0) &
.or. mod(years(i), 400) == 0
if (leap) then
print '(i0, a)', years(i), ' is a leap year'
else
print '(i0, a)', years(i), ' is not a leap year'
end if
end do
end program leap_years1900 is not a leap year
2000 is a leap year
2023 is not a leap year
2024 is a leap year
2100 is not a leap year
2400 is a leap year
Named constructs and nesting
Constructs can be nested, and any construct can be given a name. The name goes before the if with a colon and must be repeated at the end if; it helps readers match the ends of long blocks.
program named_if
implicit none
real :: a = 1.0, b = -3.0, c = 2.0 ! a*x**2 + b*x + c = 0
real :: disc
disc = b**2 - 4.0*a*c
roots: if (disc > 0.0) then
print '(a, 2f8.3)', 'Two real roots:', (-b + sqrt(disc)) / (2*a), (-b - sqrt(disc)) / (2*a)
else if (disc == 0.0) then roots
print '(a, f8.3)', 'One double root:', -b / (2*a)
else roots
print '(a)', 'Complex roots'
end if roots
end program named_ifTwo real roots: 2.000 1.000
Comparing reals
Floating-point numbers are rarely exactly equal after a calculation. Compare with a tolerance instead of ==:
program float_compare
use iso_fortran_env, only: real64
implicit none
real(real64) :: x
x = 0.1_real64 + 0.2_real64
if (x == 0.3_real64) then
print '(a)', 'exactly equal'
else
print '(a, es10.2)', 'not equal, difference = ', x - 0.3_real64
end if
if (abs(x - 0.3_real64) < 1.0e-12_real64) print '(a)', 'equal within tolerance'
end program float_comparenot equal, difference = 5.55E-17
equal within tolerance
Test yourself
Which keyword must follow the condition of a block if?
dothenbegin:
With x = 85, which branch runs in if (x >= 70) … else if (x >= 80) … end if?
- The first one
- The second one
- Both
- Neither
What is the safest way to test whether two computed reals are equal?
a == ba .eq. babs(a - b) < toleranceint(a) == int(b)