Day 11: Understanding `break` and `continue` Statements in Java
Source: Dev.to
What are break and continue?
In Java, break and continue are loop‑control statements that alter the normal flow of loops (for, while, do‑while) and switch blocks.
break– stops the loop (orswitch) completely.continue– skips the current iteration and proceeds to the next one.
break Statement
What is break?
The break statement terminates a loop or a switch block immediately when a specific condition is met. After break executes, control moves to the statement that follows the loop or switch.
Syntax of break
break;
break in a loop
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
Output
1
2
3
4
The loop stops when i becomes 5; numbers after 4 are not printed.
break in a switch statement
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}
break prevents execution from falling through to the next case. Without it, multiple cases would execute.
continue Statement
What is continue?
The continue statement skips the remainder of the current loop iteration and proceeds directly to the next iteration. The loop itself does not stop.
Syntax of continue
continue;
continue in a loop
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output
1
2
4
5
When i equals 3, the continue statement skips printing 3 and moves to the next iteration.
break vs continue
| Feature | break | continue |
|---|---|---|
| Stops loop | Yes | No |
| Skips iteration | No | Yes |
| Control moves to | After loop | Next iteration |
Usable in switch | Yes | No |
breakexits the loop (orswitch) completely.continueskips only the current iteration.
Both improve loop control and readability and are typically used with conditional statements (if).
When to use break
- A required value is found during a search.
- Input validation fails and the loop should stop.
- The loop needs to terminate early for any logical reason.
When to use continue
- Certain values should be ignored (e.g., filtering out even numbers).
- Skipping invalid or unwanted data within a loop.
- When only part of the iteration should be bypassed while the loop continues.
break and continue together
for (int i = 1; i <= 10; i++) {
if (i == 3) {
continue; // skip printing 3
}
if (i == 8) {
break; // stop the loop when i reaches 8
}
System.out.println(i);
}
Output
1
2
4
5
6
7
continue skips the number 3, and break stops the loop before printing 8.
Common pitfalls
- Forgetting
breakinswitchcases, causing fall‑through. - Using
continuewithout understanding how it affects loop flow, leading to unexpected results. - Accidentally creating infinite loops by misplacing
break/continue. - Confusing the behaviors of
breakandcontinue.
Summary
Use break and continue when you need finer control over loop execution:
break– for early exit from a loop orswitch.continue– for skipping specific iterations while keeping the loop running.
Applying these statements thoughtfully leads to cleaner, more efficient Java code.