Challenge A: Looping with stride functions
In the last chapter you wrote some for-loops with ranges. Ranges are limited in that they must always be increasing. The Swift stride(from:to:by:) and stride(from:through:by:) functions let you loop much more flexibly. For example, if you wanted to loop from 10 to 20 by 4s you can write:
for index in stride(from: 10, to: 22, by: 4) { print(index)
}
// prints 10, 14, 18
for index in stride(from: 10, through: 22, by: 4) { print(index)
}
// prints 10, 14, 18, and 22
- What is the difference between the two stride function overloads?
- Write a loop that goes from 10.0 to (and including) 9.0, decrementing by 0.1.