English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Swift Nested Loops

In this article, you will learn about nested loops and how they work through examples.

If a loop exists within the body of another loop, it is called a nested loop. This is a nested for-Example of in loop.

for i in 1...5 {
    //Outer loop statements
    for j in 1...2 {
        //Inner loop statements
    }
    //Outer loop statements
}

Here, for j in 1...2 The loop called the inner loop, it is located in the for i in 1...5Called the outer loop body.

It should be noted that nested loops may not contain loops of the same type. For example, you can place a while loop inside the body of a for loop, but it is still a nested loop.

Swift nested for...in loops

Nestedfor-in loopin another for-in loop containing a for-in loop as a statement. You can have any number of nested for loops as needed.-in loop.

Example1: 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)")
    }
}

The output when running the program is:

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

The outer loop iterates5time(s). In each iteration of the outer loop, the inner loop iterates2time(s). In each iteration of the outer loop, the inner loop iterates

Swift nested while loop

Nestedwhile loopContaining a while loop, as a statement in another while loop. You can set any number of nested while loops as needed.

Example2: Swift nested while loops

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 this program is the same as the above program.

Swift nested repeat...while loops

NestedRepeat while loop Containing a repeat-while loop as another repeat-Statements in a while loop. You can set any number of nested while loops as needed.

Example3: Swift nested repeat...while loops

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
    } 2)
    i += 1
} 5)

The output of this program is the same as the above program.

Different types of Swift nested loops

It is not necessary to have nested loops of the same type. Nested loops can also be created by placing one type of loop inside another type of loop.

Example3: Swift Nested Loops of while and for

The following program includes different types of nested loops (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 this program is the same as the above program.

Example4Program to create patterns using Swift loops

Nested loops are often used in programming to create patterns. The following program demonstrates how to use nested loops to create simple patterns.

let rows = 5
for i in 1...rows {
    for j in 1...i {
        print("\(j) ", terminator: "")
    }
    print("")
}

The output when running the program is:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5