Counting with stride(from:to:by:)
Step by custom increments including negatives.
Why stride Exists
Plain ranges step by 1. To count by other amounts, count backwards, or use decimals, Swift provides the stride function.
for n in stride(from: 0, to: 10, by: 2) {
print(n)
}stride from to by
stride(from: a, to: b, by: step) produces values starting at a, stepping by step, stopping before b. The end is exclusive.
for n in stride(from: 0, to: 5, by: 1) {
print(n)
}All lessons in this course
- Closed and Half-Open Ranges
- One-Sided Ranges
- Counting with stride(from:to:by:)
- Ranges as Collections