Range Operator in Swift is a basic operator that is used to operate over a range. There are multiple types of range operators where we can include or exclude the upper range. Or we can start the range with some value or can end before some max value. The range operators can be used with for loops, if conditions, switch conditions or even in array iteration. First, let to see a basic example of a range operator.
A basic range operator can be defined with ...
having a lower range value to the left side and upper range value to the right side of ...
//For-in loop
for pos in 0...5 {
print(pos)
}
//If condition
if (0...5).contains(value){
print("between 0 to 5")
}
//Switch operator
let value = 5
switch(value){
case 0...5:
print("between 0 to 5")
default:
print("less than 0 or greater than 5")
}
//Array iteration
let numbers = ["One", "Two", "Three", "Four", "Five"]
for number in numbers[1...3] {
print(number)
}
Now that we understood the very basic use cases of the range operators, let’s try to understand its types.
There are 3 types of range operators
- Closed Range Operator
- Half-Open Range Operator
- One-Sided Range Operator
Lets try to understand them one by one with example.
Closed Range Operator
The closed range operator operates from an initial value say a
to a final value say b
including both the values (a...b
). The syntax for that would be
for pos in 0...5 {
print(pos)
}
The minimum criteria for closed range operator is the initial value should be less then or equal to the upper value else this will throw a run time error as below.
error: Fatal error: Range requires lowerBound <= upperBound
Half-Open Range Operator
The half range operator operates from an initial value say a
to a final value say b
excluding the final value (a..<b
). This is most useful when we work with zero-based lists such as arrays. The syntax for that would be
let numbers = ["One", "Two", "Three", "Four", "Five"]
for pos in 0..<numbers.count {
print("Item at \(pos) is \(numbers[pos])")
}
The same criteria of having a lower value should be less or equal to the upper value exist for half range operator also but if both lower and upper values are same, there will be no run time error but it will an empty statement or the condition will be false.
One-Sided Range Operator
The one-sided range operator is a special type of range operator where we skip either initial value or final value in case of closed range and we can skip only the initial value in case of half-open range.
Let’s understand it with the below example.
To make a closed range to a one-sided range, we can skip either the initial value or the final value.
let numbers = ["One", "Two", "Three", "Four", "Five"]
for number in numbers[...2] {
print(number)
//this will print One, Two and Three only
}
for number in numbers[2...] {
print(number)
//this will print Three, Four and Five only
}
To make a half-open range to a one-sided range, we can skip the initial value.
let numbers = ["One", "Two", "Three", "Four", "Five"]
for number in numbers[..<2] {
print(number)r
//this will print One and Two only
}
All the above three types of range operators can be used with loops, if conditions, array iterations and even with switch cases statements according to the use cases.