Computer Applications | Java | Class 9 | ICSE | 2026 Examination
Conditional Construct in Java | Class 9 | Computer Applications | ICSE
Java Switch Case Tutorial for Class 9 & 10 | Case, Break & Default
The switch-case construct is a multi-way selection statement in Java that allows a program to execute one code block among many alternatives based on the value of an expression. It provides a cleaner and more organized way to handle multiple conditions when compared to nested if-else ladders, especially when testing a single variable against multiple constant values.
Real-World Analogy: Imagine you're at a restaurant with a numbered menu. The waiter asks you to select a dish by its number (say, 1 for Biryani, 2 for Pasta, 3 for Pizza, etc.). Based on the number you choose, the kitchen prepares that specific dish. Similarly, in a switch-case statement, your program "selects" and executes a specific block of code based on the value of a variable.
When you have multiple conditions to check against the same variable, using an if-else ladder can become lengthy, difficult to read, and harder to maintain. The switch-case construct solves this problem by:
Example Scenario: If you need to display the day name based on a number (1–7), using seven if-else statements would be cumbersome. The switch-case makes this task simple and clear.
The general syntax of the switch-case construct is:
switch (expression) {
case value1:
*// Code to be executed if expression == value1*
break;
case value2:
*// Code to be executed if expression == value2*
break;
case value3:
*// Code to be executed if expression == value3*
break;
*// You can have any number of case statements*
default:
*// Code to be executed if no case matches*
break;
}
How it Works: