Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 39 ยท Control Flow

The select case Statement

Use Fortran's select case to choose between many alternatives: single values, lists, ranges, case default, character selectors, and why there is no fall-through.

When one value decides among many actions, a long else if ladder gets noisy. Fortran offers select case, a multi-way branch that matches a value against case selectors: single values, lists of values and ranges. It works with integer, character and logical expressions, and unlike C's switch, only one case ever runs, so there is no break and no accidental fall-through.

Basic form

FORTRAN
program weekday_demo
  implicit none
  integer :: day

  day = 3
  select case (day)
  case (1)
    print *, "Monday"
  case (2)
    print *, "Tuesday"
  case (3)
    print *, "Wednesday"
  case default
    print *, "Some other day"
  end select
end program weekday_demo

For day = 3 the program prints Wednesday. Parts:

  • select case (expression) evaluates the expression once.
  • Each case (selector) lists what it matches.
  • case default runs when nothing else matches. It is optional but you should include it, even if only to report an error.
  • end select closes the construct.

A case selector is checked against the value; the cases may appear in any order and may not overlap.

flowchart TD
  A["select case (day)"] --> B{"match?"}
  B -->|"1"| C["Monday"]
  B -->|"2"| D["Tuesday"]
  B -->|"3"| E["Wednesday"]
  B -->|"none"| F["case default"]

Exactly one branch runs; control then continues after end select.

Lists and ranges

A single case can match several values, a range, or both:

FORTRAN
select case (score)
case (90:100)
  grade = "A"
case (80:89)
  grade = "B"
case (70:79)
  grade = "C"
case (0:69)
  grade = "F"
case default
  grade = "?"          ! outside 0..100
end select
Selector Matches
case (5) exactly 5
case (1, 3, 5) any of 1, 3 or 5
case (10:20) 10 through 20, both included
case (:0) 0 and everything below
case (100:) 100 and everything above
case (1:3, 7, 9:) a mix of ranges and values

Open-ended ranges (:0 and 100:) are the elegant way to cover everything on one side without picking an arbitrary limit.

Python Stand-in (Python): case ranges with match

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Selecting on characters

Character selectors work too. The comparison is by the character collating sequence (ASCII for English text), and shorter strings are padded with blanks:

FORTRAN
character(len=1) :: ch
ch = "e"
select case (ch)
case ("a", "e", "i", "o", "u")
  print *, "vowel"
case ("b":"d", "f":"h", "j":"n", "p":"t", "v":"z")
  print *, "consonant"
case default
  print *, "not a lowercase letter"
end select

This prints vowel. The cases must not overlap: you cannot list "e" in a vowel case and again inside "a":"z", so the consonant ranges above skip the vowels.

Whole strings can be selected: select case (command) with case ("start"), case ("stop"). Strings are compared with blank padding, so "stop" and "stop " match.

Python Stand-in (Python): vowels and consonants

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Logical selectors and no fall-through

A logical expression can be selected on with .true. and .false., though an if is clearer for two outcomes. More important is what select case does not do: when a case finishes, control leaves the construct. C programmers must write break; Fortran programmers never do. If you want two values to run the same code, list both in one case: case (6, 7).

Real numbers are not allowed

The selector expression must be integer, character or logical. A real is rejected, since matching floating-point numbers exactly would be unreliable. Convert to integer first, with care, or use an if ladder:

FORTRAN
select case (nint(temperature / 10.0))
case (:0)
  print *, "freezing"
case (1:2)
  print *, "cool"
case default
  print *, "warm"
end select

Named select constructs and exiting early

Like if and do, a select case can be named, and an exit with that name leaves it from inside a nested block:

FORTRAN
router: select case (code)
case (1)
  if (.not. ready) exit router
  call process()
case default
  print *, "unknown"
end select router

Exit, cycle and named constructs covers this in detail.

A worked example: a tiny calculator

FORTRAN
program calc
  implicit none
  character(len=1) :: op
  real :: a, b, result

  a = 12.0; b = 4.0; op = "/"
  select case (op)
  case ("+")
    result = a + b
  case ("-")
    result = a - b
  case ("*")
    result = a * b
  case ("/")
    if (b == 0.0) then
      print *, "Division by zero"
      stop
    end if
    result = a / b
  case default
    print *, "Unknown operator: ", op
    stop
  end select
  print *, a, op, b, "=", result
end program calc

With these inputs the program prints a line ending in = 3.0 (digits vary by compiler). stop ends the program early.

Python Stand-in (Python): the calculator dispatch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does case (:0) match?

    An open-ended range with no lower limit matches that upper value and everything below.
  2. 2. Which type is NOT allowed as a select case expression?

    Only integer, character and logical expressions can be selected on; reals are not allowed.
  3. 3. How do you make two values run the same code?

    There is no fall-through; put several values in one selector list.
  4. 4. What runs when no case matches and there is no case default?

    Without a default, an unmatched value simply skips the whole construct, which is why a default branch is good practice.

Summary

  • select case (expr) picks one case and then leaves; there is no break and no fall-through.
  • Selectors can be single values, lists, ranges (10:20, :0, 100:) or mixtures.
  • Selectors must not overlap; case default handles the rest.
  • Works with integer, character and logical expressions, not reals.
  • The construct can be named and left with exit.

Next: do Loops