Make decisions with if/else and switch, and repeat work with for, while, and do-while loops — including break and continue.
Why: this is how a program makes decisions. The first condition that is true runs; the rest are skipped. else is the fallback when nothing matched.
int score = 82;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B"); // this one runs
} else {
System.out.println("C or below");
}Why: conditions are built from comparisons (== equal, != not equal, < > <= >=) joined with && (and), || (or), and ! (not). Note == for numbers — but remember strings use .equals().
int age = 25;
boolean hasTicket = true;
System.out.println(age >= 18 && hasTicket); // true — both must hold
System.out.println(age < 13 || age > 65); // false — either would do
System.out.println(!hasTicket); // false — flips the valueWhy: when you are checking one value against many fixed options, the modern switch is cleaner than a long if/else chain. The arrow form (->) runs just that branch with no fall-through, and can return a value.
String day = "SAT";
String kind = switch (day) {
case "SAT", "SUN" -> "weekend";
case "MON", "TUE", "WED", "THU", "FRI" -> "weekday";
default -> "unknown";
};
System.out.println(kind); // "weekend"Why: a for loop repeats a known number of times. It packs three parts into one line: a start (int i = 0), a condition to keep going (i < 5), and a step (i++) that runs after each pass.
for (int i = 0; i < 5; i++) {
System.out.println("Pass number " + i);
}
// prints 0, 1, 2, 3, 4Why: a while loop repeats as long as a condition stays true — use it when you do not know the count in advance. A do-while always runs at least once because it checks the condition at the bottom.
int countdown = 3;
while (countdown > 0) {
System.out.println(countdown);
countdown--;
}
int n = 0;
do {
System.out.println("runs once even though n is " + n);
} while (n > 0);Why: break stops a loop entirely; continue skips the rest of the current pass and jumps to the next. They let you exit early or ignore certain values without nesting more ifs.
for (int i = 1; i <= 10; i++) {
if (i == 7) break; // stop the whole loop at 7
if (i % 2 == 0) continue; // skip even numbers
System.out.println(i); // prints 1, 3, 5
}