In this article, you will learn-
Nested Loops in Swift
Swift Nested Loops: In this tutorial, you will learn about nested loops and how it functions, with examples
On the off chance that a loop exists inside the body of another loop, it’s known as a nested loop.
In the event that a loop exists inside the body of another loop, it’s known as a nested loop. Here’s an illustration of a 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 ought to be noticed that, the nested loop may not contain loops of the same type. For instance, you can put some time loop inside the body of a for loop and it is as yet 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 quite a few 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)") } }
At the point 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 external loop iterates 5 times. In each iteration of the external loop, the internal loop iterates 2 times.
Swift Nested while Loop
Swift nested while loop contains some time loop as a statement inside another while loop. You can have quite a few 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 a statement inside another repeat while loop. You can have quite a few 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 isn’t important to have nested loops of the same sort. You can likewise create varieties of nested loops by putting a type of loop inside different sorts of loops.
Example 3: Swift Nested Loop of while and for
The underneath program contains nested loop of various 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 often used to create designs in programming. Underneath program shows how you can create a straightforward example using nested loops.
let rows = 5 for i in 1...rows { for j in 1...i { print("\(j) ", terminator: "") } print("") }
At the point when you run the program, the output will be:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.