switch
statement can be a useful piece of code. But, there are a few things you should know before you use it. A switch
statement can take the following form:switch (Expression) {
case <em>FirstValue</em>:
<em>Statements</em>
case <em>SecondValue</em>:
<em>MoreStatements</em>
// ... more cases...
default:
<em>EvenMoreStatements</em>
}
Here are some tidbits about switch
statements:
- The Expression doesn’t have to have an
int
value. It can bechar
,byte
,short
, orint
.
For example, the following code works in Java 5 and later:
char letterGrade;
letterGrade =keyboard.findWithinHorizon(".",0).charAt(0);
switch (letterGrade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
case 'C':
System.out.println("Average");
break;
}
In fact, if you avoid using the Scanner
class and its findWithinHorizon
method, this bullet's switch
statement works with all versions of Java — old and new.
- If you use Java 7 or later, the Expression can be a
String
. For example, the following code doesn't work with Java 6, but works well in Java 7:
String description;
description = keyboard.next();
switch (description) {
case "Excellent":
System.out.println('A');
break;
case "Good":
System.out.println('B');
break;
case "Average":
System.out.println('C');
break;
}
- The Expression doesn’t have to be a single variable. It can be any expression of type
char
,byte
,short
,int
, orString
. For example, you can simulate the rolling of two dice with the following code:
int die1, die2;
die1 = myRandom.nextInt(6) + 1;
die2 = myRandom.nextInt(6) + 1;
switch (die1 + die2) {
//. . . etc.
- The cases in a
switch
statement don’t have to be in order. Here’s some acceptable code:
switch (randomNumber) {
<strong>case 2:</strong>
System.out.println("No, and don't ask again.");
break;
<strong> </strong>
<strong>case 1:</strong>
System.out.println("Yes. Isn't it obvious?");
break;
<strong> </strong>
<strong>case 3:</strong>
System.out.print("Yessir, yessir!");
System.out.println(" Three bags full.");
break;
//. . .etc.
This mixing of cases may slow you down when you’re trying to read and understand a program, but it’s legal nonetheless.
- You don’t need a
case
for each expected value of the Expression. You can leave some expected values to thedefault
. Here’s an example:
switch (randomNumber) {
case 1:
System.out.println("Yes. Isn't it obvious?");
break;
case 5:
System.out.println("No chance, Lance.");
break;
case 7:
System.out.println("Yes, but only if you're nice to me.");
break;
case 10:
System.out.println("No, not until Nell squeezes Rover.");
break;
default:
System.out.println("Sorry, I just can't decide.");
break;
}
- The
default
clause is optional.
switch (randomNumber) {
case 1:
System.out.println("Yes. Isn't it obvious?");
break;
case 2:
System.out.println("No, and don't ask again.");
break;
case 3:
System.out.print("I'm too tired.");
System.out.println(" Go ask somebody else.");
}
System.out.println("Goodbye");
If you have no default
clause, and a value that’s not covered by any of the cases comes up, the switch
statement does nothing. For example, if randomNumber
is 4
, the preceding code displays Goodbye
and nothing else.
- In some ways,
if
statements are more versatile thanswitch
statements. For example, you can’t use a condition in aswitch
statement’s Expression:
<strong>//You can't do this:</strong>
switch (age >= 12 && age < 65)
You can’t use a condition as a case value
, either:
<strong>//You can't do this:</strong>
switch (age) {
case age <= 12: // … etc.