if/else and switch
Branching logic in Java, from simple conditionals to the modern switch expression.
2 min read
Branching in Java will look familiar from most other C-family languages, but Java's switch has evolved significantly in recent versions and is worth learning in its current form rather than only the old one.
if, else if, and else
int score = 82;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 70) {
System.out.println("C");
} else {
System.out.println("F");
}Conditions must evaluate to a boolean — Java doesn't treat 0 or null as automatically falsy the way some languages do. if (score) where score is an int simply won't compile.
Braces are technically optional for a single-statement body, but leaving them off is a common source of bugs when someone later adds a second line expecting it to be part of the block:
// Risky: looks like both lines are conditional, but only the first is
if (score >= 90)
System.out.println("A");
System.out.println("Great job!"); // always runs, regardless of scoreAlways use braces, even for one-liners — it costs nothing and removes an entire category of mistake.
Classic switch statements
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Some other day");
break;
}The classic switch falls through by default — without break, execution continues into the next case. This is a frequent source of bugs, which is exactly why the modern syntax below was introduced.
Modern switch expressions
Since Java 14, switch can be used as an expression with arrow syntax, no fall-through, and no break needed:
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Some other day";
};Each case returns its value directly, and only the matching branch runs — no accidental fall-through possible. When a case needs multiple statements, use a block with yield to produce the value:
String size = switch (count) {
case 0 -> "empty";
default -> {
String label = count > 100 ? "large" : "small";
yield label;
}
};For any new code, prefer the arrow-based switch expression over the classic statement — it's shorter, safer, and makes the compiler check that every case is handled when used with an exhaustive type like an enum.
The ternary operator
For a simple two-way choice that produces a value, the ternary operator (condition ? valueIfTrue : valueIfFalse) is often more concise than a full if/else:
int score = 82;
String result = (score >= 70) ? "Pass" : "Fail";This is equivalent to a four-line if/else that just assigns to result in each branch, but it reads as a single expression. Reserve it for genuinely simple choices — nesting ternaries inside one another quickly becomes harder to read than the if/else it was meant to shorten.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.