0Pricing
Swift Academy · Lesson

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

  1. Closed and Half-Open Ranges
  2. One-Sided Ranges
  3. Counting with stride(from:to:by:)
  4. Ranges as Collections
← Back to Swift Academy