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.
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
0 ???
1 bad
2 bad
3 medium
4 good
5 good
Created with @carbon_app pic.twitter.com/5R6yeSnvpY
— FortranTip (@fortrantip) December 20, 2021
- 1
Compiled using
GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
with no flags