next up previous contents
Next: Simple Input / Output Up: SELECT CASE Construct Previous: SELECT CASE Construct

 

Example Of SELECT CASE Construct

A simple example, (the corresponding IF structure is given as a comment,)

      SELECT CASE (num)

       CASE (6,9,99,66)
     ! IF(num==9.OR. .. .OR.char==66) THEN
        PRINT*, "Woof woof"

       CASE (10:65,67:98)
     ! ELSEIF((num.GE.10.AND.LE.65) .OR. ...
        PRINT*, "Bow wow"

       CASE (100:)
     ! ELSEIF (num.GE.100) THEN
        PRINT*, "Bark"

       CASE DEFAULT
     ! ELSE
        PRINT*, "Meow"

      END SELECT     
     ! ENDIF

An IF .. ENDIF construct could be used but a SELECT CASE is neater and more efficient.

Important points are,

A more complex exmaple is given below, this also demonstrates how SELECT CASE constructs may be named.

     ...
    outa: SELECT CASE (n)
     CASE (:-1) outa
      M = -1
     CASE (1:) outa
      DO i = 1, n
       inna: SELECT CASE (line(i:i))
        CASE ('@','&','*','$')
         PRINT*, "At EOL"
        CASE ('a':'z','A':'Z')
         PRINT*, "Alphabetic"
        CASE DEFAULT
         PRINT*, "CHAR OK"
       END SELECT inna
      END DO
     CASE DEFAULT outa
      PRINT*, "N is zero"
     END SELECT outa

Analysis:


next up previous contents
Next: Simple Input / Output Up: SELECT CASE Construct Previous: SELECT CASE Construct

Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 15:03:35 GMT 1996
Not for commercial use.