8.5: SELECT CASE Statement
- Page ID
- 54276
A SELECT CASE statement, often referred to as a CASE statement, is used to compare a given value with preselected constants and take an action according to the first constant to match. A CASE statement can be handy to select between a series of different possibilities or cases.
The select case variable or expression must be of type integer, character, or logical. A real type is not allowed. Based on the selector, a set of one or more of Fortran statements can be executed.
The general format of the SELECT CASE statement is:
select case (variable) case (selector-1) <fortran statement(s)-1> case (selector-2) <fortran statement(s)-2> . . . case (selector-n) <fortran statement(s)-n> case default <fortran statement(s)-default> end select
where <fortran statement(s)-1>
, <fortran statement(s)-2>
, <fortran statement(s)-3>
, ..., <fortran statement(s)-n>
and <fortran statement(s)-default>
are sequences of one or more executable statements. The selector-1
, selector-2
, selector-3
, ..., and selector-n
are called selector lists. Each CASE selector list may contain a list and/or range of integers, character or logical constants, whose values may not overlap within or between selectors. A selector-list is either a single or list of values, separated by commas. Each selector list must be one of the following forms.
( value ) ( value-1 : value-2 ) ( value-1 : ) ( : value-2 )
where value
, value-1
, and value-2
are constants or literals. The type of these constants must be identical to that of the selector.
In order, each selector expression is evaluated. If the variable value is the selector or in the selector range, then the sequence of statements in <fortran statement(s)>
are executed.
If the result is not in any one of the selectors, there are two possibilities:
- if CASE DEFAULT is there, then the sequence of statements in statements-DEFAULT are executed, followed by the statement following END SELECT
- if the CASE DEFAULT is not there, the statement following END SELECT is executed
The constants listed in selectors must be unique. The CASE DEFAULT is optional. But with a CASE DEFAULT, you are guaranteed that whatever the selector value, one of the labels will be used. The place for CASE DEFAULT can be anywhere within a SELECT CASE statement; however, putting it at the end would be more natural.
For example, given the declarations,
integer :: hours24, hours12, year logical :: isAM
the following case statement,
select case (hours24) case (0) hours12 = 12 isAM = .true. case (1:11) hours12 = hours24 isAM = .true. case (12) hours12 = hours24 isAM = .false. case (13:23) hours12 = hours24 - 12 isAM = .false. end select
might be useful to convert 24-hour time into 12-hour time. In this example, a logical variable isAM
is used to indicate AM (true) or PM (false).
Additionally, the selectors can be combined and separated by commas. For example, given the declarations,
integer :: monthnumber, daysinmonth
the following case statement,
select case (monthnumber) case (1,3,5,7,8,10,12) daysinmonth = 31 case (2) if (mod(year,4)==0) then daysinmonth = 29 else daysinmonth = 28 end if case (4,6,9,11) daysinmonth = 30 case default write (*,*) "Error, month number not valid." end select
might be useful to determine the number of days in a given month. The leap-year calculation is not complete, but is adequate if the range of the year is sufficiently limited.