What are switch statements used for?
- Switch statements are often used when you have a menu of options to choose from.
- Algorithms that use switch statements can also be written with a series of nested if..else statements.
- Using switch statements instead of a series of nested if..else statements can make code more readable.
- Default statements are often required to ensure that all cases are addressed, and the switch is exhaustive.
Basic Structure of a Switch Statement
switch(<variable>) {
case <value1> -> <what you want to do in this case>;
case <value2> -> <what you want to do in this case>;
...
case <valuex> -> <what you want to do in this case>;
default -> <what you want to do if none of the other cases match>;
}
Sometimes multiple case values have the same action associated with it. At these times, you can put a comma between the two cases as follows:
switch(<variable>) {
case <value1>, <value2>, ... <valuex> -> <what you want to do in this case>;
case <valuey> -> <what you want to do in this case>;
...
case <value> -> <what you want to do in this case>;
default -> <what you want to do in this case>;
}
Example 1: Training
Consider the following code segment which assigns students in an organization to a specific training session based on which weeks they can volunteer. Students who can volunteer in weeks 1 and 2, will attend training 1; week 3 will attend training 2; week 4 will attend training 3; and all other weeks will attend training 4.
Your Turn
Let's try it in the Java Playground.
- Re-written using a switch statement.
- use the keyword
switch
followed by the variable that you want to check the cases against.
- In the body of the switch, there are a series of case statements.
- Each case value is followed by the
->
operator and what statements to do for that case.
- The arrow symbol
->
is used in switch statements and prevents fall through. This means that once a case statement matches, no other statements will be executed.
- If the values for a case matches the value of the variable, then the statements associated with that case are executed.
- The re-write is started below.
- Predict the output of the code.
- Run the code to determine if your prediction is correct or not. Modify the
weekSelected
to have the values 2, 3, and 4 and run each one.
- Add the additional cases that to print
Attend training 2
and Attend training 2
for these weeks as well.
Example 2: Calling Methods
Consider a class called Calculator
that has methods to calculate the sum and positive difference of two values as follows:
double sum(double value1, double value2);
double posDifference(double value1, double value2);
Based on a string input of +
for sum and +-
for positive difference, we can call the appropriate method using the conditional operator:
Calculator calc = new Calculator();
double x = //input from user;
double y = //input from user;
String input = //input from user;
double answer = input.equals("+") ? calc.sum(x, y) : calc.posDifference(x, y);
Expanding on this example, assume Calculator
has product
and quotient
methods as well, the code would be:
Calculator calc = new Calculator();
double x = //input from user;
double y = //input from user;
String input = //input from user;
double answer = (input.equals("+") ? calc.sum(x, y)
: ((input.equals("+-") ? calc.posDifference(x, y)
: ((input.equals("*") ? calc.product(x, y)
: calc.quotient(x, y))))));
This code could also be re-written as an if..else statement as follows:
double answer;
if (input.equals("+")) {
answer = calc.sum(x, y);
} else {
if (input.equals("+-")) {
answer = calc.posDifference(x, y);
} else {
if (input.equals("*")) {
answer = calc.product(x, y);
} else {
answer = calc.quotient(x, y);
}
}
}
Your Turn
Let's try it in the Java Playground
- The
Calculator
class is provided for you.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Modify the conditional operator expression to use a switch statement instead.
- Test you code with different
x
, y
, and input
values.
Resources
Calculator - Using Conditionals and Lambda Mini-Lab
Next Learn Tutorial