A range is an interval of values. A simple example of range is 0,1,2,3,4,5,6,7,8,9 because the numbers are sequential from 0 to 9.
We can create range in swift using two range operators described below:
It includes all the values in the interval(lowerbound to upperBound). It is declared using …
(3 dots)operator.
E.g: 1...3
Defines range containing values 1,2,3
It includes all the values in the interval(lowerbound to upperBound) but excludes the last (upperBound) number. It is declared using ..<
operator.
E.g: 1..<3
Defines range containing values 1 and 2
Ranges created using the closed range operator are called as closed range. It includes all the values from lowerbound to upperbound.
// 1...3 Defines a range containing values 1, 2 and 3
for value in 1...3 {
print(value)
}
When you run the program, the output will be:
1 2 3
The above example creates a range that contains numbers from 1 to 3 ( 1...3
). We used a for-in loop to see what values the range contains. To learn more about for-in loops, visit Swift for-in loop.
Using for-in loop, we can see closed range contains all values in the given range including the lower (1) and upper (3) bound values.
Ranges created using the half open range operator are called as half open ranges. It includes all values from lowerbound to upper bound but excludes the upper bound value.
// 1..<3 Defines a range containing values 1,2
for value in 1..<3 {
print(value)
}
When you run the program, the output will be:
1 2
In the above example, we’ve used for-in loop to see how half-open range works.
Instead of printing all the values, we can clearly see using half open operator only prints 1 and 2, and it excludes the upper bound value (i.e. 3).
One sided range are those types of range that continue as far as possible in one direction. It can be created using both half open range operator and closed range operator but the operator can have a value on only one side.
let range = ..<2
print(range.contains(-1))
print(range.contains(2))
When you run the program, the output will be:
true false
The above example creates a one sided range using half-open range operator that contains any numbers less than two.
To validate our result we have used contains
method. The contains
method simply returns true
if the element lies inside the range otherwise it returns false
.
range.contains(-1)
checks if -1 lies inside the range or not. Since, its one sided range with upper bound 2 ,and -1 < 2 it lies inside the range and print(range.contains(-1))
outputs true
in the screen.
However, because of half-open range operator, the upper bound value(2) does not contains inside the range. So, range.contains(2)
returns false
.
let range = 2...
print(range.contains(100))
print(range.contains(1))
When you run the program, the output will be:
true false
The above example creates a one sided range using closed operator that contains numbers from 2 to any values greater than 2.
range.contains(100)
checks if 100 lies inside the range or not. Since, its one sided range and 100 is greater than 2, it lies inside the range and prints true
in the screen.
However, it has a lower bound value (2), so range.contains(1)
returns false
.
(lowerBound...upperBound)
, the lower bound value must be smaller than upper bound value. However, it can be a negative value.3...1 // fatal error: Can't form Range with upperBound < lowerBound -3...1 // This is a valid range. Lowerbound/Upperbound can contain a negative value but should valid the above statement.
It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:
Disabling AdBlock on Programiz. We do not use intrusive ads.
or
Donate on Paypal