Suppose you are building a small program that needs to perform different actions depending on a particular value.
For example, imagine you have a person's age and you want to show a specific message when their age is 18 or 60.
You could write this using an if-else ladder:
int age = 18;
if (age == 18) {
System.out.println("Go and apply for driving license");
} else if (age == 60) {
System.out.println("It's time to retire");
} else {
System.out.println("I have nothing to say about you");
}
This works perfectly fine. But when we have multiple fixed values that we want to compare against the same variable, a switch statement can make the code easier to read.
The basic idea is simple:
Take one value and check which case matches that value.
Basic Switch Syntax
The traditional syntax of a switch statement looks like this:
switch (expression) {
case value:
// code
break;
case value:
// code
break;
default:
// code
break;
}
Here, the expression is the value that Java will check.
For example:
int age = 18;
switch (age) {
case 18:
System.out.println("Go and apply for driving license");
break;
case 60:
System.out.println("It's time to retire");
break;
default:
System.out.println("I have nothing to say about you");
break;
}
Here, age is passed to the switch.
Java then checks the cases one by one.
If age contains 18, this case matches:
case 18:
So Java executes:
System.out.println("Go and apply for driving license");
If age contains 60, then this case matches instead:
case 60:
and Java prints:
It's time to retire
What Is default?
You can think of default as the final option when none of the cases match.
For example, what happens if:
int age = 21;
There is no:
case 21:
So Java won't execute either case 18 or case 60.
Instead, it reaches:
default:
System.out.println("I have nothing to say about you");
The output will be:
I have nothing to say about you
So, in a simple way:
-
casehandles a particular value. -
defaulthandles the situation when no case matches.
Why Do We Need break?
This is one of the most important things to understand about the traditional switch statement.
Suppose we have:
int age = 60;
switch (age) {
case 18:
System.out.println("Go and apply for driving license");
break;
case 60:
System.out.println("It's time to retire");
case 45:
System.out.println("You have covered your half life");
break;
default:
System.out.println("I have nothing to say about you");
break;
}
The value of age is 60.
So Java finds:
case 60:
and executes:
System.out.println("It's time to retire");
But without a break, Java can continue executing the statements in the following cases.
So you may also see:
It's time to retire
You have covered your half life
This behavior is called fall-through.
To stop execution after the matching case, we use:
break;
So we should write:
case 60:
System.out.println("It's time to retire");
break;
Now, as soon as Java executes break, it exits the switch statement.
Real-World Example
Imagine a manufacturing machine with different status codes.
You could have:
int status = 2;
switch (status) {
case 1:
System.out.println("Machine is starting");
break;
case 2:
System.out.println("Machine is running");
break;
case 3:
System.out.println("Machine needs maintenance");
break;
default:
System.out.println("Unknown machine status");
break;
}
If the status is 2, Java finds case 2 and prints:
Machine is running
The break then stops Java from moving into the next case.
This is the kind of situation where a switch statement becomes quite readable. You have one value, and that value can represent different fixed options.
Switch Statement vs Else-If Ladder
The same age example could be written using an else-if ladder:
if (age == 18) {
System.out.println("Go and apply for driving license");
} else if (age == 60) {
System.out.println("It's time to retire");
} else {
System.out.println("I have nothing to say about you");
}
Or using a traditional switch:
switch (age) {
case 18:
System.out.println("Go and apply for driving license");
break;
case 60:
System.out.println("It's time to retire");
break;
default:
System.out.println("I have nothing to say about you");
break;
}
Both can solve this type of problem.
When there are many fixed cases, a switch statement can make the structure easier to read compared with a large else-if ladder.
Key Takeaway
The traditional switch statement allows us to compare one expression against multiple cases.
The important parts are:
switch → value being checked
case → possible matching value
default → no case matched
break → exit the switch
This is the traditional or old-school switch syntax discussed in the video. Modern Java also provides another way of writing switch logic that doesn't require break, which we'll look at next.