C++ Break and Continue
⛔➡️ C++ break and continue
In C++, break and continue are loop control statements.
They change the normal flow of loops (for, while, do-while).
🔴 break Statement
break is used to terminate the loop completely when a condition is met.
1. break in a for Loop
Output:
➡ Loop stops when i == 3.
2. break in a while Loop
Output:
3. break in a switch Statement
➡ Prevents fall-through.
🟡 continue Statement
continue is used to skip the current iteration and move to the next iteration of the loop.
4. continue in a for Loop
Output:
➡ 3 is skipped.
5. continue in a while Loop
Output:
🔁 break vs continue
| Feature | break | continue |
|---|---|---|
| Effect | Exits the loop | Skips current iteration |
| Loop continues? | ❌ No | ✔ Yes |
| Used in | Loops & switch | Loops only |
🔹 6. break and continue in Nested Loops
➡ break stops only the inner loop.
⚠️ Common Mistakes
✅ Best Practices
Use
breakto stop loops early (search, validation)Use
continueto skip unwanted casesAvoid overusing them (can reduce readability)
📌 Summary
breakexits the loop completelycontinueskips the current iterationBoth improve control over loops
breakalso used inswitch
