next up previous contents
Next: Exceptional Situations Up: More on Flow Control Previous: Initial Test (DO...WHILE) Loop   Contents

SELECT CASE

Syntax:
$[\langle name \rangle :]$ SELECT CASE ( )  
   
  CASE ( )
   
   
   
  CASE DEFAULT
   
   
  END SELECT

A CASE SELECT function can be implemented with an IF ... ELSEIF... ENDIF construction; however, the former needs a different expression to evaluate at each branch, and is therefore less efficient. Example:

select case (i)
  case (:0)   ; print*, "i<=0"
  case (1)    ; print*, "i=1"
  case default; print*, "i>=2"
end select

program season
  implicit none
  integer::month
  print*,'Give month'
  read*,month
  select case (month)
  case (12,1,2)
    print*,'Month ',month,' is in winter'
  case(3:5)   !this is range from 3 to 5
    print*,'Month ',month,' is in spring'
  case(6:8)
    print*,'Month ',month,' is in summer'
  case(9:11)
    print*,'Month ',month,' is in fall'
  case default
    print*,'invalid month: ',month
  end select
end program season


next up previous contents
Next: Exceptional Situations Up: More on Flow Control Previous: Initial Test (DO...WHILE) Loop   Contents
Adrian Sandu 2001-08-26