-
Hajipur, Bihar, 844101
In Java, the switch statement is another way to control the flow of a program based on conditions, similar to the if...else ladder. It is used when you have multiple possible outcomes that depend on the value of a single variable or expression. Instead of writing several if...else statements, you can use a switch to make the code cleaner and easier to understand.
The switch statement compares a variable against several possible values called cases, and executes the matching block of code.
The basic syntax of a switch statement in Java is as follows:
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}
The expression inside switch() is evaluated once.
The result is compared with each case value.
When a match is found, the code under that case runs.
The break statement stops the execution of the switch block.
If no case matches, the default block executes.
Here’s a basic example that prints the name of a weekday based on a number input:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day number");
}
In this example, since day equals 3, the output will be Wednesday.
The break statement is used to stop the execution of code once a matching case has been executed. If you forget to include break, the program continues executing the next case statements until it finds another break or reaches the end of the switch block.
Example without break:
int num = 2;
switch (num) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
}
Output:
Two
Three
The program prints both “Two” and “Three” because there’s no break after case 2.
The default case runs if no other case matches the given value. It acts as a fallback option. It’s not required, but it’s a good practice to include one.
Example:
int month = 13;
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
default:
System.out.println("Invalid month");
}
Since there’s no case for 13, the default message “Invalid month” will display.
Starting from Java 7, the switch statement also supports String values, which makes it more flexible for text-based comparisons.
Example:
String color = "blue";
switch (color) {
case "red":
System.out.println("Stop");
break;
case "yellow":
System.out.println("Wait");
break;
case "green":
System.out.println("Go");
break;
default:
System.out.println("Invalid color");
}
Output:
Invalid color
This example shows that strings are compared exactly as written, and Java is case-sensitive, so “Blue” and “blue” are not the same.
You can also use char type values in a switch statement.
Example:
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Good job!");
break;
case 'C':
System.out.println("Well done");
break;
default:
System.out.println("Invalid grade");
}
Here, the output will be Good job! because the grade is 'B'.
A nested switch means having a switch statement inside another switch. It’s useful when decisions depend on two variables.
Example:
int branch = 2;
int year = 3;
switch (branch) {
case 1:
System.out.println("Computer Science");
switch (year) {
case 1:
System.out.println("First Year");
break;
case 2:
System.out.println("Second Year");
break;
case 3:
System.out.println("Third Year");
break;
}
break;
case 2:
System.out.println("Electrical Engineering");
switch (year) {
case 1:
System.out.println("First Year");
break;
case 2:
System.out.println("Second Year");
break;
case 3:
System.out.println("Third Year");
break;
}
break;
default:
System.out.println("Invalid branch");
}
This example checks both the student’s branch and year before printing the result.
Starting from Java 14, the enhanced switch syntax allows cleaner and shorter code. It removes the need for break statements and uses -> for direct execution.
Example:
int day = 5;
switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
case 3 -> System.out.println("Wednesday");
case 4 -> System.out.println("Thursday");
case 5 -> System.out.println("Friday");
default -> System.out.println("Weekend");
}
This is more readable and avoids errors caused by missing break statements.
The switch statement in Java is a powerful alternative to multiple if...else conditions when comparing a single variable to different possible values. It makes code shorter, easier to maintain, and more readable. With support for data types like int, char, and String, and the newer enhanced syntax, the switch statement is widely used in menus, control flows, and text-based decisions in Java programs.
Write a Java program that takes an integer input from 1 to 7 and prints the corresponding day of the week using a switch statement. If the number is not in the range, print “Invalid day.”
Create a Java program where the user enters a month number, and the program displays the name of the month using a switch statement. Also, include a default case for invalid inputs.
Write a Java program using a switch statement that checks a given character and prints whether it is a vowel or a consonant.
Develop a program that takes a grade character (A, B, C, D, or F) as input and prints a message such as “Excellent,” “Good,” or “Fail” using a switch case structure.
Write a program that uses a switch statement to perform simple arithmetic operations like addition, subtraction, multiplication, and division based on an operator symbol entered by the user.
Create a menu-driven Java program using a switch statement where the user selects an option such as “1 for Area of Circle,” “2 for Area of Rectangle,” or “3 for Area of Triangle,” and the program calculates the result accordingly.
Develop a program that accepts an integer input representing a year of study (1 to 4) and a branch name, then prints the corresponding department and year using a nested switch statement.
Write a program that takes a number from 1 to 12 and prints how many days are in that month using a switch statement. Assume February has 28 days.
Create a program that uses a switch case to convert numerical grades (1 to 5) into text descriptions such as “Excellent,” “Very Good,” “Good,” “Average,” and “Poor.”
Write a program that uses a switch statement with String values to simulate a traffic light system where the user enters a color name, and the program prints “Stop,” “Get Ready,” or “Go” accordingly.