So far, we have seen situations where we had one or two possible outcomes.
But what happens when our program needs to choose between multiple possibilities?
For example, imagine we are creating different sections in an event based on the user's age.
Our requirements are:
-
Below 13 → Children section
-
13 to 17 → Teenage section
-
18 to 50 → Adult section
-
Above 50 → Senior section
Now we have several possible outcomes.
Using only one if and one else would not be enough.
This is where the else-if ladder comes in.
What is an else-if Ladder?
An else-if ladder allows us to check multiple conditions one after another.
Its basic structure is:
if (condition1) {
} else if (condition2) {
} else if (condition3) {
} else {
}
You can have multiple else if conditions.
The structure is:
if
↓
else if
↓
else if
↓
else
Java checks the conditions one by one.
When a condition is satisfied, its block is executed.
Real-World Example: Age-Based Sections
Let's say an event has four different sections.
Below 13 → Children
13 to 17 → Teenage
18 to 50 → Adult
Above 50 → Senior
We can implement this using an else-if ladder:
if (age < 13) {
System.out.println("You will be going in children section");
} else if (age >= 13 && age <= 17) {
System.out.println("You will be going in teenage section");
} else if (age >= 18 && age <= 50) {
System.out.println("You will be going in adult section");
} else {
System.out.println("You are going in senior section");
}
Now let's understand how Java goes through this.
When the Age is 10
Suppose:
int age = 10;
Java starts with:
if (age < 13)
That becomes:
10 < 13
The condition is true.
So Java executes:
You will be going in children section
It doesn't need to check the remaining conditions for this decision.
When the Age is 15
Now:
int age = 15;
First condition:
15 < 13
This is false.
So Java moves to the next condition:
age >= 13 && age <= 17
For age 15:
15 >= 13 → true
15 <= 17 → true
So the condition is satisfied.
Output:
You will be going in teenage section
When the Age is 19
Suppose:
int age = 19;
First condition:
19 < 13
False.
Second condition:
19 >= 13 && 19 <= 17
False.
Java then checks:
19 >= 18 && 19 <= 50
This is true.
Therefore:
You will be going in adult section
is printed.
When the Age is 51
Now suppose:
int age = 51;
Java checks the conditions one by one.
First condition
51 < 13
False.
Second condition
51 >= 13 && 51 <= 17
False.
Third condition
51 >= 18 && 51 <= 50
False.
There are no more else if conditions.
So Java reaches the final else.
The output becomes:
You are going in senior section
This is the purpose of the final else: it handles the case where none of the previous conditions are satisfied.
How to Visualize an else-if Ladder
You can imagine a person standing in front of several doors.
The system checks each door's condition.
Age < 13?
↓
Yes → Children
No
↓
Age 13–17?
↓
Yes → Teenage
No
↓
Age 18–50?
↓
Yes → Adult
No
↓
Senior
The program keeps moving through the conditions until it finds the applicable path.
Why Is It Called a Ladder?
Look at the structure:
if (condition1) {
} else if (condition2) {
} else if (condition3) {
} else {
}
The repeated else if conditions create a structure that looks like a ladder:
if
|
else if
|
else if
|
else
That's why it is commonly called an else-if ladder or if-else ladder.
One Important Point
The else if conditions are placed between the initial if and the final else.
For example:
if (condition1) {
} else if (condition2) {
} else if (condition3) {
} else {
}
You can use more than one else if.
So if your program has several possible conditions, you can keep adding additional else if blocks.
In our example, we needed three conditions before reaching the final else:
Children
↓
Teenage
↓
Adult
↓
Senior
Key Takeaway
Use an else-if ladder when your program needs to check multiple possible conditions.
The general pattern is:
if (condition1) {
// first possibility
} else if (condition2) {
// second possibility
} else if (condition3) {
// third possibility
} else {
// remaining possibility
}
The easiest way to remember it is:
ifchecks the first possibility,else ifchecks additional possibilities, andelsehandles the remaining case.