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:

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:

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

  1. Add case (0) before case (1, 7) and select 0. Which branch runs?
  2. Change the range to case (2:5) and select 6. Which branch runs now?
  3. Move case default to the top of the integer construct. Does the output change?
  4. 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.

  1. 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.

  2. Which values does case (2:6) match?

    Answer

    2, 3, 4, 5 and 6

    Both ends of the range are included.

  3. Which types may a selector have?

    Answer

    Integer, character or logical

    A real selector is refused because equality between reals is not reliable.

  4. Where may case default appear?

    Answer

    Anywhere among the cases

    The default is a case like the others and its position does not matter.

  5. 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 p

    Answer

    mid

    n is 3, and the second case covers 3 and 4.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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