Difference Between break and continue in Java
0 118
Introduction to break and continue in Java
In Java programming, controlling the flow of loops is crucial for writing efficient code. Two important statements used for this purpose are break and continue. Although both affect loop execution, they serve distinct roles in managing how loops behave.
What is the break Statement?
The break
statement in Java is used to immediately exit the entire loop or switch block. Once break
is executed, the control jumps out of the loop, skipping any remaining iterations.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // loop exits when i equals 3
}
System.out.println("i: " + i);
}
Output:
i: 1
i: 2
What is the continue Statement?
The continue
statement is used to skip the current iteration of a loop and jump directly to the next iteration, without exiting the loop entirely.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // skips the iteration when i equals 3
}
System.out.println("i: " + i);
}
Output:
i: 1
i: 2
i: 4
i: 5
Key Differences Between break and continue
Aspect | break | continue |
Functionality | Exits the entire loop or switch immediately | Skips current iteration and proceeds to next iteration |
Effect on Loop | Terminates loop execution | Loop continues with the next cycle |
Common Use | Stop looping when a condition is met | Ignore certain values or cases but keep looping |
Use in switch | Yes, used to exit switch cases | No, not applicable in switch statements |
Practical Example: Combining break and continue
Here’s a loop that prints numbers from 1 to 10 but skips multiples of 3 and stops entirely if the number reaches 8:
for (int i = 1; i <= 10; i++) {
if (i == 8) {
break; // exit loop completely when i is 8
}
if (i % 3 == 0) {
continue; // skip multiples of 3
}
System.out.println("Number: " + i);
}
Output:
Number: 1
Number: 2
Number: 4
Number: 5
Number: 7
Conclusion
Both break and continue statements are essential for managing loops in Java, but they serve different purposes. break
immediately ends the loop, while continue
skips only the current iteration and moves on. Understanding their differences helps you write cleaner and more efficient loop logic.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments