Control Flow
Without control statements a program runs its statements strictly top to bottom. Conditions choose which branch runs, loops repeat a block, and break and continue change the order from inside a loop. Conditions in Java are strictly typed: if and while accept only a boolean, and if (x) on a number does not compile, unlike in C or JavaScript.
The main fork of the topic is the two kinds of switch. The classic one, with colons, falls through from one branch into the next until it meets a break. The arrow form (Java 14) does not fall through and can return a value as an expression. Interviews check whether you understand the difference and why the compiler demands that a switch expression cover every value.
Topic map
- if, else and ?: — the condition is a
booleanonly, the trap of anifwithout braces, the conditional operator as an expression. - The Classic switch —
case,break,default, fall-through and the allowed selector types. - Switch Expressions —
case ->without fall-through, several labels,yieldand the demand to cover every value. - Loops —
while,do-while,forand the order of its header parts, for-each and the scope of the loop variable. - break and continue — leaving a loop, skipping to the next iteration and labels for nested loops.
Common traps
| Mistake | Consequence |
|---|---|
Writing if (x) or if (x = 5) for an int x | It does not compile: the condition must be a boolean |
Writing an if without braces around two statements | Only the first is under the condition; the second always runs |
Forgetting break in a classic switch | Execution falls through into the following cases up to the first break |
Leaving a switch expression over an int without default | It does not compile: the expression must cover every selector value |
Mixing case 1 -> and case 2: in one switch | It does not compile: arrow and colon labels do not mix |
Using the for variable after the loop | It does not compile: the variable exists only inside the loop |
Writing i <= a.length in a loop over an array | The last iteration throws ArrayIndexOutOfBoundsException |
Calling continue in a while before updating the counter | The loop repeats the same iteration forever |
Expecting a break inside a switch to stop the loop | break leaves only the switch; the loop needs a label |
Interview relevance
Nobody asks "how does if work". The topic is checked with output puzzles: a switch without break, a do-while with a false condition, continue together with break, a labeled break. The second layer of questions is the modern switch: how the arrow form differs from the classic one, what yield is and why a switch expression must be exhaustive.
Typical checks:
- Fall-through in the classic
switchand the role ofbreak. - The difference between a
switchstatement and a switch expression,yield, exhaustiveness. - Which types a
switchon constants accepts:intand the smaller integers,char, the wrappers,String, enums. whilevsdo-while, the order of theforparts, the scope of the loop variable.breakandcontinuein nested loops, and labels.
Common wrong answer: "break in a switch is just tidiness — without it only the matching branch runs anyway." That opens the discussion of fall-through: a classic switch jumps to the matching label and runs everything below it until a break or the end of the block.