Skip to content

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

java
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:

java
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:

java
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
// Equivalent to:
// String status;
// if (age >= 18) { status = "Adult"; } else { status = "Minor"; }

Switch — Multiple Branches

java
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); // Wednesday

Language: Java 14+ (switch expression with ->) Expected output: Wednesday

Loops — Repetition

For loop — when you know how many times:

java
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:

java
int i = 0;
while (i < 5) {
 System.out.println("i = " + i);
 i++;
}

Do-while — executes at least once:

java
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

java
// 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 4

Python window: Python uses elif instead of else if, match instead of switch (3.10+), and the same for/while/break/continue semantics.

C++ window: C++ control flow is identical to Java in syntax. Switch in C++ can only handle integral types (not strings).

Common Pitfalls

  1. 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.
  2. Off-by-one in loops: for (int i = 0; i <= 5; i++) runs 6 times, not 5.
  3. Forgetting break in switch (traditional syntax): Without break, execution "falls through" to the next case.

Final Challenge

  1. Print all even numbers between 1 and 20 using a for loop
  2. Write a program that reads numbers from the user until they enter 0, then prints the sum
  3. 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.
break exits a loop; continue skips to the next iteration.
Off-by-one errors are the most common loop bug.

Built with VitePress | Software Systems Atlas