| Lesson 3 | The switch statement |
| Objective | Learn how to alter program flow using the switch statement. |
Switch Statement in Java
The
switch statement is used to conditionally choose between multiple (more than two) branches of code. Following is the syntax for the
switch statement:
switch (Expression) {
case Constant1:
StatementList1
break;
case Constant2:
StatementList2
break;
default:
DefaultStatementList
}
Swtich Statement Execution
The
switch statement first evaluates the
Expression and then compares the result to all of the
case constants. If the
switch statement finds a match, it branches to the statement list corresponding to the matching
case constant. If none of the
case constants match the
Expression the program branches to the optional
DefaultStatementList,
if one has been supplied. A statement list is sort of like a
compound statement[1] except that it is not enclosed by curly braces (
{}). The
switch statement does not require a compound statement in order to branch to multiple statements.
However, the
switch statement is somewhat limited in that each
case match must be a constant.
Following is an example that demonstrates the
switch statement:
switch (score) {
case 1:
rating = "Poor";
break;
case 2:
rating = "Below Average";
break;
case 3:
rating = "Average";
break;
case 4:
rating = "Above Average";
break;
case 5:
rating = "Excellent";
break;
default:
rating = "Undefined";
}
Swtich Statement Example
This example switches on an integer variable and looks at different integer constants for a match. If the switch statement in the code encounters a match, it immediately executes the code beneath the matching case statement. The code beneath the case statement continues to execute until it reaches a break statement,
at which point it jumps out of the entire switch statement. If no match is made, the code beneath the default statement is executed. Break statement: The break statement is used to exit out of a branch or loop.
Falling through a case statement in Java
A
switch statement is similar to a nested
if-else statement in that a single branch of code is conditionally executed.
However,
switch statements can be tricky if you accidentally leave out a
break statement.
If this happens, the program will fall through to the next
case branch and continue executing until it encounters a
break.
This mistake is often the cause of very insidious bugs that are hard to track down, so make sure to double check that you have placed
break statements in the appropriate places. There are some isolated circumstances where you might want to purposely leave out a
break statement to allow a
case branch to fall through, but generally speaking it is an error.
- switch Statements:
You have seen how if and else-if statements can be used to support both simple and complex decision logic. In many cases, the switch statement provides a cleaner way to handle complex decision logic. We will now compare the following if-else if statement to the equivalently performing switch statement:
if-else
int x = 3;
if(x == 1) {
System.out.println("x equals 1");
}
else if(x == 2) {
System.out.println("x equals 2");
}
else {
System.out.println("No idea what x is");
}
Now let us examine the same functionality represented in a switch construct:
switch Construct
int x = 3;
switch (x) {
case 1:
System.out.println("x equals 1");
break;
case 2:
System.out.println("x equals 2");
break;
default:
System.out.println("No idea what x is");
}
Note: The reason this switch statement emulates the if is because of the break statements that were placed inside of the switch.
In general, break statements are optional, and as you will see in a few pages, their inclusion or exclusion causes changes in how a switch statement will execute.
Switch Statement - Exercise
[1]Compound statement: A compound statement is a block of code that is surrounded by curly braces (
{}). Compound statements allow you to use multiple lines of code in situations where a single statement is expected. A good example is the code executed in the branch of an
if statement.
