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
- Closed and Half-Open Ranges
- One-Sided Ranges
- Counting with stride(from:to:by:)
- Ranges as Collections