What continue does
The continue statement ends the current iteration of a loop and jumps to the next one. Any code below it in the body is skipped for that iteration, but the loop itself keeps running. Compare break, which ends the whole loop.
It is useful for filtering: test whether an element should be ignored, and if so, skip it.
An example
This loop lets people into a bar only if they are at least 21:
let ages = [10, 30, 21, 19, 25]
for age in ages {
if age < 21 {
continue
}
print("Someone of age \(age) entered the bar.")
}
Someone of age 30 entered the bar.
Someone of age 21 entered the bar.
Someone of age 25 entered the bar.
For ages 10 and 19 the condition is true, continue runs, and the print below is skipped. For 30, 21 and 25 the condition is false, so the body runs to the end. Notice that 21 is allowed in, because the test is age < 21, which is false for 21.
Skipping with guard
Here is the same idea with guard, summing only the non-negative numbers:
let nums = [3, -1, 4, -5, 9]
var total = 0
for n in nums {
guard n >= 0 else { continue }
total += n
}
print(total)
It prints 16, which is 3 + 4 + 9. The two negative numbers hit continue and never reach total += n. Notice that a guard states the condition to keep (n >= 0), while the if form in the bar example states the condition to skip.
Swift details
- Same shape as
break. Both go inside anifinside the loop body. Only their effect differs. - Labels work too. In nested loops,
continueapplies to the innermost loop.continue outer, with a labeled outer loop, skips to the next iteration of that outer loop. - Braces are required. The body of an
ifneeds braces even for a single statement, and the condition needs no parentheses. guardis a common alternative.guard n >= 0 else { continue }skips negative numbers and keeps the rest of the body unindented. Aguardbody must leave the current scope, andcontinuedoes.- Be careful in
whileloops.continuejumps straight back to the condition. If the statement that changes the counter sits below thecontinue, it is skipped and the loop may never end. Put the update before thecontinue.
The exercise
Only people with a height of at least 170 may enter the arena. The code starts with:
let heights = [180, 139, 174, 190, 162]
- Loop over the heights.
for height in heightsvisits each person in turn. - Skip the short ones. Inside the loop,
if height < 170 { continue }. At least 170 means 170 itself passes, so the test is strictly less than. - Print the rest. Below the
if,print(height)runs only for heights that were not skipped.
let heights = [180, 139, 174, 190, 162]
for height in heights {
if height < 170 {
continue
}
print(height)
}
The output is:
180
174
190
Common mistakes
- Using
<=.height <= 170would turn away a person who is exactly 170. - Printing before the check. Every height is printed, because the print happens before
continuecan skip anything. - Confusing it with
break. Withbreak, the first short person (139) would end the loop and only 180 would print.
Key takeaways
continueskips the rest of the current iteration and moves to the next one.- Write the test for what to skip, then the code for what to keep.
- In
whileloops, update the counter before anycontinue.
