”;
Swift supports a special type of operator known as a range operator. The range operator is used to create a range of values. They are generally used with a for-in loop. There are three types of range operators are available in Swift −
Operator | Name | Example |
---|---|---|
(a…b) | Closed Range | 1…4 return 1, 2, 3, 4 |
(a..<b) | Half-Open Range | 1..<3 return 1, and 2 |
(a…) and (…a) | One-Sided Range | (1..) return 1, 2, 3, … end of the element (…2) return beginning to 2 |
Closed Range Operator in Swift
A closed range operator is used to define a range that contains both starting and end values. In other words, a closed range is used to create a range from x to y, including the value of x and y. Here the starting value or value of x must not be greater than the ending value or value of y.
Syntax
Following is the syntax of the closed range operator −
x…y
Example
Swift program to display a sequence of numbers using a closed range operator.
import Foundation // For loop to display a sequence of numbers for x in 3...11{ print(x) }
Output
3 4 5 6 7 8 9 10 11
Half-Open Range Operator in Swift
A half-open range operator is used to create a range from x to y. This range only includes the starting value and does not include the ending value or y. Also, the value of x or the starting value should not be greater than the ending value of y.
Or if the value of x is equal to y, then the range will be empty. This operator is generally used with zero-based lists like arrays, etc.
Syntax
Following is the syntax of the half-open range operator −
x..<y
Example
Swift program to display the elements of an array using a half-open range operator.
import Foundation let arr = [1, 4, 6, 2, 7] for x in 0..<arr.count{ print(arr[x]) }
Output
1 4 6 2 7
One-Sided Ranges in Swift
The one-sided ranges are used to define ranges that have a value of only one side. Such types of ranges are used when we need to know from where the iteration should begin. One-sided ranges are of two types −
-
Partial Range from Start − It defines a range that starts from the beginning of the given collection or sequence and goes to the specified end value. This range contains only the end value and does not contain the starting value.
Syntax
Following is the syntax of partial range from start −
…x or ..<x
-
Partial Range to the End − It creates a range that starts from the specified value and goes all the way to the end of the given collection or sequence. This range contains only the starting value and does not contain the end value.
Syntax
Following is the syntax of partial range to the end −
x…
Example
Swift program to demonstrate partial range to the start.
import Foundation let arr = [1, 4, 6, 2, 7] // This one-sided range displays elements till index 3 print("Sequence 1:") for x in arr[...3]{ print(x) } // This one-sided range displays all the elements before index 3 print("Sequence 2:") for y in arr[..<3]{ print(y) }
Output
Sequence 1: 1 4 6 2 Sequence 2: 1 4 6
Example
Swift program to demonstrate partial range to the end.
import Foundation let arr = [1, 4, 6, 2, 7] // This one-sided range displays all elements // starting from index 2 to the end of the sequence print("Sequence:") for x in arr[2...]{ print(x) }
Output
Sequence: 6 2 7
”;