Use the switch
statement to conditionally perform statements
based on an integer expression. For example, suppose that your program
contained an integer named month
whose value indicated the
month in some date. Suppose also that you wanted to display the name of
the month based on its integer equivalent. You could use Java's
switch
statement to perform this feat:
int month;
. . .
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
}
The switch
statement evaluates its expression,
in this case, the value of month
, and executes the
appropriate case
statement. Of course, you could
implement this as an if
statement:
int month;
. . .
if (month == 1) {
System.out.println("January");
} else if (month == 2) {
System.out.println("February");
. . .
// you get the idea
. . .
Deciding whether to use an if
statement or a switch
statement is a judgment call.
You can decide which to use based on readability and other factors.
An if
statement can be used to make decisions
based on ranges of values or conditions,
whereas a switch
statement can make decisions
based only on a single integer value.
Also, the value provided to each case
statement must be unique.
Another point of interest in the switch
statement are the
break
statements after each case
. The break
statements cause control to break out of the switch
and continue
with the first statement following the switch
.
The break
statements are necessary because case
statements fall through. That is, without an explicit break
control will flow sequentially through subsequent case
statements.
In the previous example, you don't want control to flow from one case
to the next, so you have to put in break
statements.
However, there are certain scenarios when you do want control to proceed
sequentially through case
statements. Like in the following Java code that
computes the number of days in a month according to the old rhyme that
starts "Thirty days hath September...":
int month;
int numDays;
. . .
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if ( ((year % 4 == 0) && !(year % 100 == 0))
|| (year % 400 == 0) )
numDays = 29;
else
numDays = 28;
break;
}
Finally, you can use the default
statement at the end of the
switch
to handle all values that aren't explicitly handled
by one of the case
statements.
int month;
. . .
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
default: System.out.println("Hey, that's not a valid month!");
break;
}