Java Break and Continue Statements – Complete Guide with Examples
Learn how to control the flow of loops in Java using break and continue statements with detailed examples for both simple and nested loops.
Break and Continue Statements in Java – Complete Detailed Tutorial
In Java, loop control statements like break and continue allow us to alter the normal flow of loops based on conditions.
These statements are essential for efficient loop control.
1. break Statement
- Terminates the current loop immediately
- Execution jumps outside the loop
- Can be used with for, while, do-while, and switch
Syntax:
Example – break in for loop
Output:
Explanation:
- When
i == 5, the loop terminates immediately
Example – break in while loop
Output:
2. continue Statement
- Skips the current iteration and moves to the next iteration
- Loop continues after skipping
Syntax:
Example – continue in for loop
Output:
Explanation:
- The number 3 is skipped but the loop continues
Example – continue in while loop
Output:
3. Using break and continue in Nested Loops
- break terminates inner-most loop
- continue skips current iteration of inner-most loop
Example – nested for loop
Output:
Example – break in nested loop
Output:
4. Summary
break→ exit loop immediatelycontinue→ skip current iteration and continue- Can be used in for, while, do-while, and switch
- In nested loops,
break/continueaffects inner-most loop only