How to use break and continue

Stop with break

break exits the nearest loop immediately.

for (int n = 1; n <= 10; n++) {
    if (n == 4) {
        break;
    }
    System.out.println(n);
}

Skip with continue

continue jumps to the next iteration of the nearest loop.

for (int n = 1; n <= 5; n++) {
    if (n == 3) {
        continue;
    }
    System.out.println(n);
}

Search efficiently

Break once a match is found so unnecessary iterations do not run.

int[] values = {4, 7, 9};
for (int value : values) {
    if (value == 7) {
        System.out.println("Found");
        break;
    }
}

Keep control flow readable

Use break and continue for clear early exits. If nested loop control becomes difficult to follow, move the work into a method.