Lesson · Control Flow
Conditional statements: branching without the traps
10 min readConditional Statements
if, else-if chains, the classic switch and its fall-through, plus the dangling else and comparison mistakes that still ship to production every year.
if, else, and the chain
An if takes a boolean expression and runs a block when it is true. Java does not treat ints or objects as truthy: the condition must be a boolean, which removes a whole family of C bugs before they exist. An else attaches to the nearest unmatched if, which is exactly how the dangling-else trap works when braces are omitted.
Chains of else if evaluate top to bottom and stop at the first match, so order is behaviour: put the most specific condition first. When the chain checks ranges, later overlap means an unreachable branch, and the compiler will not warn. if can also take a single statement without braces, and every style guide says: always use braces, because the next edit will add a second statement.
The classic switch and fall-through
The statement-form switch takes an int, char, String or enum, compares with equality, and jumps to the matching case. Execution then continues into the NEXT case unless you write break. That is fall-through, and it is a bug generator far more often than a feature.
The values in cases must be compile-time constants (literals, enum names, final constants), and duplicates are compile errors. A default is optional in the statement form. Everything else about switch, arrow labels, expressions and yield, belongs to the modern switch topic.
The traps worth memorising
Equality versus assignment: if (x = 1) does not compile in Java for booleans? Actually it does if both sides are boolean: if (flag = next()) assigns and tests, which is occasionally deliberate and usually a typo. Comparing objects with == is the bigger classic: use equals for content, with the constant first ("yes".equals(input)) to survive null.
- Braces always: the dangling-else and the one-line edit both disappear.
- equals for content:
==compares references for objects, and interned strings make it *accidentally* work sometimes. - Constant first:
"yes".equals(answer)never throws on null;answer.equals("yes")does. - Early return over deep nesting: guard clauses keep the happy path at one level.
The missing break is a run-through
Forget a break in a classic switch and execution falls into the next case, running code that clearly does not belong. The compiler says nothing. Modern arrow-form switch removes the trap entirely, which is one of several reasons to prefer it.
Interview angles
What can a classic switch statement switch on?+
int (and byte, short, char via promotion), String, and enum. Not long, float, double or boolean. Case labels must be compile-time constants, and duplicates are compile errors.
What is fall-through and why does it exist?+
Without break, control continues into the next case body. It exists from C for deliberate shared-handling, and in practice it mostly produces bugs when a break goes missing. The arrow-form switch eliminates it by design.
How do you compare strings in an if?+
equals (or equalsIgnoreCase), never ==. == compares references, and it sometimes appears to work because literals share the pool. Write the literal first: "yes".equals(input) is null-safe.