0Pricing
Swift Academy · Lesson

for-in Loops with Ranges

Iterating with for-in using integer ranges and closed/half-open range operators.

for-in Loops with Ranges is a free Swift Academy lesson on CoddyKit — lesson 1 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.

Welcome

Loops let you repeat work without repeating code. The `for-in` loop is Swift's most versatile iteration tool, working with ranges, collections, and any sequence.

Closed Range: a...b

The closed range operator `...` includes both endpoints: ```swift for i in 1...5 { print(i) // 1, 2, 3, 4, 5 } ``` This is the most common range used with `for-in`.

Half-Open Range: a..<b

The half-open range `..<` excludes the upper bound — perfect for 0-based indexing: ```swift for i in 0..<5 { print(i) // 0, 1, 2, 3, 4 } ``` Useful when iterating array indices: `0..

One-Sided Ranges

One-sided ranges extend to one end: ```swift let nums = [10, 20, 30, 40, 50] for n in nums[2...] { print(n) } // 30, 40, 50 for n in nums[..<3] { print(n) } // 10, 20, 30 ``` Very handy when slicing collections from/to a position.

stride(from:to:by:)

Use `stride` when you need a custom step size: ```swift for i in stride(from: 0, to: 10, by: 2) { print(i) // 0, 2, 4, 6, 8 } for i in stride(from: 10, through: 0, by: -3) { print(i) // 10, 7, 4, 1 } ```

Ignoring the Loop Variable

Use `_` when you don't need the iteration value: ```swift var sum = 0 for _ in 1...10 { sum += 1 } print(sum) // 10 ``` This signals to readers that the count matters, not the index.

Iterating Characters in a String

Strings conform to `Sequence`, so you can iterate characters directly: ```swift for char in "Swift" { print(char) // S, w, i, f, t } ``` Each `char` is of type `Character`.

Ranges as Values

Ranges are first-class Swift values and can be stored: ```swift let valid: ClosedRange = 1...100 if valid.contains(42) { print("In range") } ``` Useful for validation and switch case matching.

Nested for-in Loops

Loops can be nested for multi-dimensional iteration: ```swift for row in 1...3 { for col in 1...3 { print("(\(row),\(col))", terminator: " ") } print() } ``` Watch out for O(n²) complexity when nesting deep.

Counting Down with Reversed

Reverse a range with `.reversed()`: ```swift for i in (1...5).reversed() { print(i) // 5, 4, 3, 2, 1 } ``` Alternatively use `stride(from:through:by:)` with a negative step.

Quick Check

Which range operator **excludes** the upper bound?

Recap

Key takeaways: • `a...b` — closed range (includes b) • `a..

Frequently asked questions

Is the “for-in Loops with Ranges” lesson free?

Yes — the full text of “for-in Loops with Ranges” 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 “for-in Loops with Ranges”?

Iterating with for-in using integer ranges and closed/half-open range operators. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “for-in Loops with Ranges” 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. for-in Loops with Ranges
  2. while and repeat-while Loops
  3. Iterating Collections with for-in
  4. break, continue and Labeled Statements
← Back to Swift Academy