⛔ break Statement in C
🔍 Short Explanation:
The break statement immediately exits from the nearest enclosing loop or switch block, stopping further execution inside it.
✅ Syntax
break;
📋 Example: Break in a for Loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // exit loop when i is 5
}
printf("%d\\n", i);
}
return 0;
}
🧠 Output: 1 2 3 4
💡 Common Use Cases
- Exit loops early based on condition
- Stop processing when desired result is found
- Used in
switchcases to prevent fall-through
⚠️ Tips & Best Practices
- Use carefully to avoid skipping important code
- Prefer clear logic to maintain readability
- Don’t confuse with
continue, which skips only current iteration
📌 Summary
The break statement is useful to immediately stop a loop or switch, giving you control over the program flow.
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন