Java provides a powerful tool known as the “switch” statement, which allows developers to easily control the flow of their programs based on a specific value or variable. In this blog post, we will take a look at how to use switch statements in Java, including an example program to help illustrate the concept.
First, let’s start with the basic structure of a switch statement. The switch statement is used to compare a variable or value against a series of “case” statements. Each case statement is associated with a specific value or range of values, and the program will execute the code associated with the first case statement that matches the variable or value being tested.
Here is an example of a simple switch statement:
int myValue = 3;
switch (myValue) {
case 1:
System.out.println("The value is 1");
break;
case 2:
System.out.println("The value is 2");
break;
case 3:
System.out.println("The value is 3");
break;
default:
System.out.println("The value is not 1, 2, or 3");
break;
}In this example, the variable “myValue” is set to 3. The switch statement compares this value against each of the case statements, and when it finds a match (in this case, case 3), it executes the code associated with that case statement (in this case, “The value is 3”).
It’s important to note that the “break” statement is used to end the switch statement and exit the block of code associated with each case statement. If you don’t include a break statement, the program will continue to execute the code for all of the case statements that come after the matching case statement.
Flow chart of the switch statement

Now let’s look at a more complex example program that uses a switch statement to control the flow of a simple menu-based program.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int menuOption;
boolean validOption = false;
while (!validOption) {
System.out.println("Please choose a menu option:");
System.out.println("1. Option 1");
System.out.println("2. Option 2");
System.out.println("3. Option 3");
System.out.println("4. Exit");
menuOption = input.nextInt();
switch (menuOption) {
case 1:
System.out.println("You have selected Option 1.");
validOption = true;
break;
case 2:
System.out.println("You have selected Option 2.");
validOption = true;
break;
case 3:
System.out.println("You have selected Option 3.");
validOption = true;
break;
case 4:
System.out.println("Exiting program.");
validOption = true;
break;
default:
System.out.println("Invalid option. Please try again.");
break;
}
}
}
}- The program imports the Scanner class from java.util package in order to allow user input.
- A variable named “menuOption” is declared as an integer and a variable named “validOption” is declared as a boolean and initialized as false.
- A while loop is created that continues to run as long as the “validOption” variable is false.
- Within the while loop, the program prompts the user to choose a menu option with a list of options (1. Option 1, 2. Option 2, 3. Option 3, 4. Exit).
- The user’s input is saved as the “menuOption” variable.
- A switch statement is used to check the value of the “menuOption” variable and corresponding actions are taken based on the user’s input.
- If the user input is 1, the program prints “You have selected Option 1.” and sets the “validOption” variable to true, breaking the loop.
- If the user input is 2, the program prints “You have selected Option 2.” and sets the “validOption” variable to true, breaking the loop.
- If the user input is 3, the program prints “You have selected Option 3.” and sets the “validOption” variable to true, breaking the loop.
- If the user input is 4, the program prints “Exiting program.” and sets the “validOption” variable to true, breaking the loop.
- If the user input is anything other than 1, 2, 3, or 4, the program prints “Invalid option. Please try again.” and the loop continues.
- The program exits once a valid menu option has been selected.
Latest Posts:
- Count Subarrays with Odd SumTitle: Count Subarrays with Odd Sum In this guide, you’ll learn how to count the subarrays of an integer array… Read more: Count Subarrays with Odd Sum
- Count Subarrays Whose Product is Less than KTitle: Count Subarrays Whose Product is Less than K In this guide, you’ll learn how to count the contiguous subarrays… Read more: Count Subarrays Whose Product is Less than K
- Minimum Number of Increments to Make Array Strictly IncreasingTitle: Minimum Number of Increments to Make Array Strictly Increasing In this guide, we will solve a classic greedy array… Read more: Minimum Number of Increments to Make Array Strictly Increasing
- Minimum Number of Operations to Make Array EqualTitle: Minimum Number of Operations to Make Array Equal In this guide, you’ll learn how to solve the Minimum Number… Read more: Minimum Number of Operations to Make Array Equal
- Minimum Number of Deletions to Make an Array SortedTitle: Minimum Number of Deletions to Make an Array Sorted Sorting problems often hide a subtler question: what is the… Read more: Minimum Number of Deletions to Make an Array Sorted
- Minimum Number of Swaps to Bring All Elements Less Than K TogetherTitle: Minimum Number of Swaps to Bring All Elements Less Than K Together In this guide, you’ll learn how to… Read more: Minimum Number of Swaps to Bring All Elements Less Than K Together



