A nested loop is a loop placed inside another loop. The outer loop controls the larger repetition, while the inner loop runs completely for each iteration of the outer loop.
A loop inside another loop is called a nested loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
std::cout << i << " " << j << std::endl;
}
}
Here, the outer for loop contains another for
loop.
for (initialization; condition; update) {
for (initialization; condition; update) {
// inner loop statements
}
}
The inner loop is executed for every iteration of the outer loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
std::cout << j << " ";
}
std::cout << std::endl;
}
Output:
1 2 3
1 2 3
1 2 3
The inner loop runs three times for each outer loop iteration.
Consider:
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
std::cout << i << "," << j << std::endl;
}
}
The execution is:
i = 1.j = 1, 2, 3.i = 2.j = 1, 2, 3.for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 4; column++) {
std::cout << "* ";
}
std::cout << std::endl;
}
Output:
* * * *
* * * *
* * * *
The outer loop represents rows and the inner loop represents columns.
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
std::cout << j << " ";
}
std::cout << std::endl;
}
Output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
std::cout << j << " ";
}
std::cout << std::endl;
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}
Output:
*
* *
* * *
* * * *
* * * * *
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}
Output:
* * * * *
* * * *
* * *
* *
*
for (int i = 1; i <= 3; i++) {
std::cout << "Table of " << i
<< std::endl;
for (int j = 1; j <= 10; j++) {
std::cout << i
<< " x "
<< j
<< " = "
<< i * j
<< std::endl;
}
std::cout << std::endl;
}
The outer loop selects the table and the inner loop generates its multiplication values.
int i = 1;
while (i <= 3) {
int j = 1;
while (j <= 3) {
std::cout << j << " ";
j++;
}
std::cout << std::endl;
i++;
}
Nested loops are not limited to for loops. A while loop
can also contain another while loop.
int i = 1;
do {
int j = 1;
do {
std::cout << j << " ";
j++;
} while (j <= 3);
std::cout << std::endl;
i++;
} while (i <= 3);
A do-while loop can also be placed inside another do-while loop.
Different types of loops can be nested together.
for (int i = 1; i <= 3; i++) {
int j = 1;
while (j <= 3) {
std::cout << i
<< ","
<< j
<< std::endl;
j++;
}
}
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 3) {
std::cout << "X ";
}
else {
std::cout << "* ";
}
}
std::cout << std::endl;
}
An if statement can control what is displayed inside the inner loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 3) {
break;
}
std::cout << j << " ";
}
std::cout << std::endl;
}
The break statement terminates only the inner loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 3) {
continue;
}
std::cout << j << " ";
}
std::cout << std::endl;
}
The continue statement skips 3 in the inner loop while the remaining values continue to execute.
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
Output:
10 20 30
40 50 60
Nested loops are commonly used to process two-dimensional arrays.
int a[2][2] = {
{1, 2},
{3, 4}
};
int b[2][2] = {
{5, 6},
{7, 8}
};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
std::cout << a[i][j] + b[i][j]
<< " ";
}
std::cout << std::endl;
}
The outer loop handles rows and the inner loop handles columns.
for (int x = 1; x <= 3; x++) {
for (int y = 1; y <= 3; y++) {
std::cout << "("
<< x
<< ","
<< y
<< ") "
;
}
std::cout << std::endl;
}
Nested loops can generate combinations of rows and columns or coordinate values.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
std::cout << i
<< " + "
<< j
<< " = "
<< i + j
<< std::endl;
}
}
The inner loop creates different pairs for each value of the outer loop.
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
std::cout << i * j << "\t";
}
std::cout << std::endl;
}
Output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
The outer loop controls rows and the inner loop controls columns.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4; j++) {
std::cout << "Hello"
<< std::endl;
}
}
The outer loop runs 3 times and the inner loop runs 4 times for each outer iteration.
Total executions:
3 × 4 = 12
int rows, columns;
std::cout << "Enter rows: ";
std::cin >> rows;
std::cout << "Enter columns: ";
std::cin >> columns;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= columns; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}
The user can decide how many rows and columns should be displayed.
int marks[3][3] = {
{70, 80, 90},
{60, 75, 85},
{88, 92, 95}
};
for (int student = 0; student < 3; student++) {
std::cout << "Student "
<< student + 1
<< ": ";
for (int subject = 0; subject < 3; subject++) {
std::cout << marks[student][subject]
<< " ";
}
std::cout << std::endl;
}
The outer loop represents students and the inner loop represents subjects.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
std::cout << i
<< ","
<< j
<< std::endl;
}
}
#include <iostream>
int main() {
int rows;
std::cout << "Enter number of rows: ";
std::cin >> rows;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}
return 0;
}
If the user enters 5, the program displays a triangle pattern.
#include <iostream>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
std::cout << matrix[i][j]
<< " ";
}
std::cout << std::endl;
}
return 0;
}
This program uses nested loops to display a 3×3 matrix.
row and column.| Concept | Meaning |
|---|---|
| Nested Loop | A loop placed inside another loop. |
| Outer Loop | Controls the larger repetition. |
| Inner Loop | Runs for each iteration of the outer loop. |
| Pattern | Nested loops are commonly used to create patterns. |
| Matrix | Nested loops can process rows and columns. |
| break | Terminates the nearest enclosing loop. |
| continue | Skips the current iteration of the nearest enclosing loop. |
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
// inner loop code
}
}
Question: What is a nested loop in C++?