Java Tutorial Series: This lesson is part of our comprehensive Complete Java Tutorial: Learn Java from Beginner to Advanced.
Break and Continue in Java: Control Loop Execution with Examples
What Are break and continue?
break and continue alter normal loop execution. break stops the nearest applicable loop, while continue skips the remainder of the current iteration and proceeds toward the next iteration.
The break Statement
The loop stops as soon as i reaches 6.
The continue Statement
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
The value 3 is skipped while later iterations continue.
break with while
int count = 1;
while (count <= 10) {
if (count == 5) {
break;
}
count++;
}
break is useful when a search or process should stop as soon as a condition is satisfied.
continue with while
When continue is used in a while loop, make sure the variables needed to reach termination are updated. Otherwise, the skipped update can create an infinite loop.
break in switch
Traditional switch statements also use break to stop fall-through. The meaning is tied to the control-flow construct in which break appears.
Nested Loops
An ordinary break exits the nearest enclosing loop. In nested loops, breaking the inner loop does not automatically stop the outer loop.
Labeled break and continue
Java supports labels for controlling an outer loop from nested loops. Labels can solve specific problems, but overusing them can make control flow harder to understand.
Search Example
int[] numbers = {4, 8, 12, 20, 25};
int target = 12;
for (int number : numbers) {
if (number == target) {
System.out.println("Found");
break;
}
}
Stopping after a match avoids unnecessary work.
Filtering Example
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue;
}
System.out.println(i);
}
continue is useful when certain records or values should be skipped.
Common Mistakes
Do not confuse break with continue. In nested loops, verify which loop is affected. With while loops, make sure continue does not skip the update that controls termination.
When Not to Use Them
If a clear loop condition can express the same logic, it may be easier to maintain than scattered break or continue statements. These tools should improve clarity rather than hide the algorithm.
Best Practices
Use early exit for clear search conditions, use continue for explicit filtering, keep loop flow easy to trace and test both the exit path and normal path.
Practice Problems
Search for a target and stop, print only matching values, skip invalid records, stop processing after a threshold and find the first value meeting a condition.
Frequently Asked Questions
What does break do? It exits the nearest applicable loop or switch.
What does continue do? It skips the current iteration and proceeds toward the next.
Does break stop all nested loops? No, an unlabeled break stops the nearest loop.
Conclusion
break and continue are practical tools for early termination and filtering. Use them deliberately so that the loop remains easy to understand and maintain.
Learn More
- Java Syntax
- Java Variables
- Java Data Types
- Java Operators
- Java Input and Output
- Java Methods
- Java Arrays and Collections
Continue Learning Java
After this topic, continue through the Java control-flow path by practicing the next related statement or loop. Build small programs rather than only reading examples.
Deeper Practical Coverage
Exact Execution of break
When Java encounters break inside a loop, it immediately exits the nearest enclosing loop. Statements remaining in that loop body are skipped, and execution continues after the loop.
Exact Execution of continue
continue skips the remainder of the current iteration. In a classic for loop, Java then performs the update expression before checking the loop condition again. In a while loop, control returns to the condition check.
Search Optimization
break is especially useful for linear searches. Once the target is found, continuing to inspect unrelated elements provides no benefit.
for (int value : values) {
if (value == target) {
found = true;
break;
}
}
This improves practical work by avoiding unnecessary iterations.
Filtering with continue
continue can keep the main operation focused on valid values.
for (int value : values) {
if (value < 0) {
continue;
}
process(value);
}
This is often clearer than wrapping the entire processing block inside if.
Nested Loop Control
In nested loops, an unlabeled break exits only the nearest loop. If the algorithm must stop the outer search as well, a flag, method return, or carefully used labeled break may be appropriate.
Control Flow and Readability
Many breaks and continues can make a loop difficult to reason about. If control jumps are everywhere, consider extracting logic into methods or restructuring the algorithm. Use branching statements to clarify the algorithm, not to hide it.
Interview Perspective
Be able to explain break versus continue, identify which loop a break affects, trace a loop containing continue, and explain how break interacts with traditional switch statements.
Extended Practice Set
- Search with break.
- Filter with continue.
- Use break inside nested loops.
- Explain labeled break with an example.
- Refactor excessive break/continue usage.
Java Language and Version Note
The Java platform documentation groups control flow into decision-making, looping, and branching constructs. Modern Java has also expanded switch with expression forms, arrow rules, and pattern-oriented capabilities. When publishing this article, the website should state the Java version when an example depends on a newer language feature. Core syntax such as if, if-else, for, while, do-while, break, continue, and traditional switch remains foundational, while newer forms should be presented as version-aware additions.