Welcome to AE Resources
Converted document Converted document

FORTRAN 90+: SELECT CASE

The SELECT CASE command is used to perform a command based on a variable value. The format of the SELECT CASE command is:
SELECT CASE (selector)
          CASE (label_1)
               execution_statement_1
          CASE (label_2)
               execution_statement_2
          CASE (label_n)
               execution_statement_n
          CASE DEFAULT
               execution_statement
END SELECT
The selector can be of variable type INTEGER, LOGICAL or CHARACTER [Note: it can not be a REAL type]. The selector can be for a single value or a range of values. To define a range of values use a colon “:” in one of four ways:
CASE (1:5) !Runs if the selector is in the range of 1 to 5
CASE (: 5) !Runs if the selector is less than 5
CASE (5: ) !Runs if the selector is greater than 5
CASE (1:5, 100: ) !Runs if the selector is in the range of 
                  !1 to 5 or greater than 100
The last CASE shown can be expanded to be as many ranges as necessary. The CASE DEFAULT is a catch all that will run its prescribed executable statement if all the other cases fail. If the CASE DEFAULT is not present and all the CASEs fail, the program simply moves to the next part of the program after the END SELECT.