When writing loops and switch statements, there are situations where you don't want the normal flow of execution.
Sometimes you want to:
- Exit a loop immediately.
- Skip the current iteration.
- Prevent fall-through in a
switch.
Java provides transfer statements to transfer program control from one location to another.
In this article, we'll learn the two most commonly used transfer statements:
breakcontinue
We'll cover where they can be used, how they work, common mistakes, and interview questions.
What Are Transfer Statements?
Transfer statements change the normal execution flow of a program.
Instead of executing statements sequentially, they transfer control to another part of the program.
Java provides four forms:
breakcontinue- Labeled
break - Labeled
continue
In this article, we'll focus on the first two.
The break Statement
The break statement immediately terminates the nearest enclosing structure.
Depending on where it's used, it can:
- Exit a
switch - Exit a loop
- Exit a labeled block (covered in Part 2)
Where Can break Be Used?
break is allowed only in these places:
| Location | Purpose |
|---|---|
switch statement |
Stop fall-through |
Loops (for, while, do-while) |
Exit the loop immediately |
| Labeled blocks | Exit the labeled block (covered later) |
Using break anywhere else results in a compile-time error.
Using break Inside a switch
Without break, execution continues into the next case.
This behavior is called fall-through.
Example
int option = 1;
switch (option) {
case 1:
System.out.println("Java");
break;
case 2:
System.out.println("Spring Boot");
}
Output
Java
Because of break, execution leaves the switch after printing "Java".
What Happens Without break?
int option = 1;
switch (option) {
case 1:
System.out.println("Java");
case 2:
System.out.println("Spring Boot");
}
Output
Java
Spring Boot
Execution "falls through" into the next case.
Using break Inside a Loop
The break statement immediately terminates the loop.
Example
for (int number = 1; number <= 10; number++) {
if (number == 6) {
break;
}
System.out.println(number);
}
Output
1
2
3
4
5
As soon as number becomes 6, the loop stops completely.
Execution Flow
for (int number = 1; number <= 5; number++) {
if (number == 3) {
break;
}
System.out.println(number);
}
| Iteration | Condition | Output |
|---|---|---|
| 1 |
1 == 3 → false |
1 |
| 2 |
2 == 3 → false |
2 |
| 3 |
3 == 3 → true |
Loop ends |
Using break Outside a Loop or switch
This is illegal.
int age = 25;
if (age > 18) {
break;
}
Compile-time error
break outside switch or loop
break only works inside:
- loops
switch- labeled blocks
The continue Statement
Unlike break, the continue statement does not terminate the loop.
Instead, it skips the current iteration and starts the next one.
Example
for (int number = 1; number <= 10; number++) {
if (number % 2 == 0) {
continue;
}
System.out.println(number);
}
Output
1
3
5
7
9
Whenever an even number is encountered, the remaining statements of that iteration are skipped.
Execution Flow
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
}
System.out.println(number);
}
| Iteration | Condition | Output |
|---|---|---|
| 1 | false | 1 |
| 2 | false | 2 |
| 3 | true | skipped |
| 4 | false | 4 |
| 5 | false | 5 |
break vs continue
Although they look similar, they behave very differently.
| Statement | Effect |
|---|---|
break |
Terminates the loop completely |
continue |
Skips only the current iteration |
Example
for (int number = 1; number <= 5; number++) {
if (number == 3) {
break;
}
System.out.println(number);
}
Output
1
2
Now replace break with continue.
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
}
System.out.println(number);
}
Output
1
2
4
5
Can continue Be Used Inside a switch?
No.
continue is allowed only inside loops.
Example
switch (1) {
case 1:
continue;
}
Compile-time error
continue outside of loop
If a switch is inside a loop, then continue refers to the loop—not the switch.
Common Beginner Mistakes
Using break Outside a Loop
Incorrect
if (true) {
break;
}
Compile-time error.
Confusing break and continue
Many beginners think continue exits the loop.
It doesn't.
It skips only the current iteration.
Forgetting break Inside a switch
Without break, Java performs fall-through.
This often causes unexpected output.
Interview Questions
Can break be used inside an if statement?
Only if the if statement is inside a loop or a switch.
Otherwise, it causes a compile-time error.
Can continue terminate a loop?
No.
It only skips the current iteration.
Can continue be used inside a switch?
No.
Unless the switch is inside a loop, continue causes a compile-time error.
Which statement exits the loop immediately?
break.
Which statement skips only one iteration?
continue.
Best Practices
- Use
breakwhen no further iterations are needed. - Use
continueonly when skipping specific iterations improves readability. - Avoid excessive use of
breakandcontinue, as they can make code harder to follow. - Keep loop logic simple and readable.
- Prefer meaningful variable names like
student,order, oremployeeinstead ofiwhenever possible.
Quick Memory Trick 🧠
Remember BC:
- B → Break the loop completely.
- C → Continue to the next iteration.
Or simply remember:
Break ends the loop. Continue skips one iteration.
Key Takeaways
- Transfer statements change the normal flow of execution.
-
breakimmediately exits a loop orswitch. -
continueskips the current iteration and moves to the next one. -
breakis allowed inside loops,switch, and labeled blocks. -
continueis allowed only inside loops. - Forgetting
breakinside aswitchresults in fall-through. - Understanding the difference between
breakandcontinueis a common Java interview topic.
If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.
Happy Coding!
Top comments (0)