Nested loop means a loop statement inside another loop statement. That is why nested loops are also called as "loop inside loop".
- For a nested loop, the inner loop performs all of its iterations for each iteration of the outer loop.
- If the outer loop is running from i = 1 to 5 and the inner loop is running from j = 0 to 3. Then, for each value of the outer loop variable (i), the inner loop will run from j = 0 to 3.
1. Nested For loop
A nested for loop means using one for loop inside another for loop, where the inner for loop executes fully for every single iteration of the outer for loop.
Example Program to Print Identity Matrix Using Nested for Loops
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j)
cout << "1 ";
else
cout << "0 ";
}
cout << endl;
}
return 0;
}
Output
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Explanation:
- n = 4 sets the size of the square matrix.
- The outer loop (i) controls the rows of the matrix.
- The inner loop (j) controls the columns in each row.
- if (i == j) prints 1 on the diagonal and 0 in all other positions.

2. Nested While loop
A nested while loop means using one while loop inside another while loop, where the inner while loop executes completely for every single iteration of the outer while loop.
Example Program to Print a Square Pattern Using Nested while Loops
#include <iostream>
using namespace std;
int main() {
int i = 1, j;
int n = 4;
while (i <= n) { // Outer while loop
j = 1;
while (j <= n) { // Inner while loop
cout << "* ";
j++;
}
cout << endl;
i++;
}
return 0;
}
Output
* * * * * * * * * * * * * * * *
Explanation:
- n = 4 decides how many rows and columns will be printed.
- The outer while loop (i) controls the number of rows.
- The inner while loop (j) prints stars in each row.

3. Nested Do-While loop
A nested do-while loop means using one do-while loop inside another do-while loop, where the inner loop runs completely for every single iteration of the outer loop.
Example Program to Print a Star Triangle Using Nested do-while Loops
#include <iostream>
using namespace std;
int main() {
int i = 1, j;
int n = 4;
do {
j = 1;
do {
cout << "* ";
j++;
} while (j <= i);
cout << endl;
i++;
} while (i <= n);
return 0;
}
Output
* * * * * * * * * *
Explanation:
- n = 4 sets how many rows will be printed.
- The outer do-while loop (i) controls the row number.
- The inner do-while loop (j) prints stars equal to the current row number.
- cout << endl; moves to the next line after completing each row.
Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any type of loop nested inside any type and to any level.
#include <iostream>
using namespace std;
int main() {
int i = 1;
do { // Outer do-while loop
int j = 1;
while (j <= 2) { // Inner while loop
for (int k = 1; k <= 3; k++) { // Inside for loop
cout << i << j << k << " ";
}
cout << endl;
j++;
}
i++;
} while (i <= 2);
return 0;
}
Output
111 112 113 121 122 123 211 212 213 221 222 223
Explanation:
- The outer do-while loop (i) runs two times.
- The while loop (j) runs fully for each outer loop iteration.
- The for loop (k) prints numbers three times in each while loop cycle.
Uses of Nested Loops
- Printing Patterns: Nested loops are often used to print complex patterns such as printing shapes, grids, or tables.
- Searching and Sorting: Nested loops are used in algorithms that involve searching for or sorting elements like bubble sort, insertion sort, matrix searching etc.
- Multi-Dimensional Data: Nested loops are useful when dealing with multidimensional data structures like 2D or 3D arrays, matrices, list of lists.
- Dynamic Programming: Nested loops are commonly used in dynamic Programming for solving problems like knapsack problem or longest common subsequence.