Bird
Raised Fist0
Swiftprogramming~10 mins

Break and continue behavior in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Break and continue behavior
Start Loop
↓
Check Condition
Yes↓
Inside Loop Body
↓
If 'continue' condition?
Yes→Skip rest, next iteration
↓
If 'break' condition?
Yes→Exit Loop
↓
Execute remaining loop code
↓
Next iteration
Back to Check Condition↓
End Loop
↩Back to Check Condition
The loop checks a condition each time. If 'continue' is met, it skips to the next iteration. If 'break' is met, it exits the loop immediately.
Execution Sample
Swift
for i in 1...5 {
    if i == 3 {
        continue
    }
    if i == 4 {
        break
    }
    print(i)
}
This loop prints numbers 1 to 5 but skips 3 and stops before printing 4.
Execution Table
IterationiCheck i==3ActionCheck i==4ActionPrint Output
11FalseNo continueFalseNo breakPrint 1
22FalseNo continueFalseNo breakPrint 2
33TrueContinue - skip print---
44FalseNo continueTrueBreak - exit loop-
------Loop ends
💡 Loop stops at i=4 because break condition is met.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
i-1234-
Key Moments - 3 Insights
Why does the loop skip printing when i equals 3?
Because at iteration 3, the condition i == 3 is true, so 'continue' runs, skipping the print statement as shown in execution_table row 3.
Why does the loop stop before printing 4?
At iteration 4, the condition i == 4 is true, so 'break' runs, exiting the loop immediately as shown in execution_table row 4.
Does 'continue' stop the entire loop?
No, 'continue' only skips the rest of the current iteration and moves to the next one, as seen in iteration 3 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when i equals 3?
ANothing
B3
C4
DError
💡 Hint
Check row 3 in the execution_table where 'continue' causes skipping print.
At which iteration does the loop stop executing?
AAfter i=2
BAt i=4
CAfter i=3
DAt i=5
💡 Hint
Look at the 'break' action in execution_table row 4.
If we remove the 'continue' statement, what will be printed?
A1, 2
B1, 2, 3, 4
C1, 2, 3
D1, 2, 4
💡 Hint
Without 'continue', i=3 will print, but loop breaks at i=4 before printing it.
Concept Snapshot
Swift 'break' exits the loop immediately.
Swift 'continue' skips the rest of current iteration.
Use 'break' to stop looping early.
Use 'continue' to skip specific steps.
Both affect loop flow control.
Full Transcript
This example shows a Swift for-loop from 1 to 5. When i equals 3, the 'continue' statement skips printing 3 and moves to the next number. When i equals 4, the 'break' statement stops the loop entirely, so 4 and 5 are not printed. The execution table tracks each iteration's checks and actions, showing how 'continue' skips printing and 'break' ends the loop. Variables are tracked to show i's value each step. Key moments clarify why skipping and stopping happen. The quiz tests understanding of these behaviors.

Practice

(1/5)
1. What does the break statement do inside a Swift loop?
easy
A. Restarts the loop from the beginning.
B. Skips the current iteration and continues with the next one.
C. Pauses the loop until a condition is met.
D. Stops the entire loop immediately and exits it.

Solution

  1. Step 1: Understand the role of break in loops

    The break statement immediately stops the loop and exits it, no matter where it is in the loop.
  2. Step 2: Compare with other options

    continue skips to the next iteration, but break stops the loop entirely. Restarting or pausing the loop is not the behavior of break.
  3. Final Answer:

    Stops the entire loop immediately and exits it. -> Option D
  4. Quick Check:

    break stops loop [OK]
Hint: Break means stop the loop completely [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break restarts the loop
  • Assuming break pauses the loop
2. Which of the following is the correct syntax to skip the current iteration in a Swift for loop?
easy
A. skip
B. continue
C. break
D. stop

Solution

  1. Step 1: Identify the keyword to skip iteration

    In Swift, continue is used to skip the rest of the current loop iteration and move to the next one.
  2. Step 2: Verify other options

    break stops the loop, skip and stop are not valid Swift keywords for loops.
  3. Final Answer:

    continue -> Option B
  4. Quick Check:

    Skip iteration = continue [OK]
Hint: Use continue to skip current loop step [OK]
Common Mistakes:
  • Using break instead of continue
  • Using non-existent keywords like skip or stop
  • Confusing continue with break
3. What will be the output of this Swift code?
for i in 1...5 {
    if i == 3 {
        break
    }
    print(i)
}
medium
A. 1 2
B. No output
C. 3 4 5
D. 1 2 3 4 5

Solution

  1. Step 1: Analyze the loop and break condition

    The loop runs from 1 to 5. When i equals 3, break stops the loop immediately.
  2. Step 2: Determine printed values before break

    Values 1 and 2 are printed before reaching 3. At 3, loop stops, so 3, 4, 5 are not printed.
  3. Final Answer:

    1 2 -> Option A
  4. Quick Check:

    Break stops at 3, prints before it [OK]
Hint: Break stops loop before printing 3 [OK]
Common Mistakes:
  • Assuming break skips only current iteration
  • Thinking all numbers print
  • Confusing break with continue
4. Find the error in this Swift code snippet:
for i in 1...5 {
    if i == 3 {
        continue
    }
    if i == 6 {
        break
    }
    print(i)
}
medium
A. break should come before continue in the code.
B. continue cannot be used inside a for loop.
C. The loop will never break because i never reaches 6.
D. The range 1...5 is invalid.

Solution

  1. Step 1: Check the loop range and conditions

    The loop runs from 1 to 5, so i will never be 6.
  2. Step 2: Analyze the break condition

    Since i == 6 never occurs, the break statement will never execute, so the loop runs fully except skipping 3 due to continue.
  3. Final Answer:

    The loop will never break because i never reaches 6. -> Option C
  4. Quick Check:

    Break condition unreachable [OK]
Hint: Check if break condition can ever be true [OK]
Common Mistakes:
  • Thinking continue is invalid in for loops
  • Assuming break runs before continue always
  • Not checking loop range carefully
5. You want to print all numbers from 1 to 10 except 5 and stop printing after 8. Which code snippet correctly uses break and continue in Swift?
hard
A. for i in 1...10 { if i == 5 { continue } if i > 8 { break } print(i) }
B. for i in 1...10 { if i == 5 { break } if i > 8 { continue } print(i) }
C. for i in 1...10 { if i == 5 { continue } if i > 7 { continue } print(i) }
D. for i in 1...10 { if i == 5 { break } if i > 8 { break } print(i) }

Solution

  1. Step 1: Understand requirements

    Skip printing 5 (use continue) and stop printing after 8 (use break when > 8).
  2. Step 2: Check each option

    for i in 1...10 { if i == 5 { continue } if i > 8 { break } print(i) } uses continue to skip 5 and break to stop after 8, matching requirements exactly.
  3. Final Answer:

    for i in 1...10 { if i == 5 { continue } if i > 8 { break } print(i) } -> Option A
  4. Quick Check:

    Continue skips 5, break stops loop after 8 [OK]
Hint: Continue skips, break stops loop [OK]
Common Mistakes:
  • Using break to skip 5 instead of continue
  • Using continue to stop loop after 8
  • Mixing order of break and continue incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard