if/else, the ternary operator and four loop forms — plus labeled break/continue for nested loops.
if / else if / else
public class Main {
static String grade(int score) {
if (score >= 90) return "A";
else if (score >= 75) return "B";
else if (score >= 50) return "C";
else return "F";
}
public static void main(String[] args) {
System.out.println(grade(95) + grade(80) + grade(60) + grade(10));
int n = 7;
String parity = (n % 2 == 0) ? "even" : "odd"; // ternary: an expression
System.out.println(n + " is " + parity);
}
}ABCF
7 is oddLoops at a glance
| Loop | Syntax | Use when |
|---|---|---|
for | for (int i = 0; i < n; i++) | You need the index or a counted range |
enhanced for | for (T x : arrayOrIterable) | Visiting every element, read-only |
while | while (cond) { … } | Unknown number of iterations; may run 0 times |
do-while | do { … } while (cond); | Body must run at least once (menus, retries) |
public class Main {
public static void main(String[] args) {
for (int i = 0, j = 10; i < j; i += 3, j -= 3) // several variables
System.out.print(i + ":" + j + " ");
System.out.println();
int[] nums = {3, 1, 4};
int sum = 0;
for (int x : nums) sum += x;
System.out.println("sum=" + sum);
int n = 1234, digits = 0;
while (n > 0) { n /= 10; digits++; }
System.out.println("digits=" + digits);
int k = 100;
do { System.out.println("runs once, k=" + k); } while (k < 10);
}
}0:10 3:7
sum=8
digits=4
runs once, k=100break, continue & labels
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) continue; // skip evens
if (i > 7) break; // stop the loop
System.out.print(i + " ");
}
System.out.println();
int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
outer:
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] == 5) {
System.out.println("found at " + r + "," + c);
break outer; // leaves BOTH loops
}
}
}
}
}1 3 5 7
found at 1,1Common mistakes
Off-by-one:
i <= arr.length→ArrayIndexOutOfBoundsException. Usei < arr.length.Modifying the loop variable inside an enhanced
fordoes nothing to the array —xis a copy.Removing from a
Listinside a for-each throwsConcurrentModificationException— use anIteratororremoveIf.A stray
;—for (…);orwhile (…);makes an empty body.Floating-point loop counters (
for (double d = 0; d != 1; d += 0.1)) may never terminate.
What is the difference between while and do-while?
How do you break out of nested loops?
Can the ternary operator replace every if/else?