Metadata Card
- Prerequisites: Ch3 (Expressions & Operators)
- Estimated time: 50 min
- Core difficulty:
- Reading mode: Steady progress
- Completion marker: Can write if/else, switch, for, while loops; understand break/continue
The Breakthrough · Tracing the Origins
Variables hold data. Expressions compute values. But programs need to make decisions and repeat tasks — that's control flow.
If/Else — Making Decisions
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}Language: Java Expected output: You are an adult.
Chained conditions:
int score = 85;
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");
}Expected output: B
Ternary operator — shorthand if/else:
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
// Equivalent to:
// String status;
// if (age >= 18) { status = "Adult"; } else { status = "Minor"; }Switch — Multiple Branches
int day = 3;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid day";
};
System.out.println(dayName); // WednesdayLanguage: Java 14+ (switch expression with ->) Expected output: Wednesday
Loops — Repetition
For loop — when you know how many times:
for (int i = 0; i < 5; i++) {
System.out.println("Count: " + i);
}Expected output: Count: 0, 1, 2, 3, 4 (each on its own line)
While loop — when you have a condition:
int i = 0;
while (i < 5) {
System.out.println("i = " + i);
i++;
}Do-while — executes at least once:
int i = 0;
do {
System.out.println("Will run even if condition is false...");
i++;
} while (i < 0);Expected output: Will run even if condition is false...
Break and Continue
// break — exit the loop
for (int i = 0; i < 10; i++) {
if (i == 5) break;
System.out.print(i + " ");
} // Output: 0 1 2 3 4
// continue — skip to next iteration
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.print(i + " ");
} // Output: 0 1 3 4Python window: Python uses
elifinstead ofelse if,matchinstead of switch (3.10+), and the samefor/while/break/continuesemantics.C++ window: C++ control flow is identical to Java in syntax. Switch in C++ can only handle integral types (not strings).
Common Pitfalls
- Using
=instead of==:if (x = 5)assigns 5 to x and always evaluates to true. Java catches this (boolean expressions only), but many C-descended languages don't. - Off-by-one in loops:
for (int i = 0; i <= 5; i++)runs 6 times, not 5. - Forgetting
breakin switch (traditional syntax): Withoutbreak, execution "falls through" to the next case.
Final Challenge
- Print all even numbers between 1 and 20 using a for loop
- Write a program that reads numbers from the user until they enter 0, then prints the sum
- Use a nested loop to print a multiplication table (1-9)
Traveler's Notes
if/else for decisions, loops for repetition, switch for many branches.
The ternary operator? :is a concise if/else expression.breakexits a loop;continueskips to the next iteration.
Off-by-one errors are the most common loop bug.