0Pricing
Swift Academy · Lesson

Closed and Half-Open Ranges

Use ... and ..< correctly in loops and slices.

What Is a Range

A range represents a sequence of values between a lower and upper bound. Swift has two main range operators for this.

let r = 1...5
for n in r {
    print(n)
}

Closed Range

The closed range operator a...b includes both endpoints. 1...5 contains 1, 2, 3, 4, and 5.

for n in 1...5 {
    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