Day 11: Understanding `break` and `continue` Statements in Java

Published: (January 8, 2026 at 09:07 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

What are break and continue?

In Java, break and continue are loop‑control statements that alter the normal flow of loops (for, while, do‑while) and switch blocks.

  • break – stops the loop (or switch) completely.
  • continue – skips the current iteration and proceeds to the next one.

break Statement

What is break?

The break statement terminates a loop or a switch block immediately when a specific condition is met. After break executes, control moves to the statement that follows the loop or switch.

Syntax of break

break;

break in a loop

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

Output

1
2
3
4

The loop stops when i becomes 5; numbers after 4 are not printed.

break in a switch statement

int day = 2;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    default:
        System.out.println("Invalid day");
}

break prevents execution from falling through to the next case. Without it, multiple cases would execute.


continue Statement

What is continue?

The continue statement skips the remainder of the current loop iteration and proceeds directly to the next iteration. The loop itself does not stop.

Syntax of continue

continue;

continue in a loop

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

Output

1
2
4
5

When i equals 3, the continue statement skips printing 3 and moves to the next iteration.


break vs continue

Featurebreakcontinue
Stops loopYesNo
Skips iterationNoYes
Control moves toAfter loopNext iteration
Usable in switchYesNo
  • break exits the loop (or switch) completely.
  • continue skips only the current iteration.

Both improve loop control and readability and are typically used with conditional statements (if).


When to use break

  • A required value is found during a search.
  • Input validation fails and the loop should stop.
  • The loop needs to terminate early for any logical reason.

When to use continue

  • Certain values should be ignored (e.g., filtering out even numbers).
  • Skipping invalid or unwanted data within a loop.
  • When only part of the iteration should be bypassed while the loop continues.

break and continue together

for (int i = 1; i <= 10; i++) {
    if (i == 3) {
        continue;   // skip printing 3
    }
    if (i == 8) {
        break;      // stop the loop when i reaches 8
    }
    System.out.println(i);
}

Output

1
2
4
5
6
7

continue skips the number 3, and break stops the loop before printing 8.


Common pitfalls

  • Forgetting break in switch cases, causing fall‑through.
  • Using continue without understanding how it affects loop flow, leading to unexpected results.
  • Accidentally creating infinite loops by misplacing break/continue.
  • Confusing the behaviors of break and continue.

Summary

Use break and continue when you need finer control over loop execution:

  • break – for early exit from a loop or switch.
  • continue – for skipping specific iterations while keeping the loop running.

Applying these statements thoughtfully leads to cleaner, more efficient Java code.

Back to Blog

Related posts

Read more »

Ruby 제어문 - 조건문과 반복문

조건문 ruby s1 = 'Jonathan' s2 = 'Jonathan' s3 = s1 if s1 == s2 puts 'Both Strings have identical content' else puts 'Both Strings do not have identical content'...

Iterando lo recursivo

Conversión de HTML reducido a Markdown con un árbol sintáctico En uno de mis proyectos laterales o “mascota” escribí un pequeño parser para un HTML reducido, p...