Switch

The switch statement provides a more readable and efficient way to execute different code blocks based on the value of a single variable.

Apprentice · Java · Updated

By

Introduction to the Switch Statement

The switch statement provides a more readable and efficient way to execute different code blocks based on the value of a single variable. It is particularly useful when you need to compare the variable to multiple potential values.

When to Use a Switch Statement

  • Multiple Conditions: When you have a single variable that needs to be compared against many possible values.
  • Readability: When the if-else structure becomes too lengthy and complex.
  • Performance: In some cases, switch can be more efficient than multiple if-else statements.

Syntax

The basic syntax of a switch statement is:

switch (variable) {
    case value1:
        // code to execute if variable == value1
        break;
    case value2:
        // code to execute if variable == value2
        break;
    // more cases...
    default:
        // code to execute if none of the above cases match
        break;
}

Example

Let's consider an example where we have a String variable called dayOfTheWeek. Based on the day, we will print out a suggested restaurant.

  • Variable: The switch statement evaluates the dayOfTheWeek variable.
  • Cases: Each case corresponds to a different day of the week.
  • Break: The break statement terminates the switch block after a match is found, preventing fall-through to subsequent cases.
  • Default: The default case handles any values that do not match the specified cases, providing a fallback action.
String dayOfTheWeek = "Wednesday";

switch (dayOfTheWeek) {
    case "Monday":
        System.out.println("Let's eat at Joe's Diner.");
        break;
    case "Tuesday":
        System.out.println("How about Taco Tuesday at El Mexicano?");
        break;
    case "Wednesday":
        System.out.println("Mid-week sushi at Sakura Sushi!");
        break;
    case "Thursday":
        System.out.println("Try the pasta at Bella Italia.");
        break;
    case "Friday":
        System.out.println("Relax with burgers at Burger Barn.");
        break;
    default:
        System.out.println("It's the weekend! Explore new places!");
        break;
}

By using a switch statement, we can cleanly and efficiently manage multiple conditional branches, improving the code's readability and maintainability.

Do it: name the deli's soup of the day

The corner deli makes one soup each weekday and none on the weekend. Its sign needs the soup for any day.

The function gets one value:

  • dayOfTheWeek: the day's name, a String with a capital first letter ("Tuesday").

The soups:

Day Soup
"Monday" "Tomato"
"Tuesday" "Chicken noodle"
"Wednesday" "Minestrone"
"Thursday" "Chicken noodle"
"Friday" "Clam chowder"
any other day "No soup today"

For example, "Tuesday" matches the Tuesday case, so soupOfTheDay("Tuesday") returns "Chicken noodle".

Your job: finish the function String soupOfTheDay(String dayOfTheWeek) with a switch statement.

  • Give each weekday a case that puts its soup from the table in soup, then ends with break.
  • Any other value, such as "Saturday" or "Sunday", puts "No soup today" in soup.
  • The function returns soup; that line is already there.

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets