Fortran – select case construct

Fortran select case construct

In this guide, we will discuss Fortran select case construct. A select case statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being selected on is checked for each select case.

Syntax

The syntax for the select case construct is as follows −

[name:] select case (expression) 
   case (selector1)          
   ! some statements          
   ... case (selector2)           
   ! other statements           
   ...       
   case default          
   ! more statements          
   ...   
end select [name]

The following rules apply to a select statement −

  • The logical expression used in a select statement could be logical, character, or integer (but not real) expression.
  • You can have any number of case statements within a select. Each case is followed by the value to be compared to and could be logical, character, or integer (but not real) expression and determines which statements are executed.
  • The constant-expression for a case, must be the same data type as the variable in the select, and it must be a constant or a literal.
  • When the variable being selected on, is equal to a case, the statements following that case will execute until the next case statement is reached.
  • The case default block is executed if the expression in select case (expression) does not match any of the selectors.

Flow Diagram

select case flow diagram

Example 1

program selectCaseProg
implicit none

   ! local variable declaration
   character :: grade = 'B'

   select case (grade)
   
      case ('A') 
      print*, "Excellent!" 

      case ('B')
      
      case ('C') 
         print*, "Well done" 

      case ('D')
         print*, "You passed" 

      case ('F')
         print*, "Better try again" 

      case default
         print*, "Invalid grade" 
      
   end select
   
   print*, "Your grade is ", grade 
 
end program selectCaseProg

When the above code is compiled and executed, it produces the following result −

Your grade is B

Specifying a Range for the Selector

You can specify a range for the selector, by specifying a lower and upper limit separated by a colon −

case (low:high) 

The following example demonstrates this −

Example 2

program selectCaseProg
implicit none

   ! local variable declaration
   integer :: marks = 78

   select case (marks)
   
      case (91:100) 
         print*, "Excellent!" 

      case (81:90)
         print*, "Very good!"

      case (71:80) 
         print*, "Well done!" 

      case (61:70)
         print*, "Not bad!" 

      case (41:60)
         print*, "You passed!"  

      case (:40)
         print*, "Better try again!"  

      case default
         print*, "Invalid marks" 
         
   end select
   print*, "Your marks is ", marks
 
end program selectCaseProg

When the above code is compiled and executed, it produces the following result −

Well done!
Your marks is 78

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply