LF Fortran 95

CASE Construct

Description

The CASE construct selects blocks of executable code based on the value of an expression. A default case may be provided.

The SELECT CASE statement signals the beginning of a CASE construct. It contains an expression that, when evaluated, produces a case index. The case index in the CASE construct determines which block in a CASE construct, if any, is executed.

The CASE statement defines a case selector which, when matched with the value from a SELECT CASE statement, causes the following block of code to be executed.

The END SELECT statement signals the end of the innermost nested CASE construct.

 	

Syntax

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

construct-name is an optional name for the CASE construct

case-expr is a scalar expression of type INTEGER, LOGICAL, CHARACTER or case-value : case-value

case-value is a constant scalar LOGICAL, INTEGER, or CHARACTER expression.

block is a sequence of zero or more statements or executable constructs.

Remarks

Execution of a SELECT CASE statement causes the case expression to be evaluated. The resulting value is called the case index.

Execution of a CASE code block occurs if the case index derived from the SELECT CASE statement is in the range specified by the CASE statement's case-selector. Execution of the code block ends when any subsequent CASE or END CASE statement is encountered, and the innermost case construct is exited.

The case-selector is evaluated as follows:

case-value means equal to case-value;

:case-value means less than or equal to case-value;

case-value: means greater than or equal to case-value; and

case-value:case-value means greater than or equal to the left case-value,

and less than or equal to the right case-value.

Each case-value must be of the same type and kind as the case construct's case index.

The ranges of case-values in a case construct must not overlap.

If case-value is of type LOGICAL, it cannot have a range.

The block following a CASE DEFAULT, if any, is executed if the case index matches none of the case-values in the case construct. CASE DEFAULT can appear before, among, or after other CASE statements, or can be omitted.

The case-values in a case construct must not overlap.

Only one CASE DEFAULT is allowed in a given case construct.

If the SELECT CASE statement is identified by a construct-name, the corresponding END SELECT statement must be identified by the same construct name. If the SELECT CASE statement is not identified by a construct-name, the corresponding END SELECT statement must not have a construct-name.

Example 1

integer :: i=3 select case (i) case (:-2) write(*,*) "i is less than or equal to -2" case (0) write(*,*) "i is equal to 0" case (1:97) write(*,*) "i is in the range 1 to 97, inclusive" case default write(*,*) "i is either -1 or greater than 97" end select

Example 2

character(len=5) :: c="Howdy" select case (c) case ("Hi","Hello","Howdy") write(*,*) "Hello, how are you?" case ("Good Morning") write(*,*) "Nice morning, isn't it?" case ("Good Night") write(*,*) "Goodbye." case default write(*,*) "What time is it?" end select