Swift programming for Beginners – Nested Loops in Swift

(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

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

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

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!