(Swift for Beginners)
Nested Loops in Swift
In this article, you will learn about nested loops and how it works, with examples.
If a loop exists inside the body of another loop, it’s called a nested loop. Here’s an example of nested for-in loop.
for i in 1...5 { //statements of outer loop for j in 1...2 { //statements of inner loop } //statements of outerloop }
Here, the for j in 1...2
loop known as inner loop, is inside the body of for i in 1...5
known as outer loop.
It should be noted that, nested loop may not contain loops of same type. For example, you can put a while loop inside the body of a for loop and it is still a nested loop.
Swift Nested for-in Loop
A nested for-in loop contains a for-in loop as statement inside another for-in loop. You can have any number of nested for-in loops as required.
Example 1: Swift Nested for-in Loop
for i in 1...5 {
print("Outer loop iteration ", i)
for j in 1...2 {
print("Inner loop iteration ", j)
print("i = (i); j = (j)")
}
}
When you run the program, the output will be:
Outer loop iteration 1 Inner loop iteration 1 i = 1; j = 1 Inner loop iteration 2 i = 1; j = 2 Outer loop iteration 2 Inner loop iteration 1 i = 2; j = 1 Inner loop iteration 2 i = 2; j = 2 Outer loop iteration 3 Inner loop iteration 1 i = 3; j = 1 Inner loop iteration 2 i = 3; j = 2 Outer loop iteration 4 Inner loop iteration 1 i = 4; j = 1 Inner loop iteration 2 i = 4; j = 2 Outer loop iteration 5 Inner loop iteration 1 i = 5; j = 1 Inner loop iteration 2 i = 5; j = 2
In the above program, the outer loop iterates 5 times. In each iteration of outer loop, the inner loop iterates 2 times.
Swift Nested while Loop
A nested while loop contains a while loop as statement inside another while loop. You can have any number of nested while loops as required.
Example 2: Swift Nested while Loop
var i = 1
while i <= 5 {
print("Outer loop iteration ", i)
var j = 1
while j <= 2 {
print("Inner loop iteration ", j)
print("i = (i); j = (j)")
j += 1
}
i += 1
}
The output of the program is same as the above program.
Swift Nested repeat-while Loop
A nested repeat while loop contains a repeat-while loop as statement inside another repeat-while loop. You can have any number of nested while loops as required.
Example 3: Swift Nested repeat-while Loop
var i = 1
repeat {
print("Outer loop iteration ", i)
var j = 1
repeat {
print("Inner loop iteration ", j)
print("i = (i); j = (j)")
j += 1
} while (j <= 2)
i += 1
} while (i <= 5)
The output of the program is same as the above program.
Swift Nested Loop of different types
It is not necessary to have nested loops of same type. You can also create variations of nested loops by putting a type of loop inside other types of loops.
Example 3: Swift Nested Loop of while and for
The below program contains nested loop of different types (while and for-in loop).
var i = 1
while i <= 5 {
print("Outer loop iteration ", i)
for j in 1...2 {
print("Inner loop iteration ", j)
print("i = (i); j = (j)")
}
i += 1
}
The output of the program is same as the above program.
Example 4: Program to create a pattern with Swift loops
Nested loops are frequently used to create patterns in programming. Below program shows how you can create a simple pattern using nested loops.
let rows = 5
for i in 1...rows {
for j in 1...i {
print("(j) ", terminator: "")
}
print("")
}
When you run the program, the output will be:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Swift programming for Beginners – Nested Loops in Swift
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.