Java If-Else — Conditional Statements Explained
Conditional statements let your program make decisions. The if-else statement is the most fundamental control structure in Java.
The if Statement
Executes a block only if the condition is true.
int temperature = 35;
if (temperature > 30) {
System.out.println("It's hot outside!");
}
Syntax:
if (condition) {
// code to execute when condition is true
}
The condition must be a boolean expression.
if-else
Provides an alternative when the condition is false.
int age = 16;
if (age >= 18) {
System.out.println("You can vote.");
} else {
System.out.println("You cannot vote yet.");
}
else if — Multiple Conditions
int score = 75;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
// Output: Grade: C
Java checks conditions top to bottom and executes the first matching block.
Nested if
boolean hasTicket = true;
int age = 15;
if (hasTicket) {
if (age >= 18) {
System.out.println("Welcome to the movie.");
} else {
System.out.println("You need a guardian.");
}
} else {
System.out.println("Please buy a ticket.");
}
Tip: Avoid deep nesting (more than 2-3 levels). Refactor using early returns or separate methods.
Single-Statement if (No Braces)
if (age >= 18)
System.out.println("Adult");
This works, but always use braces {} — even for single statements:
// Dangerous — the second line ALWAYS runs
if (age >= 18)
System.out.println("Adult");
System.out.println("Can vote"); // NOT inside the if!
// Safe — intent is clear
if (age >= 18) {
System.out.println("Adult");
System.out.println("Can vote");
}
Common Patterns
Guard Clauses (Early Return)
Instead of deep nesting, return early:
// Deep nesting — hard to read
public void processOrder(Order order) {
if (order != null) {
if (order.isValid()) {
if (order.hasStock()) {
// actual logic here
}
}
}
}
// Guard clauses — flat and clear
public void processOrder(Order order) {
if (order == null) return;
if (!order.isValid()) return;
if (!order.hasStock()) return;
// actual logic here
}
Range Checking
public static String getAgeGroup(int age) {
if (age < 0) return "Invalid";
if (age < 13) return "Child";
if (age < 18) return "Teenager";
if (age < 65) return "Adult";
return "Senior";
}
Null Checking
if (name != null && !name.isEmpty()) {
System.out.println("Hello, " + name);
} else {
System.out.println("Hello, stranger");
}
The Ternary Operator (Recap)
A shorthand for simple if-else:
String status = (age >= 18) ? "Adult" : "Minor";
Use it only for simple, one-line decisions.
Common Mistakes
1. Using = Instead of ==
// WRONG — assignment, not comparison
if (x = 5) { } // compile error in Java
// CORRECT
if (x == 5) { }
2. Comparing Strings with ==
String input = scanner.nextLine();
// WRONG
if (input == "yes") { }
// CORRECT
if (input.equals("yes")) { }
// or
if ("yes".equals(input)) { } // null-safe
3. Unnecessary Boolean Comparison
// Bad
if (isReady == true) { }
// Good
if (isReady) { }
4. Empty if Body
// Bug — the semicolon makes the if do nothing
if (x > 5);
{
System.out.println("Greater"); // ALWAYS runs
}
Quick Exercises
- Write a program that reads a number and prints “Positive”, “Negative”, or “Zero”.
- Write a program that determines if a year is a leap year.
- Refactor this nested if into guard clauses:
if (user != null) { if (user.isActive()) { if (user.hasPermission("admin")) { grantAccess(); } } }
Summary
| Structure | When to Use |
|---|---|
if |
Single condition |
if-else |
Two alternatives |
else if |
Multiple conditions |
Nested if |
Conditions within conditions |
Ternary ? : |
Simple inline decisions |
| Guard clauses | Early exit to avoid nesting |
What’s Next?
When you have many specific values to check, if-else chains get long. That’s where switch comes in: Switch Statement.