Coding for All
Lesson 21: Continue
Continue Skip one trip around a loop with continue.
1for number in 1...5 {
2 if number == 3 {
3 continue
4 }
5 print(number)
6}
Terminal
$ swift main.swift
1
2
4
5
Every line, explained
for number in 1...5 {
Counts 1 to 5 using a closed range.
if number == 3 {
if checks a condition. Swift style: no round brackets around the condition, but the curly braces { } are always required. We single out the moment the counter hits 3.
continue
continue skips the REST of this trip and jumps straight to the next number. When number is 3, the print below never happens, so 3 is missing from the output.
}
This } closes the if block.
print(number)
print(...) shows whatever is inside its round brackets in the terminal, then moves to a new line. All lowercase, both brackets needed, and no semicolon at the end; Swift doesn't use them. Prints 1, 2, 4 and 5, but not 3.
}
This } closes the for loop.