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:

1 2

➡ Loop stops when i == 3.


2. break in a while Loop


 

Output:

1 2 3

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:

1 2 4 5

3 is skipped.


5. continue in a while Loop


 

Output:

1 2 4 5

🔁 break vs continue

Featurebreakcontinue
EffectExits the loopSkips current iteration
Loop continues?❌ No✔ Yes
Used inLoops & switchLoops only

🔹 6. break and continue in Nested Loops



 

break stops only the inner loop.


⚠️ Common Mistakes

continue; // ❌ outside a loop
break; // ❌ outside loop/switch

✅ Best Practices

  • Use break to stop loops early (search, validation)

  • Use continue to skip unwanted cases

  • Avoid overusing them (can reduce readability)


📌 Summary

  • break exits the loop completely

  • continue skips the current iteration

  • Both improve control over loops

  • break also used in switch

You may also like...