Swift Range Operators

This post was originally posted at https://agrawalsuneet.github.io/blogs/swift-range-operators/ and reposted on Medium on 03rd Dec 2021.
//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)
}

There are 3 types of range operators

  1. Closed Range Operator
  2. Half-Open Range Operator
  3. One-Sided Range Operator

Please continue reading at https://agrawalsuneet.github.io/blogs/swift-range-operators/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store