(Swift for Beginners)
Swift Ranges
In this article, you will learn about range, its type and use cases in Swift.
A range is an interval of values. A simple example of range is 0,1,2,3,4,5,6,7,8,9 because the numbers are sequential from 0 to 9.
We can create range in swift using two range operators described below:
1. Closed Range Operator (lowerBound…upperBound)
It includes all the values in 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 includes all the values in the interval(lowerbound to upperBound) but excludes 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 includes all 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)
}
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 see what values the range contains.
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 includes all values from lowerbound to upper bound but excludes the upper bound value.
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)
}
When you run the program, the output will be:
1 2
In the above example, we’ve used for-in loop to see how half-open range works.
Instead of printing all the values, we can clearly see using half open operator only prints 1 and 2, and it excludes the upper bound value (i.e. 3).
3. One sided range
One sided range are those types of range that continue as far as possible in one direction. It can be created using both half open range operator and closed range operator but the operator can have a value on only one side.
Example 3: One-sided range less than 2
let range = ..<2
print(range.contains(-1))
print(range.contains(2))
When you run the program, the output will be:
true false
The above example creates a one sided range using half-open range operator that contains any numbers less than two.
To validate our result we have used contains
method. The contains
method simply returns true
if the element lies inside the range otherwise 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.
However, because of half-open range operator, the upper bound value(2) does not contains inside the range. So, range.contains(2)
returns false
.
Example 4:One-sided range starting from 2
let range = 2...
print(range.contains(100))
print(range.contains(1))
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.
However, it has a lower bound value (2), so range.contains(1)
returns false
.
Things to remember
- A range’s start must be less than or equal to its end. In our example
(lowerBound...upperBound)
, the lower bound value must be smaller than upper bound value. However, it can be a negative value.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 also use range operator to access elements of an array.
Swift programming for Beginners – Swift Ranges
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.