The if / else if / else Statement
The if / else if / else Statement
Every program eventually needs to make a decision: run this code only if something is true, or choose between several paths depending on a value. Java's if statement is the fundamental tool for that kind of branching. You already know what a boolean expression produces (from the previous lesson); now you will learn how to act on it.
The basic if block
An if statement evaluates a boolean expression. When the expression is true, the body runs; when it is false, Java skips straight past it.
The curly braces { } define the block — the group of statements that belong to this branch. You can put as many statements inside as you need.
Adding an else branch
An else branch provides the fallback — the code that runs when the if condition is false.
Exactly one of the two branches will execute — never both, never neither. That is the guarantee the if / else structure gives you.
Chaining with else if
When there are more than two possible situations, you chain additional conditions with else if. Java evaluates them top to bottom and executes the first branch whose condition is true, then skips all the rest.
score >= 60 at the top, a score of 95 would print "Grade: D" — clearly wrong. Always think about which range is narrowest and put it first.
Nested if statements
You can place an if statement inside another if block. This is called nesting and is useful when a second decision only makes sense after the first one is resolved.
&&, ||) to combine conditions into a single if. Deep nesting makes code hard to read and hard to test.
The dangling-else trap
Consider this ambiguous-looking code:
The indentation looks like the else pairs with the outer if (x > 0), but Java's rule is: an else always belongs to the nearest preceding if that does not yet have an else. So the else here pairs with if (x > 10). For x = 5, the outer condition is true but the inner one is false, so the else runs and prints "Not positive" — even though 5 is positive. This is the classic dangling-else bug.
{ }. Then your indentation and the compiler's interpretation can never disagree.
The missing-braces trap
A related pitfall: omitting braces when you later add a second statement to a body.
The second println is outside the if entirely. Because there are no braces, only the first statement after if is conditional. "Loading admin panel..." will print for every user. This is a subtle, real-world security-style bug. The fix is the same: always use braces.
Putting it all together
Here is a small, self-contained example that uses branching to describe a traffic light:
== operator checks whether two variables point to the same object in memory, not whether their content is equal. For string content comparison always call .equals() or .equalsIgnoreCase().
Summary
ifruns a block only when its condition istrue.elseprovides the fallback path when no prior condition wastrue.else ifchains additional conditions; Java picks the first match and ignores the rest.- Conditions are evaluated top to bottom — put the most specific check first.
- Nesting is valid but should be kept shallow; prefer logical operators to combine conditions when possible.
- Always wrap
if,else if, andelsebodies in braces to avoid the dangling-else and missing-braces traps.