C# Break and Continue
C# Break and Continue
In C#, break and continue are loop control statements used to change the normal flow of loops (for, while, do-while, foreach, and switch).
break Statement
The break statement is used to exit a loop or switch statement immediately.
✔ break in a for Loop
Output:
✔ break in a while Loop
✔ break in switch
continue Statement
The continue statement is used to skip the current iteration and move to the next loop iteration.
✔ continue in a for Loop
Output:
✔ continue in a while Loop
break vs continue
| Feature | break | continue |
|---|---|---|
| Stops loop | ✅ Yes | ❌ No |
| Skips iteration | ❌ No | ✅ Yes |
| Used in loops | ✅ Yes | ✅ Yes |
| Used in switch | ✅ Yes | ❌ No |
Common Mistakes
❌ Forgetting loop update before continue (causes infinite loop)
❌ Overusing break
❌ Using continue unnecessarily
Best Practices
✔ Use break to exit early when condition is met
✔ Use continue to skip unwanted iterations
✔ Keep logic readable
Summary
✔ break exits the loop or switch
✔ continue skips current iteration
✔ Works with all loops
✔ Improves control flow
