# Swift Range Operators

--

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.

*This post was originally posted at *https://agrawalsuneet.github.io/blogs/swift-range-operators/ *and reposted on Medium on 03rd Dec 2021.*

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 in0...5{

print(pos)

}//If condition

if (0...5).contains(value){

print("between 0 to 5")

}//Switch operator

let value = 5switch(value){

case0...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