typestar

select case in Fortran

Depth bands routed by select case: open ranges, closed ranges, and a default.

program select_case
    implicit none
    integer :: depth_m = 42

    ! select case takes values and ranges; default catches the rest.
    select case (depth_m)
    case (:10)
        print *, "Snorkel water"
    case (11:40)
        print *, "Open-water dive"
    case (41:60)
        print *, "Deep dive: plan your stops"
    case default
        print *, "Submersible territory"
    end select
end program select_case

How it works

  1. select case (depth_m) names the subject once.
  2. case (:10) is everything up to 10; case (11:40) is a closed band.
  3. case default catches what no band claimed, and end select closes.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
353
Tokens
55
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 47 seconds.

Type this snippet

Step 2 of 3 in Control flow, step 8 of 27 in Language basics.

← Previous Next →