In the previous example, we used if to handle one situation.
For example:
If the user is under 18, show an underage message.
But there was a problem.
What should happen when the user is 18 or older?
With only an if statement, we weren't doing anything.
Now imagine a security gate. The rule is:
If the person is under 18, deny access. Otherwise, grant access.
Now we have two possible outcomes.
This is where the if-else statement becomes useful.
What is if-else?
The basic syntax is:
if (condition) {
// executes when condition is true
} else {
// executes when condition is false
}
The difference from a simple if is that else gives Java an alternative path.
Think about it like this:
Condition
↓
┌──────┴──────┐
true false
↓ ↓
if block else block
If the condition is true, the if block executes.
If the condition is false, the else block executes.
And notice something important:
elsedoes not have a condition.
Why?
Because else represents what should happen when the if condition is not satisfied.
Real-World Example: Access Control
Let's say we are building a simple access-control system.
Our requirement is:
-
Under 18 → Access Denied
-
18 or older → Access Granted
We can write:
if (age < 18) {
System.out.println("Access Denied");
} else {
System.out.println("Access Granted");
}
Suppose the user enters:
12
Java checks:
12 < 18
The condition is true.
Therefore, the if block executes:
Access Denied
Now suppose the user enters:
45
Java checks:
45 < 18
The condition is false.
So Java skips the if block and executes the else block.
Output:
Access Granted
Why Do We Need else?
Let's compare this with our previous if example.
With only if:
if (age < 18) {
System.out.println("Access Denied");
}
If the age is 12, we get:
Access Denied
But if the age is 45, nothing happens.
Now add else:
if (age < 18) {
System.out.println("Access Denied");
} else {
System.out.println("Access Granted");
}
Now every situation has an outcome.
Age < 18
↓
Access Denied
Age >= 18
↓
Access Granted
This makes if-else particularly useful when you have two possible paths.
Understanding the Execution Flow
Let's say:
int age = 16;
if (age < 18) {
System.out.println("Access Denied");
} else {
System.out.println("Access Granted");
}
Java starts with the condition:
age < 18
Substitute the value:
16 < 18
This is true.
So Java executes the if block.
It does not execute the else block.
Now change the age:
int age = 25;
The condition becomes:
25 < 18
This is false.
So Java executes the else block.
It does not execute the if block.
Therefore, in an if-else statement, Java chooses between the two blocks based on the condition.
Another Real-Life Way to Think About It
Imagine a door with an age restriction.
The system asks:
Is the person under 18?
If yes:
Access Denied
Otherwise:
Access Granted
There is no third option in our current requirement.
That's exactly what if-else represents.
IF this condition is true
→ do this
ELSE
→ do the other thing
Key Takeaway
Use if-else when your program needs to handle two possible outcomes.
if (condition) {
// first possibility
} else {
// second possibility
}
Remember:
ifhandles the condition being true, whileelsehandles the condition being false.