MATLAB break & continue Statements
⛔🔁 MATLAB break & continue Statements
They are used inside for and while loops to control the flow of execution.
MATLAB is developed by MathWorks.
🔹 Difference at a Glance
| Statement | Purpose |
|---|---|
break |
Exits the loop completely |
continue |
Skips current iteration and continues with next |
1️⃣ break Statement
The break statement immediately terminates the loop, even if the condition is still true.
🔸 Example: break in for Loop
Output
📌 Loop stops when i becomes 5.
🔸 Example: break in while Loop
Output
2️⃣ continue Statement
The continue statement skips the remaining code in the current iteration and moves to the next iteration.
🔸 Example: continue in for Loop
Output
📌 When i = 3, display is skipped.
🔸 Example: continue in while Loop
Output
3️⃣ Using break & continue with Conditions
Output
⚠️ Important Notes
-
Can be used only inside loops
-
breakexits the nearest loop -
continueskips current iteration only -
Overuse can reduce code readability
🎯 Interview Questions: break & continue
🔹 Q1. What does break do in MATLAB?
Answer:
Terminates the loop immediately.
🔹 Q2. What does continue do?
Answer:
Skips current iteration and continues with next.
🔹 Q3. Can break be used outside loops?
Answer:
No.
🔹 Q4. Which loop supports break and continue?
Answer:
Both for and while.
🔹 Q5. What happens when continue is executed?
Answer:
Remaining statements in that iteration are skipping.
🔹 Q6. Does break exit all nested loops?
Answer:
No, only the innermost loop.
🔹 Q7. How do you stop an infinite loop?
Answer:
Using break or Ctrl + C.
✅ Summary
-
break→ exit loop -
continue→ skip iteration -
Improve control over loop execution
-
Essential for conditional looping
