1. Introduction
The if-else statement is Java's fundamental decision-making construct. It allows a program to execute different blocks of code depending on whether a boolean condition evaluates to true or false, letting programs react intelligently to varying input and state.
Cricket analogy: The if-else statement is like a captain deciding whether to review an umpire's decision: if the ball clearly hit the stumps, review; else, accept the call — the program branches based on a true/false read of the situation.
Java provides several forms of conditional branching built on if: the plain if, the if-else pair, the else-if ladder for multiple alternatives, and nested if statements for checking conditions within conditions.
Cricket analogy: Java's conditional forms mirror match-day decisions: a plain if is 'if it rains, cover the pitch'; if-else adds an alternative like 'else play on'; an else-if ladder handles multiple weather thresholds; and a nested if checks sub-conditions, like whether the outfield is also wet once rain is confirmed.
2. Syntax
// Simple if
if (condition) {
// executes when condition is true
}
// if-else
if (condition) {
// true branch
} else {
// false branch
}
// else-if ladder
if (condition1) {
// branch 1
} else if (condition2) {
// branch 2
} else {
// default branch
}
// nested if
if (outerCondition) {
if (innerCondition) {
// runs only if both are true
}
}3. Explanation
The condition inside the parentheses of an if statement must be a boolean expression (an expression of type boolean, or one that evaluates to true/false). Unlike C or C++, Java does not allow integers to be used directly as conditions — if (1) is a compile-time error.
Cricket analogy: Unlike an old-school scorer who might accept a scribbled '1' as shorthand for 'out', Java's if statement insists on a proper boolean verdict — if (1) is rejected outright, just as an umpire must give a clear out/not-out, never a number.
In an else-if ladder, conditions are evaluated top to bottom, and the first one that evaluates to true has its block executed; all remaining branches are skipped. If no condition matches, the final else block (if present) runs.
Cricket analogy: In an else-if ladder, umpires check appeals top to bottom — lbw first, then caught behind, then run out — and the moment one condition is confirmed true, that's the decision given and the rest are skipped; if none apply, the batter is given not-out by default.
Exam trap: Java requires boolean expressions in if conditions — writing if (x = 5) (assignment instead of ==) is a compile error in Java because the assignment result is an int, not a boolean, unlike C where this silently compiles.
Braces {} are optional when a branch contains a single statement, but omitting them is a common source of bugs (the classic 'dangling else' problem) and is generally discouraged in production code.
4. Example
public class GradeChecker {
public static void main(String[] args) {
int marks = 76;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 75) {
System.out.println("Grade: B");
} else if (marks >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
// Nested if example
boolean hasPassed = marks >= 40;
if (hasPassed) {
if (marks >= 75) {
System.out.println("Eligible for merit scholarship");
} else {
System.out.println("Passed but not eligible for scholarship");
}
} else {
System.out.println("Did not pass");
}
}
}5. Output
Grade: B
Passed but not eligible for scholarship6. Key Takeaways
- if conditions must be boolean expressions; Java does not implicitly convert int to boolean.
- else-if ladders execute only the first matching branch, then skip the rest.
- Nested if statements let you test a condition only after an outer condition is satisfied.
- Omitting braces for single-statement blocks is legal but risky and best avoided.
- The final else in a ladder acts as a catch-all default case.
- Assignment (
=) inside a condition is a compile-time type error in Java, unlike some other languages.
Practice what you learned
1. Which of the following is a valid if condition in Java?
2. In an else-if ladder, how many branches execute if multiple conditions are true?
3. What happens if you write `if (1) { }` in Java?
4. What is a nested if statement used for?
5. Are curly braces mandatory for a single-statement if branch in Java?
Was this page helpful?
You May Also Like
switch Statement in Java
Master Java's switch statement for multi-way branching, covering classic case-break syntax, fall-through behavior, and modern Java 14+ switch expressions.
Operators in Java
Explore Java's arithmetic, relational, logical, bitwise, assignment, unary, ternary, and instanceof operators along with operator precedence.
for Loop in Java
Understand Java's classic three-part for loop and the enhanced for-each loop for iterating over arrays and collections concisely.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics