Control Flow & Loops

break, continue & Labeled Loops

15 min Lesson 8 of 14

break, continue & Labeled Loops

So far you have written loops that run from start to finish on their own schedule. But real programs often need to cut a loop short or skip a single turn. Java gives you two keywords for that — break and continue — plus a labeling mechanism that extends both keywords to work across nested loops.

break — leaving a loop early

When Java executes break, it immediately exits the innermost enclosing loop (or switch) and continues with the first statement after it. This is useful when you have found what you were looking for and further iterations would be wasted work.

// Find the first even number in an array int[] numbers = {3, 7, 11, 4, 9, 2}; int firstEven = -1; for (int n : numbers) { if (n % 2 == 0) { firstEven = n; break; // stop as soon as we find one } } System.out.println("First even: " + firstEven); // 4

Without break the loop would keep running through 9 and 2 even though the answer was already known at 4.

break only exits one level. If the for above were nested inside another loop, break would leave the inner loop but the outer loop would keep going. More on that below.

continue — skipping one iteration

continue does not exit the loop — it skips the rest of the current iteration's body and jumps straight to the loop's update step (in a for) or back to the condition check (in a while). Use it when a particular value is uninteresting and you want to move on.

// Print only odd numbers from 1 to 10 for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // skip even numbers } System.out.print(i + " "); } // Output: 1 3 5 7 9
Prefer positive conditions when possible. The same loop could be written as if (i % 2 != 0) { System.out.print(i + " "); } without continue. Use continue when it makes the logic noticeably clearer — for example, when skipping several lines of work.

break vs. continue at a glance

  • break — exit the loop entirely, run whatever comes after the loop.
  • continue — skip the rest of this iteration, then check the loop condition again.

Labeled break — escaping nested loops

Nested loops are common in matrix traversal, grid searches, and pattern printing. When you find your target inside the inner loop, a plain break only exits the inner loop — the outer loop keeps running. A labeled break lets you exit both loops at once.

A label is just an identifier followed by a colon, placed immediately before the loop you want to be able to exit:

// Search for a value in a 2-D grid int[][] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int target = 5; int foundRow = -1, foundCol = -1; outer: // label for the outer loop for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[row].length; col++) { if (grid[row][col] == target) { foundRow = row; foundCol = col; break outer; // exits BOTH loops } } } System.out.println("Found at row=" + foundRow + ", col=" + foundCol); // Found at row=1, col=1

After break outer Java jumps to the first statement after the outer for loop — row 2 is never visited.

Labeled continue — restarting the outer loop

You can also use a label with continue. Instead of jumping to the next iteration of the inner loop, Java jumps to the next iteration of the labeled (outer) loop.

// Skip any row that contains a negative number int[][] data = { {1, 2, 3}, {4, -1, 6}, // skip this row {7, 8, 9} }; rows: for (int r = 0; r < data.length; r++) { for (int c = 0; c < data[r].length; c++) { if (data[r][c] < 0) { continue rows; // go to next row immediately } } // Only reached if no negative was found in this row System.out.println("Clean row " + r); } // Clean row 0 // Clean row 2

Where to put labels

Labels must appear directly before a loop statement (for, while, do-while). They cannot label arbitrary blocks. By convention, label names are written in lowercase (outer, rows) to distinguish them from constants.

Do not overuse labels. They can make control flow hard to follow, especially with more than two loop levels. If your nested loop logic is complex, consider extracting the inner loop into a helper method and using a plain return — that is usually cleaner.

A complete worked example — prime sieve sketch

Here is a snippet that uses both break and continue together: it checks each candidate number and skips composites using continue, stopping at the first prime above a threshold.

int limit = 50; int firstPrimeAbove40 = -1; candidates: for (int n = 41; n <= limit; n++) { for (int d = 2; d * d <= n; d++) { if (n % d == 0) { continue candidates; // n is composite — try next candidate } } // Reached only if no divisor was found firstPrimeAbove40 = n; break candidates; // found it — stop outer loop too } System.out.println("First prime above 40 (up to " + limit + "): " + firstPrimeAbove40); // First prime above 40 (up to 50): 41

Summary

  • break exits the innermost loop immediately.
  • continue skips the rest of the current iteration and moves on.
  • A labeled break exits a specific outer loop by name.
  • A labeled continue restarts a specific outer loop's next iteration.
  • Labels are a power tool — use them when they genuinely simplify logic, and consider a helper method instead for very complex cases.