Java Break and Continue Statements

Java Break and Continue Statements

In Java, break and continue are used to control the flow of loops (for, while, do...while).

  • break → Exits the loop completely.

  • continue → Skips the current iteration and moves to the next iteration.


1. Break Statement

The break statement stops the loop immediately.

Example 1: Break in a for loop



 

📌 Output:

1
2
3
4

Example 2: Break in a while loop


 

📌 Output:

1
2
3
4
5
6

2. Continue Statement

The continue statement skips the current iteration and moves to the next one.

Example 1: Skip a number in for loop



 

📌 Output:

1
2
3
4
6
7
8
9
10

Example 2: Skip even numbers



 

📌 Output:

1
3
5
7
9

3. Break vs Continue

FeatureBreakContinue
Stops loopYes, completelyNo, only current iteration
Moves to next iteration
Common UseExit on conditionSkip unwanted iterations

4. Nested Loops with Break



 

📌 Output:

i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
i=3, j=1
i=3, j=2
i=3, j=3

5. Nested Loops with Continue



 

📌 Output:

i=1, j=1
i=1, j=3
i=2, j=1
i=2, j=3

 Summary

StatementEffectUsage
breakStops the loop completelyExit on a specific condition
continueSkips current iterationSkip unwanted elements

You may also like...