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:
Example 2: Break in a while loop
📌 Output:
✅ 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:
Example 2: Skip even numbers
📌 Output:
✅ 3. Break vs Continue
| Feature | Break | Continue |
|---|---|---|
| Stops loop | Yes, completely | No, only current iteration |
| Moves to next iteration | ❌ | ✅ |
| Common Use | Exit on condition | Skip unwanted iterations |
🔹 4. Nested Loops with Break
📌 Output:
🔹 5. Nested Loops with Continue
📌 Output:
🔹 Summary
| Statement | Effect | Usage |
|---|---|---|
break |
Stops the loop completely | Exit on a specific condition |
continue |
Skips current iteration | Skip unwanted elements |
