Using the switch Structure to Simplify Programming

Using the switch Structure to Simplify Programming

The situation in the Binary Dice program happens often enough that another structure is designed for exactly this kind of case, when you are comparing one variable to a number of possible values. The Switch Dice program shown in Figure 3.9 looks identical to the Binary Dice program as far as the user is concerned, except it shows the Roman numeral representation of the die roll instead of the binary version.

Click To expand
Figure 3.9: This version shows a die roll in Roman numerals.

While the outward appearance of the last two programs is extremely similar, the underlying structure of the code is changed to illustrate a very handy device called the switch structure.

Building the Switch Dice Program

The code of the Switch Dice program looks different than the Binary Dice demo, but the results are the same.

<title>Switch Dice</title>
<h3>Demonstrates switch structure</h3>

$roll = rand(1,6);
print "You rolled a $roll";
print "<br>";

switch ($roll){
  case 1:
    $romValue = "I";
  case 2:
    $romValue = "II";
  case 3:
    $romValue = "III";
  case 4:
    $romValue = "IV";
  case 5:
    $romValue = "V";
  case 6:
    $romValue = "VI";
    print "This is an illegal die!";
} // end switch

print "<br>";
print "<img src = die$roll.jpg>";
print "<br>";
print "In Roman numerals, that's $romValue";
print "<br>";
print "<br>";
print "<br>";

Refresh this page in the browser to roll another die.


Using the switch Structure

The switch structure is optimized for those situations where you have one variable that you want to compare against a number of possible values. To make it work, use the switch keyword followed by the name of the variable you wish to evaluate in parentheses. A set of braces indicates that the next block of code will be focused on evaluating the possible values of this variable.

For each possible value, use the case statement, followed by the value, followed by a colon. End each case with a break statement, which indicates the program should stop thinking about this particular case, and get ready for the next one.


The use of the break statement is probably the trickiest part of using the switch statement, especially if you are familiar with a language, such as Visual Basic, which does not require such a construct. It's important to add the break statement to the end of each case, or the program flow will simply "fall through" to the next possible value, even if that value would not otherwise evaluate to true. As a beginner, you should always place the break statement at the end of each case.

The last case is called default. This works just like the else clause of the multi-value if statement. It defines code to execute if none of the other cases is active. As in multi-value if statements, it's smart to test for a default case even if you think it is impossible for the computer to get to this default option. Crazy things happen, and it's good to be prepared for them.