Next: None. Up: Bits and pieces. Previous: Conditional operator.


The switch statement

Textbook: Section 8.1

We've seen else if popping up in programs with some frequency.

if(letter == 'A') {
    mingrade = 90;
} else if(letter == 'B') {
    mingrade = 80;
} else if(letter == 'C') {
    mingrade = 70;
} else if(letter == 'D') {
    mingrade = 60;
} else if(letter == 'F') {
    mingrade = 0;
}
The switch statement is an alternative to this.

Cases

A switch statement looks like the following.

switch(letter) {
case 'A':
    mingrade = 90;
    break;
case 'B':
    mingrade = 80;
    break;
case 'C':
    mingrade = 70;
    break;
case 'D':
    mingrade = 60;
    break;
case 'F':
    mingrade = 0;
    break;
}
You put an expression in the parentheses after switch (in this case, we want to switch based on the value of letter). Then, between the braces, we list what we want to do for different possible values of that expression. For each possible value, we write case, followed by the value, followed by a colon. Then we list what to do, followed by a break statement, telling the compiler that we've reached the end of our case. The break statement sends the computer to the next statement following the closing brace.

The necessity of break

It's important to remember break statement! If you leave it out, the computer will proceed into the next case. (This is called `falling through' - which is occasionally useful. Most often, however, it's an error.) Suppose, for example, we wrote the earlier switch statement as follows.

switch(letter) {
case 'A':
    mingrade = 90;
case 'B':
    mingrade = 80;
case 'C':
    mingrade = 70;
case 'D':
    mingrade = 60;
case 'F':
    mingrade = 0;
}
Then what would happen is that mingrade would become 0, regardless of what letter holds. If, for example, letter held 'A', the computer would set mingrade to be 90, then 80, then 70, then 60, and then finally 0.

This aspect of switch statements is usually irritating. But there is one particularly important case where this is useful: When you want multiple cases to do the same thing. For example, the following code converts a character into the value it represents as a hexadecimal value.

switch(letter) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
    value = letter - '0';
    break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
    value = letter - 'A';
    break;
}

The default case

You can also use the default label as a sort of ``else'' condition.

switch(letter) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
    value = letter - '0';
    break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
    value = letter - 'A';
    break;
default:
    throw new Exception("not a hexadecimal digit");
}
Here, if letter is anything other than the accepted characters, an exception is thrown.

When switch doesn't apply

Though switch is good to know about, its usefulness is severely hampered by the following two facts.


Next: None. Up: Bits and pieces. Previous: Conditional operator.