Click here to navigate the course.

Drag the edges to resize the window.

In Projects, you can keep track of your progress as you go throught the tasks. Check each item as you complete it!

Code Editor
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."); break; case 2: System.out.println("This restaurant is good."); break; case 3: System.out.println("This restaurant is fantastic!"); break; default: System.out.println("I've never dined at this restaurant."); break; }

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.

Report a Bug
If you see a bug or any other issue with this page, please report it here.