Conditionals and Control Flow

Switch Statement

The conditional statements that we have covered so far require Boolean expressions to determine which code block to run. Java also provides a way to execute code blocks based on whether a block is equal to a specific value. For those specific cases, we can use the switch statement, which helps keep code organized and less wordy.

The switch statement is used as follows:

int restaurantRating = 3;

switch (restaurantRating) {

    case 1: System.out.println("This restaurant is not my favorite.");

    case 2: System.out.println("This restaurant is good.");

    case 3: System.out.println("This restaurant is fantastic!");

    default: System.out.println("I've never dined at this restaurant.");

In the example above, we assigned the int variable restaurantRating a value of 3. The code will print a message to console based on the value of restaurantRating.

In this case, This restaurant is fantastic! is printed to the console.

The break statement will exit the switch statement after a condition is met. Without the break statement, Java will continue to check whether the value of restaurantRating matches any other cases.

The default case is printed only if restaurantRating is not equal to an int with the value of 1, 2, or 3.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.