Swift Range Operators
Creating ranges of values in Swift
📏 What are Range Operators?
Range operators in Swift create sequences of values between two bounds. They include closed ranges (...), half-open ranges (..<), and one-sided ranges, commonly used in loops, array slicing, and pattern matching.
// Creating ranges
let closedRange = 1...5 // 1, 2, 3, 4, 5
let halfOpenRange = 1..<5 // 1, 2, 3, 4
Types of Range Operators
Closed Range
Includes both start and end values
1...5
// Includes: 1, 2, 3, 4, 5
Half-Open Range
Excludes the end value
1..<5
// Includes: 1, 2, 3, 4
One-Sided Range
Open-ended ranges
2... // From 2 onwards
...3 // Up to 3
Partial Range
Half-open one-sided range
..<3 // Up to but not including 3
🔹 Closed Range Operator (...)
The closed range operator includes both the start and end values:
// Basic closed range
for i in 1...5 {
print(i)
}
// Output: 1, 2, 3, 4, 5
// Using with arrays
let fruits = ["apple", "banana", "cherry", "date", "elderberry"]
for fruit in fruits[1...3] {
print(fruit)
}
// Output: banana, cherry, date
// Character ranges
for letter in "a"..."e" {
print(letter)
}
// Output: a, b, c, d, e
Output:
2
3
4
5
banana
cherry
date
a
b
c
d
e
🔹 Half-Open Range Operator (..<)
The half-open range operator excludes the final value:
// Half-open range in loop
for i in 1..<5 {
print(i)
}
// Output: 1, 2, 3, 4
// Perfect for array indices
let colors = ["red", "green", "blue", "yellow"]
for i in 0..<colors.count {
print("\(i): \(colors[i])")
}
// Output: 0: red, 1: green, 2: blue, 3: yellow
// Array slicing
let numbers = [10, 20, 30, 40, 50]
let slice = Array(numbers[1..<4])
print(slice) // Output: [20, 30, 40]
Output:
2
3
4
0: red
1: green
2: blue
3: yellow
[20, 30, 40]
🔹 One-Sided Ranges
One-sided ranges extend infinitely in one direction:
🔸 Partial Range From (...)
let names = ["Alice", "Bob", "Charlie", "David", "Eve"]
// From index 2 to the end
let fromIndex2 = Array(names[2...])
print(fromIndex2) // Output: ["Charlie", "David", "Eve"]
// Check if number is in range
let score = 85
if (80...).contains(score) {
print("Score \(score) is 80 or above")
}
// Output: Score 85 is 80 or above
Output:
Score 85 is 80 or above
🔸 Partial Range Through (...)
// From beginning up to index 2
let upToIndex2 = Array(names[...2])
print(upToIndex2) // Output: ["Alice", "Bob", "Charlie"]
// Grade checking
let grade = 75
switch grade {
case ...59:
print("F")
case 60...69:
print("D")
case 70...79:
print("C")
case 80...89:
print("B")
case 90...:
print("A")
default:
print("Invalid grade")
}
// Output: C
Output:
C
🔸 Partial Range Up To (..<)
// From beginning up to (but not including) index 2
let upToButNotIndex2 = Array(names[..<2])
print(upToButNotIndex2) // Output: ["Alice", "Bob"]
Output:
🔹 Practical Examples
Real-world usage of range operators:
// Password strength checker
func checkPasswordStrength(_ password: String) -> String {
let length = password.count
switch length {
case ...5:
return "Weak"
case 6...8:
return "Medium"
case 9...:
return "Strong"
default:
return "Invalid"
}
}
print(checkPasswordStrength("abc")) // Output: Weak
print(checkPasswordStrength("password")) // Output: Strong
// Age group classification
func getAgeGroup(_ age: Int) -> String {
switch age {
case ...12:
return "Child"
case 13...19:
return "Teenager"
case 20...64:
return "Adult"
case 65...:
return "Senior"
default:
return "Invalid age"
}
}
print(getAgeGroup(25)) // Output: Adult
print(getAgeGroup(70)) // Output: Senior
Output:
Strong
Adult
Senior