DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Part 1: `break` and `continue`

When writing loops and switch statements, there are situations where you don't want the normal flow of execution.

Sometimes you want to:

  • Exit a loop immediately.
  • Skip the current iteration.
  • Prevent fall-through in a switch.

Java provides transfer statements to transfer program control from one location to another.

In this article, we'll learn the two most commonly used transfer statements:

  • break
  • continue

We'll cover where they can be used, how they work, common mistakes, and interview questions.


What Are Transfer Statements?

Transfer statements change the normal execution flow of a program.

Instead of executing statements sequentially, they transfer control to another part of the program.

Java provides four forms:

  • break
  • continue
  • Labeled break
  • Labeled continue

In this article, we'll focus on the first two.


The break Statement

The break statement immediately terminates the nearest enclosing structure.

Depending on where it's used, it can:

  • Exit a switch
  • Exit a loop
  • Exit a labeled block (covered in Part 2)

Where Can break Be Used?

break is allowed only in these places:

Location Purpose
switch statement Stop fall-through
Loops (for, while, do-while) Exit the loop immediately
Labeled blocks Exit the labeled block (covered later)

Using break anywhere else results in a compile-time error.


Using break Inside a switch

Without break, execution continues into the next case.

This behavior is called fall-through.

Example

int option = 1;

switch (option) {

    case 1:
        System.out.println("Java");
        break;

    case 2:
        System.out.println("Spring Boot");

}
Enter fullscreen mode Exit fullscreen mode

Output

Java
Enter fullscreen mode Exit fullscreen mode

Because of break, execution leaves the switch after printing "Java".


What Happens Without break?

int option = 1;

switch (option) {

    case 1:
        System.out.println("Java");

    case 2:
        System.out.println("Spring Boot");

}
Enter fullscreen mode Exit fullscreen mode

Output

Java
Spring Boot
Enter fullscreen mode Exit fullscreen mode

Execution "falls through" into the next case.


Using break Inside a Loop

The break statement immediately terminates the loop.

Example

for (int number = 1; number <= 10; number++) {

    if (number == 6) {
        break;
    }

    System.out.println(number);

}
Enter fullscreen mode Exit fullscreen mode

Output

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

As soon as number becomes 6, the loop stops completely.


Execution Flow

for (int number = 1; number <= 5; number++) {

    if (number == 3) {
        break;
    }

    System.out.println(number);

}
Enter fullscreen mode Exit fullscreen mode
Iteration Condition Output
1 1 == 3 → false 1
2 2 == 3 → false 2
3 3 == 3 → true Loop ends

Using break Outside a Loop or switch

This is illegal.

int age = 25;

if (age > 18) {

    break;

}
Enter fullscreen mode Exit fullscreen mode

Compile-time error

break outside switch or loop
Enter fullscreen mode Exit fullscreen mode

break only works inside:

  • loops
  • switch
  • labeled blocks

The continue Statement

Unlike break, the continue statement does not terminate the loop.

Instead, it skips the current iteration and starts the next one.


Example

for (int number = 1; number <= 10; number++) {

    if (number % 2 == 0) {
        continue;
    }

    System.out.println(number);

}
Enter fullscreen mode Exit fullscreen mode

Output

1
3
5
7
9
Enter fullscreen mode Exit fullscreen mode

Whenever an even number is encountered, the remaining statements of that iteration are skipped.


Execution Flow

for (int number = 1; number <= 5; number++) {

    if (number == 3) {
        continue;
    }

    System.out.println(number);

}
Enter fullscreen mode Exit fullscreen mode
Iteration Condition Output
1 false 1
2 false 2
3 true skipped
4 false 4
5 false 5

break vs continue

Although they look similar, they behave very differently.

Statement Effect
break Terminates the loop completely
continue Skips only the current iteration

Example

for (int number = 1; number <= 5; number++) {

    if (number == 3) {
        break;
    }

    System.out.println(number);

}
Enter fullscreen mode Exit fullscreen mode

Output

1
2
Enter fullscreen mode Exit fullscreen mode

Now replace break with continue.

for (int number = 1; number <= 5; number++) {

    if (number == 3) {
        continue;
    }

    System.out.println(number);

}
Enter fullscreen mode Exit fullscreen mode

Output

1
2
4
5
Enter fullscreen mode Exit fullscreen mode

Can continue Be Used Inside a switch?

No.

continue is allowed only inside loops.

Example

switch (1) {

    case 1:
        continue;

}
Enter fullscreen mode Exit fullscreen mode

Compile-time error

continue outside of loop
Enter fullscreen mode Exit fullscreen mode

If a switch is inside a loop, then continue refers to the loop—not the switch.


Common Beginner Mistakes

Using break Outside a Loop

Incorrect

if (true) {

    break;

}
Enter fullscreen mode Exit fullscreen mode

Compile-time error.


Confusing break and continue

Many beginners think continue exits the loop.

It doesn't.

It skips only the current iteration.


Forgetting break Inside a switch

Without break, Java performs fall-through.

This often causes unexpected output.


Interview Questions

Can break be used inside an if statement?

Only if the if statement is inside a loop or a switch.

Otherwise, it causes a compile-time error.


Can continue terminate a loop?

No.

It only skips the current iteration.


Can continue be used inside a switch?

No.

Unless the switch is inside a loop, continue causes a compile-time error.


Which statement exits the loop immediately?

break.


Which statement skips only one iteration?

continue.


Best Practices

  • Use break when no further iterations are needed.
  • Use continue only when skipping specific iterations improves readability.
  • Avoid excessive use of break and continue, as they can make code harder to follow.
  • Keep loop logic simple and readable.
  • Prefer meaningful variable names like student, order, or employee instead of i whenever possible.

Quick Memory Trick 🧠

Remember BC:

  • BBreak the loop completely.
  • CContinue to the next iteration.

Or simply remember:

Break ends the loop. Continue skips one iteration.


Key Takeaways

  • Transfer statements change the normal flow of execution.
  • break immediately exits a loop or switch.
  • continue skips the current iteration and moves to the next one.
  • break is allowed inside loops, switch, and labeled blocks.
  • continue is allowed only inside loops.
  • Forgetting break inside a switch results in fall-through.
  • Understanding the difference between break and continue is a common Java interview topic.

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (0)