Fortran Select Case
When you compare one value against many possibilities, a select case construct is clearer than a long if … else if chain. The selector must be an integer, character or logical expression (not a real).
program weekday
implicit none
integer :: day
do day = 1, 7
select case (day)
case (1)
print '(i0, a)', day, ': Monday'
case (2, 3, 4)
print '(i0, a)', day, ': midweek'
case (5)
print '(i0, a)', day, ': Friday'
case default
print '(i0, a)', day, ': weekend'
end select
end do
end program weekday1: Monday
2: midweek
3: midweek
4: midweek
5: Friday
6: weekend
7: weekend
- Each
caselists one or more values, separated by commas. case defaultruns when no other case matches. It is optional.- Unlike C's
switch, there is no fall-through: after one case runs, control jumps toend select. Nobreakis needed. - The case values must not overlap; the compiler reports an error if two cases can match the same value.
Ranges
A case value can be a range written with a colon: low:high. Either end may be left open: :9 means "9 or less" and 90: means "90 or more".
program bmi_classes
implicit none
integer :: bmi_values(5) = [17, 22, 27, 33, 41]
integer :: i
character(len=:), allocatable :: label
do i = 1, size(bmi_values)
select case (bmi_values(i))
case (:18)
label = 'underweight'
case (19:24)
label = 'normal'
case (25:29)
label = 'overweight'
case (30:)
label = 'obese'
end select
print '(a, i0, a, a)', 'BMI ', bmi_values(i), ': ', label
end do
end program bmi_classesBMI 17: underweight
BMI 22: normal
BMI 27: overweight
BMI 33: obese
BMI 41: obese
character(len=:), allocatable declares a string whose length adapts to whatever you assign. It is explained in the strings lessons.
Selecting on strings
Character selectors are compared exactly (trailing blanks are ignored), and ranges compare alphabetically:
program commands
implicit none
character(len=8) :: cmds(5) = [character(len=8) :: 'start', 'stop', 'pause', 'help', 'reboot']
integer :: i
do i = 1, size(cmds)
select case (trim(cmds(i)))
case ('start', 'resume')
print '(a)', 'Starting the engine'
case ('stop')
print '(a)', 'Stopping'
case ('pause')
print '(a)', 'Pausing'
case ('help')
print '(a)', 'Commands: start, stop, pause, help'
case default
print '(3a)', 'Unknown command "', trim(cmds(i)), '"'
end select
end do
end program commandsStarting the engine
Stopping
Pausing
Commands: start, stop, pause, help
Unknown command "reboot"
Selection is case sensitive: 'Start' would not match 'start'.
Character ranges
program classify_chars
implicit none
character(len=*), parameter :: text = 'R2-d2!'
integer :: i
do i = 1, len(text)
select case (text(i:i))
case ('a':'z')
print '(a, a)', text(i:i), ' lower-case letter'
case ('A':'Z')
print '(a, a)', text(i:i), ' upper-case letter'
case ('0':'9')
print '(a, a)', text(i:i), ' digit'
case default
print '(a, a)', text(i:i), ' other'
end select
end do
end program classify_charsR upper-case letter
2 digit
- other
d lower-case letter
2 digit
! other
text(i:i) is a substring: the single character at position i.
Named select
Like if, a select case can be named, which helps when constructs are nested:
size_check: select case (n)
case (0)
print *, 'empty'
case default
print *, 'has data'
end select size_checkTip: Reals cannot be selectors because exact comparison of floating-point values is unreliable. To classify a real, compute an integer first, for example
select case (nint(x))orselect case (floor(x / 10.0)).
Test yourself
What happens after a matching case block finishes?
- The next case runs too (fall-through)
- Control jumps to
end select - The select restarts
- You must write
exit
Which case matches every value of 10 or more?
case (10:)case (:10)case (>= 10)case (10..)
Which type is NOT allowed as a select case selector?
- integer
- character
- logical
- real