break and continue
break exits the loop entirely; continue skips to the next iteration:
for (int i = 0; i < 10; i++) {
if (i == 5) break; // stop at 5
if (i % 2 == 0) continue; // skip evens
System.out.println(i);
}
Use break to stop early on a match; use continue to skip unwanted iterations without leaving the loop.
Nested loops and complexity
A loop inside a loop multiplies iterations:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.println(i + "," + j);
}
}
This is O(n²), 9 iterations for n=3. Nested loops are powerful but expensive; avoid nesting more than two levels deep in performance-critical code.