Sometimes one decision is not enough.
Imagine that a person has already passed the age requirement for an event. Now the system needs to check one more thing:
Is this person also a VIP customer?
So our system has to make a decision, and then inside that decision, make another decision.
This is where a nested if statement comes in.
What is a Nested if?
A nested if simply means:
An
ifstatement placed inside anotheriforelseblock.
For example:
if (condition1) {
if (condition2) {
// code
}
}
The second if is inside the first conditional block.
That's why it is called nested.
Think about entering a special event.
First, the security system asks:
Is the person 18 or older?
If not, access is denied.
If yes, the system asks another question:
Is the person a VIP?
So the decisions happen one after another.
Is age below 18?
↓
Yes → Access Denied
↓
No
↓
Is the user VIP?
↓ ↓
Yes No
↓ ↓
VIP Access Normal Access
Real-World Example
Let's create this exact system.
Our requirements are:
-
Under 18 → Access Denied
-
18 or older + VIP → Access Granted with VIP Permit
-
18 or older + not VIP → Access Granted without VIP Permit
First, let's create our variables:
int age = 35;
boolean isVIP = true;
Now we can write:
if (age < 18) {
System.out.println("Access Denied");
} else {
if (isVIP) {
System.out.println("Access Granted with VIP Permit");
} else {
System.out.println("Access Granted without VIP Permit");
}
}
There are actually two decision-making levels here.
First Decision
Java checks:
age < 18
If this is true, it prints:
Access Denied
and stops following this particular path.
The inner if is not needed because the user has already failed the age requirement.
Second Decision
If:
age < 18
is false, Java enters the outer else.
Inside that else, Java finds another if:
if (isVIP) {
...
} else {
...
}
Now Java checks whether the user is a VIP.
Case 1: User is an Adult and VIP
Suppose:
int age = 35;
boolean isVIP = true;
First condition:
35 < 18
is false.
So Java enters the outer else.
Then Java checks:
isVIP
The value is true.
Therefore:
Access Granted with VIP Permit
is printed.
Case 2: User is an Adult but Not VIP
Now suppose:
int age = 35;
boolean isVIP = false;
Again:
35 < 18
is false.
Java enters the outer else.
Then it checks:
isVIP
This time it is false.
Therefore, the inner else executes:
Access Granted without VIP Permit
Case 3: User is Under 18
Now suppose:
int age = 16;
boolean isVIP = true;
Java first checks:
16 < 18
This is true.
So Java immediately executes:
Access Denied
The VIP check is not reached.
Even though isVIP is true, the user does not get VIP access because the first condition already denied access.
This is the important idea behind nested conditions:
The inner condition is reached only when the program enters the block containing it.
Understanding the Structure
Look at the code again:
if (age < 18) {
System.out.println("Access Denied");
} else {
if (isVIP) {
System.out.println("Access Granted with VIP Permit");
} else {
System.out.println("Access Granted without VIP Permit");
}
}
The outer if-else makes the first decision:
Is the user under 18?
The inner if-else makes the second decision:
Is the user VIP?
So the program is effectively asking:
First:
Is the user allowed based on age?
Then:
If allowed, is the user VIP?
That's a nested if-else.
Key Takeaway
A nested if is simply an if placed inside another conditional block.
It is useful when one decision leads to another decision.
In our example:
Age check
↓
If allowed
↓
VIP check
So remember:
Nested
ifmeans one conditional decision inside another conditional block.