# Swift Ranges

Swift Ranges: In this tutorial, you will learn about range, its sort and use cases in Swift.

You use ranges in Swift to define values between a lower and upper limit. Ranges are useful for creating slices of arrays, checking if a value is contained in a range, and considerably more.

A range is an interval of values. A straightforward illustration of range is 0,1,2,3,4,5,6,7,8,9 on the grounds that the numbers are consecutive from 0 to 9.

We can create range in swift using two range operators depicted underneath:

## 1. Closed Range Operator (lowerBound…upperBound)

It remembers every one of the values for the interval(lowerbound to upperBound). It is declared using… (3 dots)operator.

E.g: 1…3 Defines range containing values 1,2,3

### 2. Half Open Range Operator (lowerBound..<upperBound)

It remembers every one of the values for the interval(lowerbound to upperBound) however bars the last (upperBound) number. It is declared using..< operator.

E.g: 1..<3 Defines range containing values 1 and 2

## Types of Range

### 1. Closed Range (lowerBound…upperBound)

Ranges created using the closed range operator are called as closed range. It incorporates every one of the values from lowerbound to upperbound.

Example 1: Printing closed range values using for-in loop

```// 1...3 Defines a range containing values 1, 2 and 3
for value in 1...3 {
print(value)
}```

At the point 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 perceive 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.

## 2. Half Open Range (lowerBound..<upperBound)

Ranges created using the half open range operator are called as half open ranges. It incorporates all values from lowerbound to upper bound however avoids the upper bound worth.

### Example 2: Printing half open range values using for-in loop

```// 1..<3 Defines a range containing values 1,2
for value in 1..<3 {
print(value)
}```

At the point when you run the program, the output will be:

```1
2```

In the above example, we’ve used for-in loop to perceive how half-open range functions.

Rather than printing every one of the values, we can plainly see using half open operator just prints 1 and 2, and it excludes the upper bound worth (for example 3).

## 3. One sided range

One sided range are those sorts of range that proceed beyond what many would consider possible one direction. It can be created using both half open range operator and closed range operator but the operator can have a worth on just one side.

### Example 3: One-sided range less than 2

```let range = ..<2
print(range.contains(-1))
print(range.contains(2))```

At the point when you run the program, the output will be:

```true
false```

The above example creates a one side range using half-open range operator that contains any numbers less than two.

To validate our outcome we have used contains technique. The contains strategy essentially returns true if the element lies inside the range else 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.

Nonetheless, because of half-open range operator, the upper bound value(2) doesn’t contains inside the range. Along these lines, range.contains(2) returns false.

### Example 4:One-sided range starting from 2

```let range = 2...
print(range.contains(100))
print(range.contains(1))```

At the point 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.

In any case, it has a lower bound worth (2), so range.contains(1) returns false.

## Things to remember

• A range’s beginning should be not exactly or equivalent to its end. In our example (lowerBound…upperBound), the lower bound worth should be smaller than upper bound worth. Be that as it may, it can be a negative worth.

Example:

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

• We can iterate over range (excluding one sided range) using for-in loops.

• We can likewise use range operator to access elements of an array.

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.