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.
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 weekswill attend training 4.
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.
Example 2: Calling Methods
Consider a class called Calculator
that has methods to calculate the sum and positive difference of two values as follows:
public double sum(double value1, double value2);
public 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 ternary 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);
}
}
}
We can also re-write this using a switch statement as follows:
double answer;
switch(input) {
case "+" -> answer = calc.sum(x, y);
case "+-" -> answer = calc.posDifference(x, y);
case "*" -> answer = calc.product(x, y);
default -> answer = calc.quotient(x, y);
}
Resources
Calculator - Using Conditionals and Lambda Mini-Lab