0Pricing
Swift Academy · Lesson

Counting with stride(from:to:by:)

Step by custom increments including negatives.

Counting with stride(from:to:by:) is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

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

Yes — the full text of “Counting with stride(from:to:by:)” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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