Conditional statements enable programs to make intelligent decisions at runtime based on dynamically changing input data. In Java, decision making is primarily governed by the if, if-else, if-else-if ladder, and nested if constructs.
Figure 4: Decision Tree and Execution Flow of Java Branching and Loop Constructs.
Executes a block of code strictly when the specified condition evaluates to true:
int accountBalance = 1500;
if (accountBalance > 1000) {
System.out.println("Sufficient funds for transaction!");
}
Provides a fallback alternative when the condition evaluates to false:
int age = 17;
if (age >= 18) {
System.out.println("Eligible to Vote!");
} else {
System.out.println("Underage: Not eligible to vote yet.");
}
Evaluates multiple mutually exclusive conditions in sequential top-down order. The moment a condition tests true, its block runs and the remaining branches are bypassed:
public class GradeCalculator {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Grade: A+ (Outstanding)");
} else if (score >= 80) {
System.out.println("Grade: A (Distinction)");
} else if (score >= 70) {
System.out.println("Grade: B (First Class)");
} else if (score >= 50) {
System.out.println("Grade: C (Second Class)");
} else if (score >= 40) {
System.out.println("Grade: D (Pass)");
} else {
System.out.println("Grade: F (Fail)");
}
}
}
A nested if is an if statement placed inside the body of an outer if or else block. It is useful when secondary checks depend strictly on the success of a primary validation:
boolean isAccountActive = true;
double withdrawalAmount = 5000.0;
double currentBalance = 12000.0;
if (isAccountActive) {
// Nested secondary check
if (withdrawalAmount <= currentBalance) {
currentBalance -= withdrawalAmount;
System.out.println("Withdrawal Successful! Remaining: $" + currentBalance);
} else {
System.out.println("Transaction Failed: Insufficient funds.");
}
} else {
System.out.println("Transaction Failed: Account is frozen or inactive.");
}
Excessively deep nesting (nesting 4+ levels of if statements) makes code unreadable. In enterprise code, prefer Guard Clauses (Early Return): validate error conditions upfront and return immediately, keeping the main happy-path logic unnested!
Exam Trap Question: "What is the output of this Java code snippet?"
boolean flag = false;
if (flag = true) {
System.out.println("TRUE");
} else {
System.out.println("FALSE");
}
Output: TRUE! Notice that the condition uses single equals = (assignment), NOT double equals == (comparison). The statement flag = true assigns true to flag, and the expression itself evaluates to true. Always use == or simply if (flag) for boolean comparisons!
Now let us explore multi-branch switching, modern arrow syntax, and Java 21 pattern matching for switch. Proceed to: Java Switch & Pattern Matching →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Multi-way branching alternative.
Constructing boolean expressions.
Conditional iterative execution.
Returning from conditional branches.