035. select case for conditional execution#

topic: Conditionals

A select case construct conditionally executes one block of constructs or statements depending on the value of a scalar expression in a select case statement.

https://riptutorial.com/fortran/example/5584/select-case-construct

select case is more restrictive and clear than an if block and should be used when it is possible, especially when there are many branches.

select-case.f90 | | 0 | Godbolt Compiler Explorer logo | Fortran logo#
program select_case
   implicit none
   integer :: i
   character(len=6) :: rating

   do i = 0, 5
      select case (i)
         case (1:2)
            rating = "bad"
         case (3)
            rating = "medium"
         case (4:)
            rating = "good"
         case default
            rating = "???"
      end select
      print *, i, trim(rating)
   end do

end program select_case
Output1#
           0 ???
           1 bad
           2 bad
           3 medium
           4 good
           5 good


1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags