Swift Academy · Lesson

Counting with stride(from:to:by:)

Step by custom increments including negatives.

Lesson 3 of 413 steps

Counting with stride(from:to:by:) is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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)
}

stride from through by

stride(from: a, through: b, by: step) is the inclusive version. It can reach b exactly if the step lands on it.

for n in stride(from: 0, through: 10, by: 5) {
    print(n)
}

Counting Down

Use a negative step to count downward. The by: value can be negative.

for n in stride(from: 5, through: 1, by: -1) {
    print(n)
}

Skipping Values

A larger step skips values, useful for every-Nth iteration.

for n in stride(from: 0, to: 20, by: 5) {
    print(n)
}

Stride With Doubles

Unlike ranges, stride works with floating-point values, letting you step by fractions.

for x in stride(from: 0.0, through: 1.0, by: 0.25) {
    print(x)
}

to vs through

The difference is the endpoint: to is exclusive, through is inclusive. Choose based on whether you want the final value.

print(Array(stride(from: 1, to: 4, by: 1)))
print(Array(stride(from: 1, through: 4, by: 1)))

Stride Into an Array

You can collect a stride into an array with Array(...).

let evens = Array(stride(from: 0, through: 10, by: 2))
print(evens)

Indexing With stride

Stride is handy for visiting every other index of a collection.

let items = ["a", "b", "c", "d", "e"]
for i in stride(from: 0, to: items.count, by: 2) {
    print(items[i])
}

Reverse Iteration

To walk an array from the end to the start, stride from the last index down to 0.

let nums = [10, 20, 30]
for i in stride(from: nums.count - 1, through: 0, by: -1) {
    print(nums[i])
}

Summing a Stride

Combine stride with accumulation to sum a sequence of stepped values.

var total = 0
for n in stride(from: 0, through: 100, by: 10) {
    total += n
}
print(total)

Quick Check

Test stride.

Recap: Counting with stride

stride(from:to:by:) is exclusive of the end, stride(from:through:by:) is inclusive. Use negative steps to count down and any numeric step, including doubles, to count by custom amounts.

for n in stride(from: 0, through: 6, by: 2) { print(n) }
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Counting with stride(from:to:by:)” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Counting with stride(from:to:by:)”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Counting with stride(from:to:by:)”?

Step by custom increments including negatives. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Counting with stride(from:to:by:)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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