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