Case
Construct
Generalized Conditional
§
Conditional
statement has two branches
§
It can
be generalized to have multiple
branches
§
There
are problems where different computations are invoked for different values of
an expression
§
expressions
can have more than two values
Example
Problem: Given an 24-hour clock value, compute the
corresponding value in
the AM-PM clock
Solution:
AM-PM depends upon the value in the 24-hour clock:
00:01 11:59
- AM
12 hours - noon
12:01 - 23:59 - PM
00:00
- midnight
The Program
The Program continued
case(0) !hour has the
value 0
if (minute = 0) then print *,"12 midnight"
else print *, 0,minute, AM"
case(12) !hour
has the value 12
if (minute = 0) then print *,"12 noon"
else print *, "12:",minute, PM"
case default
print *, "error in hour
end select
end program
General
form
Selection of branch
decided by the variable in the select statement
The general form
is:
select case(exp)
case(sel1) block1
case(sel2) block2
...
case(seln) blockn
case default block(n+1)
end select
General Case statement
The case expression
is of type integer, character or logical
The case selector
can be:
Integer or truth value
Integer1 : integer2
Integer1:
: integer2
A list of integers
The case expression
can not match more than one selector
The default case
Default case is
optional and is the last
When none of the case
selector match, this default case matches
Should be used with care
This case will remove
run-time errors and hence may hide logical errors in the program
Execution of Case
Statement
First
the expression in the select case statement is executed
The
first case branch following the select statement matching the evaluated value
is selected
The block
of statements following the selected case is executed
The
control then moves to the statement that follows the select statement
The
default case matches any value and hence executed when no other case matches
The Cases
Range of values can
be given, eg.,
case(0:11), case(11:23)
Implicit endpoints
allowed
case(:j) - all values <= j
case(i:) - all values >=i
i,j must be
constants
ranges and values
for different cases must be disjoint
Nesting of Cases
Select statements can be nested
A select case statement appear in one or more blocks of the cases of
another case statement.
The inner select statement should `end' within the same block
case statements apply to the innermost select statement
Case statements may be labelled
Example
out: select case(x)
case(1)
in:select
case(y)
case(0) block1
default block2
end select
in
case(2)
block3
default
block4
end select out
A Calculator example
read *, x,y
read *, op
select case(op)
case(+)
print *, (x+y)
case(*)
print *, (x*y)
case(/) ! Some problem with this character
if (y==0) then
print *, result undefined
else
print *, (x/y)
case default
print *, undefined
operation
end select
Exercises
1. There is some problem in am_pm_clock program. Find
and Improve the program
2. Develop a program that converts from am-pm format to 24-hour format
3. Write a program to compute the number of months in a given month