Fortran Execution Control SELECT CASE construct


Example

A select case construct conditionally executes one block of constructs or statements depending on the value of a scalar expression in a select case statement. This control construct can be considered as a replacement for computed goto.

[name:] SELECT CASE (expr)
[CASE (case-value [, case-value] ...) [name]
   block]...
[CASE DEFAULT [name]
   block]
END SELECT [name]

where,

  • name - the name of the select case construct (optional)
  • expr - a scalar expression of type integer, logical, or character (enclosed in parentheses)
  • case-value - one or more scalar integer, logical, or character initialization expressions enclosed in parentheses
  • block - a sequence of zero or more statements or constructs

Examples:

! simplest form of select case construct
select case(i)
case(:-1)
    s = -1
case(0)
    s = 0
case(1:)
    s = 1
case default
    print "Something strange is happened"
end select

In this example, (:-1) case value is a range of values matches to all values less than zero, (0) matches to zeroes, and (1:) matches to all values above zero, default section involves if other sections did not executed.