3.3 The case Statement

There are times when nested if statements can get a little messy. Whenever the number of branches gets to be 4 or more then the case statement can be a better alternative. It is very useful for choosing from many alternatives.

Suppose you had a program that could do simple calculations. The user will enter two numbers and then they will have the choice of what operation to perform on those numbers. They can add, subtract, multiply, divide or make a power out of them. Here's the code:

var num1, num2 : real
var choice : char

put "Enter first number"
get num1
put "Enter second number"
get num2
put "What do you want to do with the numbers?"
put "(A)dd them"
put "(D)ivide them"
put "(M)ultiply them"
put "make a (P)ower out of them"
put "(S)ubtract them"
get choice

case choice of
   label 'A': put num1 + num2
   label 'D': put num1 / num2
   label 'M': put num1 * num2
   label 'P': put num1 ** num2
   label 'S': put num1 - num2
   label    : put "Sorry that was an invalid choice"
end case 

The case statement looks at the value of choice and then tries to find a label that matches it. If it finds a match then it executes the code after it until it gets to the next label. If it doesn't find a match it executes the code after the blank label. If there is no match and no blank label then the program produces an error and crashes. So it is a good idea to always have a blank label to take care of all the cases that you didn't expect. Here is an example of the output from this program.

Enter first number
5
Enter second number
2
What do you want to do with the numbers?
(A)dd them
(D)ivide them
(M)ultiply them
make a (P)ower out of them
(S)ubtract them
D
2.5

This is not a very user friendly program. The user must type upper case letters for the menu to work properly. Here is a way to make the case statement more user friendly:

case choice of
   label 'A', 'a': put num1 + num2
   label 'D', 'd': put num1 / num2
   label 'M', 'm': put num1 * num2
   label 'P', 'p': put num1, " to the ", num2 ..
                   put " is " ..
                   put num1 ** num2
   label 'S', 's': put num1 - num2
   label         : put "Sorry that was an invalid choice"
end case 

If you want to take the same action for several different labels you can. Simply put all the labels together separated by commas. The example above has been set up to handle either upper or lower case letters.

The last example also shows how you can have as many statements as you like for each case. If the user enters P or p for their choice then all three of those put statements will be executed. Here is a sample run of the program:

Enter first number
2
Enter second number
5
What do you want to do with the numbers?
(A)dd them
(D)ivide them
(M)ultiply them
make a (P)ower out of them
(S)ubtract them
p
2 to the 5 is 32

In our examples the variable that came after the keyword case was of type char. The variable can also be of type int or boolean (although an if statement would make more sense for boolean). It cannot be real.

Exercise 3.3

  1. Rewrite using the indentation style illustrated in the text.
    case month of label 1,3,5,7,8,10,12:days:=31 label 4,6,9,11:days:=30
    label 2:if leapYear then days:=29 else days := 28 end if end case 
  2. Rewrite this case statement using nested ifs.
    case choice of
       label 'A': put num1 + num2
       label 'D': put num1 / num2
       label 'M': put num1 * num2
       label 'P': put num1 ** num2
       label 'S': put num1 - num2
       label    : put "Sorry that was an invalid choice"
    end case 
  3. A sequence of six tests, all scored out of 100, are to be given different weightings in determining a total mark. Write a program fragment that first reads a value of testNumber and score. Using a case statement it computes and then prints the appropriate value of weightedScore, using the weightings given in the following table.
    Test Number
    Weight
    1
    10%
    2
    20%
    3
    20%
    4
    15%
    5
    15%
    6
    20%
    For example, input of 3 and 27 should produce output of:

    A score of 27 on test 3 gives a weighted score of 5.4

  4. Write a program that prints, in words, the last digit of the int variable number. For example, if the value of number is 547, the program should print:

    The last digit is seven.

  5. Write a program that reads a date in numeric form as yy mm dd and then prints that date as one might on a cheque. For example, input of 03 and 06 and 23 should produce output of:

    June 23, 2003