Select Case

Statement Type:

The Select Case statement is an executable statement.

Statement Purpose:

Like the various forms of the if statements, the select case structure is another way of using branching. It is useful if you are going to base a decision upon an INTEGER, LOGICAL or CHARACTER expression. It is important to note that it can not be used on REAL arguments.

Additional Required Statements:


This is used by the compiler to define under what conditions it should execute the lines of code that immediately follow it. The (N)is what defines the conditions. The (N) can be either an INTEGER, LOGICAL or CHARACTER expression.


This is used to tell the compiler that the end of the document has been reached.

Optional Statements:


This is used by the compiler as an option in case none of the other conditions listed in the select case structure have been met. Also note that the CASE DEFAULT can appear anywhere within the select case structure. However, for clarity it is suggested that you put your CASE DEFAULT at the beginning or the end of the structure.


The name in this case can be anything you might possible want to call the case structure. This is used for extra clarity when you have imbedded loops and branching structures. If this is used, make sure to finish the case structure by Typing END SELECT NAME.


The general form of the select case construct is as follows. Note that material is a character variable in this example:
      Select case (material)
      case ('A36')
          ymod = 29e3
      case ('L2')
          Ymod = 29e3
      case ('C83400')
          ymod = 14.6e3
      case ('2014-T6')
         ymod = 10.6e3
      default case
         print *, 'You have entered an unknown material.'
      End Select
Note a few other things about this structure. If you were using an integer variable instead of a character variable, the expression in the parenthesis after each case statement would look like this (1) or (2:5). With these, the computer would execute the first case if the variable equaled one and the second if it had a value of 2,3,4 or 5. Please note that in Fortran it is unacceptable if your cases overlap. For instance, if you had the following cases:
      Select Case (I)
      case (1:3)
         x = 4.7
      case (3:4)
         x = 9.8
The compiler would return an error message since it wouldn't know value to assign x if the value of I was three. Also, if you wanted to name the first case structure, something like the following could be done.
      Elasticity: select case (material)
Then to end the block you would type the following:
      end select Elasticity

Related Topics and Lectures and Examples

if structures

logical operators

character variables

lecture twelve

examples: charvar90.f and iftests.f

Up one level / Home

Written by Jason Wehr: and Maintained by John Mahaffy :