What the for-in loop does
Swift has no C-style for (int i = 0; i < 4; i++) loop. Its for loop always goes through the values of a sequence, and a range is the simplest sequence to loop over:
for variable in range {
instruction1
instruction2
}
On each pass the loop sets variable to the next value of the range and runs the body. When the range runs out, the loop ends. You never declare the variable yourself and you never increment it: Swift creates it for each pass and moves to the next value on its own. Inside the body it is a constant, so assigning to it is a compile-time error.
Ranges: ..< and ...
Swift has two range operators:
0..<4is a half-open range. It starts at 0 and stops before 4, so it holds 0, 1, 2 and 3.0...4is a closed range. It includes both ends, so it holds 0, 1, 2, 3 and 4.
The half-open form is the usual choice for counting a number of repetitions, because 0..<n runs exactly n times.
for i in 0..<4 {
print("Hello world!")
}
This prints Hello world! on four lines. The loop does these steps:
- Take the first value of the range:
iis 0. Run the body. - Take the next value:
iis 1. Run the body. - Do the same for 2 and 3.
- The range has no more values, so the loop ends.
The body ran four times, once for each of 0, 1, 2 and 3. The loop variable i is only visible inside the loop.
Ignoring the variable with _
The example above never uses i. When you only want to repeat something, write an underscore in its place:
for _ in 0..<4 {
print("Hello world!")
}
The underscore tells Swift, and the reader, that no name is needed. It also avoids the warning Swift can give about a loop variable that is never read.
The exercise: print a sentence five times
The task is to print I promise to learn coding. five times. It is built in two steps.
- Write an empty loop over
0..<5. A half-open range from 0 holds exactly five values, so the body runs five times. Use_because the message doesn't depend on the index. - Put the
printcall inside the braces. The body runs once per value, so oneprintline gives five lines of output.
for _ in 0..<5 {
print("I promise to learn coding.")
}
Common mistakes
- Using
0...5by accident. The closed range holds six values, so the message is printed six times. - Forgetting the braces. Unlike some languages, Swift requires braces around a loop body, even for a single statement.
- Trying to change the loop variable. Writing
i += 1inside the loop is an error becauseiis a constant. To change how the loop moves, change the range. - Using a closed range that runs backwards. A range like
5...1has its start above its end, which isn't a valid range, and the program crashes at run time. Counting down needsstrideorreversed(). - Writing a C-style loop.
for (i = 0; i < 5; i++)doesn't compile in Swift.
Key takeaways
for variable in rangeruns the body once for each value in the range.- Swift creates the loop variable and advances it for you.
a..<bexcludesb, anda...bincludes it.0..<nrepeats a block exactlyntimes.- Use
_when you don't need the value.
