For loops
In the previous chapter you looked at while loops. Now that you know about ranges, it’s time to look at another type of loop: the for loop. This is probably the most common loop you’ll see, and you’ll use it to run code a certain number of times.
You construct a for loop like this:
for <CONSTANT> in <RANGE> {
<LOOP CODE>
}
The loop begins with the for keyword, followed by a name given to the loop constant (more on that shortly), followed by in, followed by the range to loop through.
Here’s an example:
let count = 10 var sum = 0
for i in 1...count { sum += i
}
In the code above, the for loop iterates through the range 1 to count. At the first iteration of the loop, i will equal the first element in the range: 1. Each time around the loop, i will increment until it’s equal to count; the loop will execute one final time and then finish.
Note: If you’d used a half-open range, the the last iteration would see i equal to count - 1.
Inside the loop, you add i to the sum variable; it runs 10 times to calculate the sequence 1 + 2 + 3 + 4 + 5 + ... all the way up to 10.
Here are the values of the constant i and variable sum for each iteration:
- Start of iteration 1: i = 1, sum = 0
- Start of iteration 2: i = 2, sum = 1
- Start of iteration 3: i = 3, sum = 3
- Start of iteration 4: i = 4, sum = 6
- Start of iteration 5: i = 5, sum = 10
- Start of iteration 6: i = 6, sum = 15
- Start of iteration 7: i = 7, sum = 21
- Start of iteration 8: i = 8, sum = 28
- Start of iteration 9: i = 9, sum = 36
- Start of iteration 10: i = 10, sum = 45
- After iteration 10: sum = 55
In terms of scope, the i constant is only visible inside the scope of the for loop, which means it’s not available outside of the loop.
Note: If you’re mathematically astute, you might notice that this example computes triangle numbers. Here’s a quick explanation: http://bbc.in/ 1O89TGP
Xcode’s playground gives you a handy way to visualize such an iteration. Hover over the sum += i line in the results pane, and you’ll see a white dot on the right. Hover over that dot to reveal a plus (+) button:
Click this plus (+) button and Xcode will display a graph underneath the line within the playground code editor:
This graph lets you visualize the sum variable as the loop iterates.
Finally, sometimes you only want to loop a certain number of times, and so you don’t need to use the loop constant at all. In that case, you can employ the underscore to indicate you’re ignoring it, like so:
let count = 10 var sum = 1 var lastSum = 0
for _ in 0..<count { let temp = sum
sum = sum + lastSum lastSum = temp
}
This code doesn’t require a loop constant; the loop simply needs to run a certain number of times. In this case, the range is 0 through count and is half-open. This is the usual way of writing loops that run a certain number of times.
It’s also possible to only perform the iteration under certain conditions. For example, imagine you wanted to compute a sum similar to that of triangle numbers, but only for odd numbers:
let count = 10 var sum = 0
for i in 1...count where i % 2 == 1 { sum += i
}
The loop above has a where clause in the for loop statement. The loop still runs through all values in the range 1 to count, but it will only execute the loop’s code block when the where condition is true; in this case, where i is odd.