When you use break inside a nested loop, it only exits the innermost loop. If you need to break out of the outer loop from inside the inner one, you need a labeled break.
outer:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i + j == 4) {
break outer;
}
System.out.println(i + ", " + j);
}
}
The label outer: marks the outer for loop. When break outer runs, both loops exit entirely. Without the label, only the inner loop would stop and the outer loop would continue to its next iteration.
The same syntax works with continue. Writing continue outer skips the rest of the inner loop and moves to the next iteration of the outer loop. Labels are rarely needed, but when you have nested loops and need precise control, they're the cleanest solution.