IF, ELSE IF and block IF
Fortran has three conditional forms and a program uses all three in a day. The one-line IF runs a single statement; the block IF runs a group and closes with END IF; and a chain of ELSE IF chooses one of several groups. This lesson shows where each one belongs, how they nest, and the error the compiler gives when a block is opened without THEN.
The one-line IF
A condition followed by one statement is the shortest form, and it needs no THEN and no END IF:
if (score > 100) print *, "clamped high"
if (score < 0) print *, "clamped low"
Two habits keep this form honest. Put it on a line of its own, because a statement hidden at the end of a line is a statement a diff breaks. And keep one statement after the condition: Fortran 90 removed the stacked form that allowed several, and the semicolon that joins two statements is not a substitute a reviewer wants to see.
The block IF
The block form opens with if (condition) then and closes with end if. Everything between them runs when the condition is true, and nothing runs when it is false. The condition must be a logical expression; a comparison is one, and an integer is not.
The file grades.f90:
program grades
implicit none
integer :: score
score = 87
if (score >= 90) then
print *, "grade A"
else if (score >= 80) then
print *, "grade B"
else if (score >= 70) then
print *, "grade C"
else
print *, "grade F"
end if
if (score > 100) print *, "clamped high"
if (score < 0) print *, "clamped low"
print *, "score:", score
end program grades
gfortran -Wall -Wextra -std=f2018 grades.f90 -o grades
./grades
grade B
score: 87
The two one-line tests after the chain are the idiom for a sanity check that changes nothing: they print when the value is out of range and stay quiet otherwise. A reader who wants the value clamped writes an assignment in a block instead, because a one-line IF is a statement and not an expression.
ELSE IF
The chain is tested from the top down and stops at the first true condition, so the order of the tests is part of the program. The version above tests >= 90 first and >= 80 second; written the other way round, a score of 95 would fall into the second branch and read as a B.
Every ELSE IF takes its own condition, and the single ELSE at the end is optional. A chain without an ELSE does nothing when no condition holds, which is correct when the cases are exhaustive by construction and a silent hole otherwise.
When the chain tests one variable against a list of values, select case states the intent better and the compiler can check it, which is the next lesson.
Nesting
A block IF may hold another, and the indentation is what keeps the reader oriented. The program below nests two levels and prints the branch it took.
The file nested.f90:
program nested
implicit none
integer :: a, b
a = 3
b = 7
if (a > 0) then
if (b > a) then
print *, "b is the larger positive"
else
print *, "a is the larger positive"
end if
else
print *, "a is not positive"
end if
end program nested
gfortran -Wall -Wextra -std=f2018 nested.f90 -o nested
./nested
b is the larger positive
Two levels are readable, three are a function that has not been written yet. A nest that deep is a sign that the condition can be inverted and the body returned from early, or that the branches belong in a select case, and both changes shrink the indentation.
The THEN that must be there
The one-line form and the block form differ by one word, and the compiler cannot read the intent of a line that has the condition and lacks the word. Delete then from a block and the next statement is read as the body of a one-line IF, which leaves the end if with nothing to close.
The file nothen.f90:
program nothen
implicit none
integer :: n
n = 1
if (n > 0)
print *, "positive"
end if
end program nothen
gfortran -Wall -Wextra -std=f2018 nothen.f90 -o nothen
nothen.f90:5:12:
5 | if (n > 0)
| 1
Error: Syntax error in IF-clause after (1)
The caret sits at the end of the condition, where the compiler expected then. A second error follows for the end if, and that one is a consequence: the first error is the one to fix, which is the rule the lesson on diagnostics states.
Two more forms exist and new code should not use them. The arithmetic IF of FORTRAN 77 branches to one of three labels depending on the sign of a real, and it is covered with the rest of the legacy forms in the lesson on fixed form. The IF ... GOTO pair is a GOTO with a condition, and the lesson on GOTO says why a block IF replaced it.
Exercises
- Change score to 95 and run the program. Which branch runs now?
- Swap the first two tests in the chain and run it with 95. What grade does it print, and why?
- Rewrite the nested program with the conditions inverted and the bodies returned early. How many levels of indentation are left?
- Put two statements after a one-line if, separated by a semicolon. Does it build, and should it?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which form needs an END IF?
Answer
The block IF with THEN
A block IF opens a construct, so it must close; a one-line IF is a single statement.
-
What must the condition of an IF be?
Answer
A logical expression
Fortran does not convert an integer to a logical, so a comparison is required.
-
In an ELSE IF chain, which branch runs?
Answer
The first one whose condition is true
The chain is tested from the top and stops at the first true condition.
-
What error does a block IF without THEN give?
Answer
Error: Syntax error in IF-clause after (1)
The compiler reads the next statement as a one-line body and then finds an orphan END IF.
-
What does this program print?
program p implicit none integer :: n n = 5 if (n > 3) then if (n > 4) then print *, "big" else print *, "medium" end if end if end program pAnswer
big
Both conditions hold for n = 5, and the inner else belongs to the inner if.
-
Why is the order of the tests in an ELSE IF chain part of the program?
Answer
Only the first true condition runs, so a broad test first hides the narrow ones
A >= 80 test placed first catches 95 before the >= 90 branch is reached.
-
What happens to the body of a block IF when the condition is false?
Answer
None of its statements run
The whole block is skipped, and execution continues after END IF.
-
Which old form should new code avoid?
Answer
The arithmetic IF with three labels
The arithmetic IF branches to labels and belongs to the legacy forms.
-
What does
if (n) print *, ndo with an integer n?Answer
It does not build: the condition is not a logical
An integer is not a logical in Fortran, so the condition is a type error.
-
What is a sign that a nest is too deep?
Answer
Three levels of indentation that a reader has to hold in mind
Deep nesting usually means the condition should be inverted or the cases split out.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev