break and continue

Loop control statements

Interview Relevant: Understanding flow control is essential
5 min read

Loop Control Statements

Java provides two keywords to control loop execution: break and continue.

break Statement

  • Immediately exits the loop
  • Execution continues after the loop
  • Also used in switch statements

continue Statement

  • Skips the current iteration
  • Jumps to the next iteration
  • Loop continues running

🔑 Memory Aid: break = "I'm done with this loop!" | continue = "Skip this one, next please!"

⚠️ Best Practice: Overusing break and continue can make code harder to read. Consider restructuring logic instead.

Code Examples

Using break to exit loop early

java
1// break - exit loop early
2for (int i = 1; i <= 10; i++) {
3    if (i == 5) {
4        break;  // Exit when i is 5
5    }
6    System.out.print(i + " ");
7}
8// Output: 1 2 3 4
9
10// Find first even number
11int[] numbers = {1, 3, 5, 8, 9, 10};
12for (int num : numbers) {
13    if (num % 2 == 0) {
14        System.out.println("First even: " + num);  // 8
15        break;
16    }
17}

Using continue to skip iterations

java
1// continue - skip current iteration
2for (int i = 1; i <= 10; i++) {
3    if (i % 2 == 0) {
4        continue;  // Skip even numbers
5    }
6    System.out.print(i + " ");
7}
8// Output: 1 3 5 7 9
9
10// Skip specific values
11String[] names = {"Alice", "", "Bob", null, "Charlie"};
12for (String name : names) {
13    if (name == null || name.isEmpty()) {
14        continue;  // Skip invalid names
15    }
16    System.out.println("Hello, " + name);
17}

break only exits the innermost loop

java
1// break in nested loops - only exits inner loop!
2for (int i = 1; i <= 3; i++) {
3    System.out.println("Outer: " + i);
4    for (int j = 1; j <= 3; j++) {
5        if (j == 2) {
6            break;  // Only exits inner loop
7        }
8        System.out.println("  Inner: " + j);
9    }
10}
11/*
12Outer: 1
13  Inner: 1
14Outer: 2
15  Inner: 1
16Outer: 3
17  Inner: 1
18*/

Practical search and while loop examples

java
1// Practical example: Search in 2D array
2int[][] matrix = {
3    {1, 2, 3},
4    {4, 5, 6},
5    {7, 8, 9}
6};
7int target = 5;
8boolean found = false;
9
10for (int i = 0; i < matrix.length && !found; i++) {
11    for (int j = 0; j < matrix[i].length; j++) {
12        if (matrix[i][j] == target) {
13            System.out.println("Found at [" + i + "][" + j + "]");
14            found = true;
15            break;  // Exit inner loop
16        }
17    }
18}
19
20// continue in while loop
21int count = 0;
22while (count < 10) {
23    count++;
24    if (count % 3 == 0) {
25        continue;  // Skip multiples of 3
26    }
27    System.out.print(count + " ");
28}
29// Output: 1 2 4 5 7 8 10

Use Cases

  • Early termination when target found (break)
  • Skipping invalid or unwanted data (continue)
  • Breaking out of infinite loops
  • Input validation loops
  • Search algorithms
  • Filtering during iteration

Common Mistakes to Avoid

  • Expecting break to exit nested loops (it only exits one)
  • Forgetting that continue skips to next iteration, not exit
  • Using break/continue when restructuring logic is cleaner
  • Infinite loops with continue but no update
  • Not considering labeled breaks for nested loops
  • Overusing break/continue making code hard to follow