Skip Top Navigation Bar

Switch Statements

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:

The break statements prevent fall through. Essentially, without break statements, once a case is matched all the statements in subsequent cases will also be output. So, if we remove the break statements, we have the following:

If weekSelected was 1, all 4 output statements would be printed, as follows:

Attend training 1
Attend training 2
Attend training 3
Attend training 4

Once a break statement is added in, the fall through would stop. For example, if only the first break is missing as follows:

If weekSelected was 1, the first two output statements would be printed, as follows:

Attend training 1
Attend training 2

Example 1 Re-written with Lambda Notation

The symbol -> is lambda notation. Lambda notation prevents fall through, therefore no break statements are needed.

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); 
      break;
   }
   case "+-": {
      answer = calc.posDifference(x, y);
      break;
   }
   case "*": {
      answer = calc.product(x, y); 
      break;
   }
   default: {
      answer = calc.quotient(x, y);
   }
}

Or re-written using lambda notation we would have:

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);
}

Switch Statements and AP CSA FRQs

  • Switch statements are OUTSIDE the AP CSA Java Subset.
  • Students can write correct solutions using switch statements.
  • Switch statements can be used in any FRQ, but may be seen more often in Question 1.

AP CSA Scoring and Teaching

  • Student solutions that use correct Java syntax can earn full credit for their solution
  • Questions are written with an in AP Java subset solution in mind
  • Students should be encouraged to write solutions within the AP Java Subset

Final Word

  • Switch statements are often used when there is a menu of options to respond to.
  • Switch statements are often more readable than using equivalent nested if statements.
  • Use break; or lambda notation (->) to prevent fall through in a switch statement.

Resources

Calculator - Using Conditionals and Lambda Mini-Lab