What does the break statement do inside a loop in Swift?
The break statement immediately stops the entire loop and exits it, continuing execution after the loop.
Click to reveal answer
beginner
What is the purpose of the continue statement in Swift loops?
The continue statement skips the rest of the current loop iteration and moves to the next iteration of the loop.
Click to reveal answer
intermediate
In Swift, what happens if you use break inside a nested loop?
The break statement only exits the innermost loop where it is called, not all outer loops.
Click to reveal answer
intermediate
Can continue be used in a switch statement in Swift?
No, <code>continue</code> is used in loops, not in <code>switch</code> statements. To skip cases in <code>switch</code>, you use <code>fallthrough</code> or just let the case end.
Click to reveal answer
beginner
How does break differ from continue in Swift loops?
break stops the whole loop immediately, while continue skips only the current iteration and continues with the next one.
Click to reveal answer
What will happen if a break statement is executed inside a Swift for loop?
AThe loop restarts from the beginning.
BThe current iteration is skipped, and the loop continues.
CThe loop stops immediately and exits.
DNothing, the loop continues normally.
✗ Incorrect
The break statement immediately stops the loop and exits it.
What does the continue statement do in a Swift loop?
AStops the entire loop.
BSkips the rest of the current iteration and moves to the next iteration.
CExits the function.
DRestarts the loop from the first iteration.
✗ Incorrect
continue skips the rest of the current iteration and proceeds with the next one.
If break is used inside a nested loop, which loop does it exit?
AOnly the innermost loop where <code>break</code> is called.
BAll loops at once.
CThe outermost loop.
DIt causes an error.
✗ Incorrect
break exits only the innermost loop it is inside.
Can continue be used to skip a case in a Swift switch statement?
AYes, it skips the case.
BNo, you must use <code>break</code> instead.
CYes, but only in <code>switch</code> with loops.
DNo, <code>continue</code> is only for loops.
✗ Incorrect
continue is used only in loops, not in switch statements.
Which statement best describes the difference between break and continue in Swift loops?
A<code>break</code> stops the loop; <code>continue</code> skips current iteration.
B<code>break</code> skips current iteration; <code>continue</code> stops the loop.
CBoth stop the loop immediately.
DBoth skip the current iteration.
✗ Incorrect
break stops the loop; continue skips the current iteration and continues.
Explain how break and continue behave inside Swift loops and how they differ.
Think about what happens to the loop after each statement runs.
You got /4 concepts.
Describe what happens when break is used inside nested loops in Swift.
Consider which loop the break affects.
You got /3 concepts.
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
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.
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.
Final Answer:
Stops the entire loop immediately and exits it. -> Option D
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
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.
Step 2: Verify other options
break stops the loop, skip and stop are not valid Swift keywords for loops.
Final Answer:
continue -> Option B
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
Step 1: Analyze the loop and break condition
The loop runs from 1 to 5. When i equals 3, break stops the loop immediately.
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.
Final Answer:
1 2 -> Option A
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
Step 1: Check the loop range and conditions
The loop runs from 1 to 5, so i will never be 6.
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.
Final Answer:
The loop will never break because i never reaches 6. -> Option C
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
Step 1: Understand requirements
Skip printing 5 (use continue) and stop printing after 8 (use break when > 8).
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.
Final Answer:
for i in 1...10 {
if i == 5 { continue }
if i > 8 { break }
print(i)
} -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.