Last Updated: January 3, 2026
When you're deep in coding, you might find yourself in a situation where you want to control the flow of your loops more precisely.
That’s where the break and continue statements come into play. They help you manage what happens during each iteration of a loop, allowing you to exit loops early or skip certain iterations.
Understanding these statements can turn your loops from simple to powerful, enabling you to write cleaner and more efficient code.
breakThe break statement is a way to exit a loop prematurely. It can be used within any loop structure: for, while, or do-while. When break is executed, control immediately jumps to the statement following the loop. This is particularly useful when you want to stop processing once you've found what you're looking for, or when a certain condition is met.
Let’s start with a simple example of using break in a for loop:
In this code, the output will be:
As soon as i reaches 5, the loop terminates. This can be very effective to avoid unnecessary iterations, especially in large data sets where you only need to find a specific value.
Consider a scenario where you’re searching for a user's data in a database represented as a list. Once you find the user, you want to stop searching:
In this example, once "Charlie" is found, we exit the loop early, saving time and resources.
Be cautious with break statements, especially in nested loops. If you have multiple levels of loops, break will only exit the innermost loop. Here’s a quick example:
This outputs:
In this case, the outer loop continues running even after the inner loop breaks. If you want to exit both loops, that’s where labeled statements come into play, which we will discuss later.
continueOn the other hand, the continue statement is used to skip the current iteration of the loop and proceed to the next one. This can be useful when you want to avoid executing certain parts of your loop based on specific conditions.
Let’s see a simple example using continue in a while loop:
Here, the output will be:
Notice how when i is equal to 5, the program skips the System.out.println(i), effectively removing 5 from the output.
Imagine you are processing a list of transactions, and you want to skip any transaction that has already been flagged for review:
This example shows how continue allows you to focus on only the transactions you want to process, improving the efficiency of your code.
Just like with break, be mindful of how continue behaves in nested loops. It only affects the innermost loop:
This will output:
Again, the outer loop continues normally, while the inner loop skips printing when j equals 1.
break and continueSometimes, you might find yourself needing both break and continue in the same loop. Here’s a scenario where you want to process a list but stop if a certain value is found while skipping others:
The output:
In this case, we both skip the even numbers and break the loop when we reach 5. It’s a good demonstration of how these statements can work together to give you finer control over your logic.
While using break and continue, there are a few common pitfalls to keep in mind:
break or continue can lead to infinite loops. Always double-check that your loop’s exit conditions are being met.break and continue: While they can simplify logic, overusing these statements can lead to code that’s hard to read and maintain. Use them judiciously to keep your code clear.break or continue aren't met. For example, if you skip all iterations or exit the loop without handling the remaining cases.In this chapter, we dove deep into how to effectively use break and continue to manage loop execution in Java. We explored the mechanics of each statement, practical use cases, and edge cases to watch out for.
These tools can significantly enhance the efficiency and readability of your code when used correctly.
In the next chapter, we'll look at how labeled statements can give you even more control over nested loops, allowing for clearer and more efficient code management.