What you'll learn
So far our programs run top to bottom, doing the same thing every time. Real programs make decisions — showing different results for different inputs. This module is all about control flow: teaching your code to choose a path.
By the end you'll be able to:
- Build boolean conditions from comparisons and logical operators
- Branch with
if,if-else, andelse-ifchains - Write compact choices with the ternary operator
- Select among many options with both traditional and modern
switch - Use pattern matching in switch (Java 21)
- Avoid the classic control-flow bugs that catch every beginner
Boolean expressions
Every decision rests on a boolean expression — something that evaluates to true or false. You build these from the comparison operators (==, !=, >, <, >=, <=) and combine them with the logical operators (&&, ||, !) you met in Module 2. A condition like age >= 18 && hasLicense is just a boolean the program can act on.
if, if-else & else-if
The if statement runs a block only when its condition is true. Add else for the "otherwise" path, and chain else if to test several conditions in order — the first one that's true wins, and the rest are skipped.
int score = 82;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}Tip
{ }, even for a single line. It prevents a whole class of bugs where someone later adds a second line that looks like it's inside the if but isn't.The ternary operator
When an if-else just picks between two values, the ternary operator does it in one line. Read condition ? a : b as "if condition then a, else b":
int age = 20;
String status = (age >= 18) ? "adult" : "minor";
System.out.println(status);
int a = 7, b = 12;
int max = (a > b) ? a : b;
System.out.println("Max: " + max);Note
if-else — readability beats cleverness.switch statements & expressions
When you're comparing one variable against many fixed options, a long else-if chain gets noisy. switch is built exactly for this. The traditional form uses case x: labels and requires break after each one:
int day = 3;
String name;
switch (day) {
case 1: name = "Monday"; break;
case 2: name = "Tuesday"; break;
case 3: name = "Wednesday"; break;
default: name = "Unknown";
}
System.out.println(name);Modern Java (14+) offers a cleaner switch expression using the arrow -> syntax. There's no break, no accidental fall-through, and — best of all — it can return a value you assign straight to a variable:
int day = 3;
String name = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Unknown";
};
System.out.println(name);Traditional switch
- • Uses
case x:and needsbreak; - • Forgetting
breakcauses fall-through - • A statement — can't return a value directly
Modern switch expression
- • Uses
case x ->, nobreakneeded - • No fall-through — safer by default
- • An expression — can be assigned to a variable
Pattern matching in switch
Recent Java (21+) supercharges switch so it can branch on an object's type, binding it to a variable in the same step. This replaces clunky chains of instanceof checks with something clean and readable:
Object value = 42;
String result = switch (value) {
case Integer i -> "int: " + i;
case String s -> "text of length " + s.length();
case null -> "nothing";
default -> "something else";
};
System.out.println(result);Note
switch has become.Common control-flow mistakes
Watch out for these — they account for a huge share of beginner bugs:
= vs ==
= assigns; == compares. Writing if (x = 5) assigns 5 to x instead of testing it. (Java catches this for non-booleans, but not always.)Missing break (traditional switch)
break and execution "falls through" into the next case, running code you didn't intend. The arrow-style switch expression avoids this entirely.Comparing doubles with ==
0.1 + 0.2 == 0.3 is false. Compare decimals by checking the difference is tiny: Math.abs(a - b) < 1e-9.Recap & quick check
Key takeaways
- Decisions run on boolean expressions built from comparison and logical operators.
- if / else if / else tests conditions in order — the first true branch wins.
- The ternary condition ? a : b is a one-line value choice.
- Prefer the arrow switch expression: no break, no fall-through, and it can return a value.
- Beware = vs ==, missing breaks, and comparing doubles with ==.
Quick check
1. In an else-if chain, how many branches run?
2. What does 'condition ? a : b' return when the condition is false?
3. Which switch style requires 'break' to avoid fall-through?
4. Why can '0.1 + 0.2 == 0.3' be false?
5. What's the difference between = and == ?
Nicely done — your programs can now think for themselves. Next up: Module 5 — Loops, where you'll make code repeat work automatically.