SELECT CASE and CASE ranges
When a chain of ELSE IF tests one value against a list, the test is not a condition but a set membership, and Fortran has a construct for it. SELECT CASE names the value once, writes one branch per set, and lets the compiler check that the branches are of a type the selector can match. This lesson covers the three shapes of a case, the default, and the character selector.
The form
The construct opens with select case (selector) and closes with end select. Each branch starts with case (...) and runs until the next branch. The selector is evaluated once, at entry, which is the difference from an ELSE IF chain that repeats the expression.
The file cases.f90:
program cases
implicit none
integer :: n
character(len=5) :: day
n = 7
select case (n)
case (1, 7)
print *, "weekend"
case (2:6)
print *, "weekday"
case default
print *, "out of range"
end select
day = "sun"
select case (trim(day))
case ("sat", "sun")
print *, "weekend day"
case default
print *, "weekday day"
end select
end program cases
gfortran -Wall -Wextra -std=f2018 cases.f90 -o cases
./cases
weekend
weekend day
A value, a range and a list
A case holds one or more selectors, separated by commas, and each selector is one of three things:
- A single value: case (0) matches zero and nothing else.
- A range with a colon: case (2:6) matches two, three, four, five and six, both ends included. An open range is allowed, so case (:-1) matches everything below zero and case (10:) everything from ten up.
- Any mix of the two in one list: case (1, 7, 9:12) matches one, seven and the four values of the range.
The compiler checks the ranges it can see. Two cases that overlap are not an error in general, because the first one wins, but they are almost always a mistake: a value that two branches claim makes the second branch dead code, and nothing in the build says so.
CASE DEFAULT
case default runs when no other case matched. It may appear anywhere among the branches, not only at the end, and it may be omitted: a construct without it does nothing for an unmatched value, which is right when the cases cover the domain and a silent hole otherwise.
A select case over an enumerated set is where the construct earns its place. The branches then read as a table of the values the program knows, and a reader can see at a glance whether the default is reachable.
A character selector
The second construct in the program selects on a string. Two details make it work:
- The selector is trim(day) and not day. The case values are literals of three characters, and a five character selector would be padded for the comparison; the padding happens to give the same answer here, but only because the case values have no trailing blanks of their own. Trimming the selector makes the intent visible.
- The case values are a list of strings, and the comparison is the character comparison of the previous lesson: case sensitive, padded to a common length.
A character selector with a default is the usual shape of a command line parser: one case per option letter and a default that prints the usage message.
What a selector may be
The standard allows an integer, a character or a logical selector, and nothing else. A real is refused, because two reals that a reader calls equal may differ in the last bit and the case would then miss for a reason nobody can see. The program below tries it.
The file realcase.f90:
program realcase
implicit none
real :: x
x = 1.5
select case (x)
case (1.0)
print *, "one"
case default
print *, "other"
end select
end program realcase
gfortran -Wall -Wextra -std=f2018 realcase.f90 -o realcase
realcase.f90:6:8:
6 | case (1.0)
| 1
Error: Expression in CASE selector at (1) cannot be REAL(4)
realcase.f90:7:18:
7 | print *, "one"
| 1
Error: Expected a CASE or END SELECT statement following SELECT CASE at (1)
realcase.f90:5:15:
5 | select case (x)
| 1
Error: Argument of SELECT statement at (1) cannot be REAL(4)
The three errors arrive in an order that is worth noticing: the case first, the branch it leaves orphaned second, and the selector last. Only the third names the real cause, and it is the one a reader who stops at the first message never sees. A program that needs to branch on a real converts to an integer with the rounding it means, or compares with a tolerance in an IF.
Exercises
- Add case (0) before case (1, 7) and select 0. Which branch runs?
- Change the range to case (2:5) and select 6. Which branch runs now?
- Move case default to the top of the integer construct. Does the output change?
- Select on day without trim and with a value of "sat ". Does the branch still match?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many times is the selector of a SELECT CASE evaluated?
Answer
Once, at entry
The selector is computed once and compared with each case in turn.
-
Which values does
case (2:6)match?Answer
2, 3, 4, 5 and 6
Both ends of the range are included.
-
Which types may a selector have?
Answer
Integer, character or logical
A real selector is refused because equality between reals is not reliable.
-
Where may
case defaultappear?Answer
Anywhere among the cases
The default is a case like the others and its position does not matter.
-
What does this program print?
program p implicit none integer :: n n = 3 select case (n) case (1:2) print *, "low" case (3:4) print *, "mid" case default print *, "high" end select end program pAnswer
mid
n is 3, and the second case covers 3 and 4.
-
What error does a real selector give?
Answer
Error: Argument of SELECT statement at (1) cannot be REAL(4)
The selector type is checked when the construct is entered.
-
Why trim the selector of a character construct?
Answer
It removes the padding so the compared values are the ones meant
A padded selector can match or miss a case for a reason a reader cannot see.
-
What happens when no case matches and there is no default?
Answer
The construct is skipped and execution continues after END SELECT
A construct without a default does nothing for an unmatched value.
-
Which is better style for one variable against a list of values?
Answer
SELECT CASE
The construct states that one value is being matched and checks the types.
-
What does
case (1, 7, 9:12)match?Answer
Six values: 1, 7, 9, 10, 11, 12
The list takes the single values and the whole range.
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