Introduction

The break and continue statements are used to control the flow of a loop in Java.

  • break → completely stops the loop.
  • continue → skips the current iteration and moves to the next iteration.

Break Statement

The break statement immediately terminates the loop.

Example


Output
1
2
3
4

When i = 5, the break statement executes and the loop stops completely.


Continue Statement

The continue statement skips the current iteration and continues with the next iteration.

Example


Output
12
4
5



When i = 3, continue skips that iteration, so 3 is not printed.

Break vs Continue

breakcontinue
Stops the loop completelySkips the current iteration
Control moves outside the loopControl moves to the next iteration
Used when we want to terminate the loopUsed when we want to skip a particular case

Key Takeaways

  • break completely terminates a loop.
  • continue skips the current iteration.
  • Use break when you no longer need the loop to continue.
  • Use continue when you want to skip a particular iteration.