Swift break and continue Statements

Swift Tutorial

⛔➡️ Swift break and continue Statements – Complete Beginner to Interview Guide

In Swift break and continue Statements are loop control statements used to change the normal flow of loops.


1️⃣ What is break in Swift? ⭐

  • Immediately stops the loop

  • Control moves outside the loop


2️⃣ break Example (for loop) ⭐


Output

1
2

📌 Loop stops when i == 3.


3️⃣ break Example (while loop) ⭐⭐


 

Output

1
2
3

4️⃣ What is continue in Swift? ⭐

  • Skips current iteration

  • Moves to next loop iteration


5️⃣ continue Example (for loop) ⭐


Output

1
2
4
5

📌 3 is skipped.


6️⃣ continue Example (while loop) ⭐⭐


 

Output

1
3
4
5

7️⃣ break and continue in Nested Loops ⭐⭐⭐

break (inner loop only)


Output

1 1
2 1
3 1

continue (inner loop only)


Output

1 1
1 3
2 1
2 3
3 1
3 3

8️⃣ Labeled break and continue ⭐⭐⭐ (Advanced)

Used to control outer loops.

Labeled break


Output

1 1

Labeled continue


Output

1 1
2 1
3 1

9️⃣ ❌ break & continue with forEach ⚠️

numbers.forEach {
if $0 == 3 {
break // ❌ Not allowed
}
}

📌 break and continue DO NOT work in forEach.


🔟 Common Mistakes ❌

❌ Using break instead of continue
❌ Forgetting loop update in while
❌ Expecting break to exit all loops
❌ Using inside forEach


📌 Interview Questions (Swift Loop Control)

Q1. Difference between break and continue?
👉 break exits loop, continue skips iteration

Q2. How to break outer loop?
👉 Use labeled break

Q3. Can we use break in forEach?
👉 No


✅ Summary

break exits loop
continue skips iteration
✔ Works in for, while, repeat-while
✔ Use labels for nested loops
✔ Important for Swift logic & interviews

You may also like...