Swift Tutorial Swift Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Swift - Range Operators



Swift provides several range operators, which can be used to create a range of values. These operators can be used in the following ways:

  • Closed Range Operator
  • Half-Open Range Operator
  • One-Sided Ranges

Closed Range Operator

The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b. The closed range operator is useful when iterating over a range in which the user want all of the values to be used.

Example:

In the example below, a for-in loop is used to iterate over a closed range to print all elements of the range.

//iterating over a closed range to
//print all elements in the range
print("iterating over closed range")
for i in (1...6) {
  print("i = \(i)")
}

The output of the above code will be:

iterating over closed range
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6

Half-Open Range Operator

The closed range operator (a..<b) defines a range that runs from a to b, but does not include b. The value of a must not be greater than b. The half-open range operator is useful when iterating over a zero-based lists such as arrays, where it is useful to count up to (but not including) the length of the list.

Example:

In the example below, a for-in loop is used to iterate over a half-open range to print all elements of the range. After that it is also used to access all elements of an array.

//iterating over a half-open range to
//print all elements in the range
print("iterating over half-open range")
for i in (1..<6) {
  print("i = \(i)")
}

var MyArray = ["Mon", "Tue", "Wed", "Thu", "Fri"]
//iterating over MyArray to print 
//of its elements
print("\nMyArray contains:")
for i in (0..<MyArray.count) {
  print(MyArray[i])
}

The output of the above code will be:

iterating over half-open range
i = 1
i = 2
i = 3
i = 4
i = 5

MyArray contains:
Mon
Tue
Wed
Thu
Fri

One-Sided Ranges

The closed range operator has an alternative form for ranges that continue as far as possible in one direction, for example: a range that includes all the elements of an array from index 2 to the end of the array. In these cases, the value from one side of the range operator cab be omitted. This kind of range is called a one-sided range.

Example:

In the example below, a one-sided range is used to access elements of an array.

var MyArray = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

//using one-sided range to access elements
//of MyArray with index in range [0, 4]
print("\nElements are:")
for i in MyArray[...4] {
  print(i)
}

//using one-sided range to access elements
//of MyArray with index in range [2, 6]
print("\nElements are:")
for i in MyArray[2...] {
  print(i)
}

The output of the above code will be:

Elements are:
Mon
Tue
Wed
Thu
Fri

Elements are:
Wed
Thu
Fri
Sat
Sun

Example:

Consider one more example to understand one-sided range concept.

var MyArray = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

//using one-sided range to access elements
//of MyArray with index in range [0, 4]
//mixed with half-open range
print("\nElements are:")
for i in MyArray[..<5] {
  print(i)
}

print()

//using one-sided range without subscripts
var range = ...5
print(range.contains(10))
print(range.contains(3))
print(range.contains(-5))

The output of the above code will be:

Elements are:
Mon
Tue
Wed
Thu
Fri

false
true
true

❮ Swift - Operators

5