MATLAB break & continue Statements

MATLAB Tutorial

⛔🔁 MATLAB break & continue Statements

In MATLAB break & continue Statements are loop control 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

1
2
3
4

📌 Loop stops when i becomes 5.


🔸 Example: break in while Loop


 

Output

1
2
3
4
5

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

1
2
4
5

📌 When i = 3, display is skipped.


🔸 Example: continue in while Loop


 

Output

1
3
4
5

3️⃣ Using break & continue with Conditions


 

Output

1
3
5
7

⚠️ Important Notes

  • Can be used only inside loops

  • break exits the nearest loop

  • continue skips 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

You may also like...