Chapter 62: Swift Break/Continue

1. What are break and continue?

Both break and continue are control transfer statements — they change the normal flow of a loop.

Keyword What it does in a loop Analogy (very clear) Effect on outer loops (when nested)
break Immediately exits the nearest loop (stops completely) “I’m done here — I’m leaving the room right now” Only exits the innermost loop
continue Immediately skips to the next iteration (jumps to the next loop cycle) “Skip this person — go to the next one in line” Only affects the innermost loop

Both only work inside loops (for, while, repeat … while) and inside switch statements (but today we focus on loops).

2. break – exit the loop early

Most common use case: You are looking for one thing — as soon as you find it → stop searching.

Swift

Real-life analogy:

Imagine you are searching for your car keys in a messy room. As soon as you find them → you stop searching (you break out of the loop).

Real app example – find first error in list

Swift

3. continue – skip the rest of this iteration

Most common use case: You want to ignore certain items and continue with the next ones.

Swift

Output:

text

Real-life analogy:

You are checking a list of job applicants. When you see someone without experience → you skip them (continue) and look at the next candidate.

Real app example – skip inactive users

Swift

4. Combining break & continue – very realistic patterns

Pattern 1 – Find first valid item (break early)

Swift

Pattern 2 – Process only valid items (continue skips)

Swift

5. Nested loops with break & continue (very important)

When you have nested loops, break and continue affect only the innermost loop.

Swift

Output (simplified):

text

How to break out of outer loop? Use labeled break (rare but very useful):

Swift

6. Very Common Beginner Mistakes & Fixes

Mistake Wrong / Dangerous code Correct / Better habit Why?
Forgetting to update loop variable while count > 0 { print(count) } count -= 1 inside loop Infinite loop
Using break / continue outside loop break outside any loop Only inside for, while, repeat, switch Compile error
Expecting break to exit outer loop break inside inner loop Use break outerLabel: break only exits innermost loop
Using continue in forEach array.forEach { if … continue } Use for-in instead forEach does not support continue
No safety limit in while true while true { … } without break Add counter + if attempt > max { break } Prevents infinite loop

7. Quick Summary – break vs continue

Keyword Effect inside loop Typical real-life sentence Can exit outer loop?
break Exit the loop completely — go after the loop “Found it — stop searching now” Yes (with label)
continue Skip the rest of this iteration — go to next one “This item is invalid — skip to the next one” No

8. Small Practice – Try these

  1. Create array of 10 numbers → Use for + continue to print only even numbers → Use break to stop after finding the first number > 50
  2. Create array of 8 words → Print only words longer than 4 letters → Stop completely if you find the word “stop”
  3. Create 2D array (3×4) of numbers → Use nested for → continue if number is negative → break inner loop if you find number > 100

Paste your code here if you want feedback or want to see more elegant versions!

What would you like to explore next?

  • break & continue with labeled loops (nested exit)
  • forEach vs for-in vs map vs filter
  • Loops in SwiftUI (ForEach vs manual while)
  • while let pattern (very common with optionals)
  • Or move to another topic (switch, functions, arrays, optionals…)

Just tell me — we’ll continue in the same clear, patient, detailed style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *