English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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.
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
Nestedwhile loopContaining a while loop, as a statement in another while loop. You can set any number of nested while loops as needed.
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.
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.
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.
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.
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.
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