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.
When one loop is written inside another loop, it is called a nested loop.
for (...)
{
for (...)
{
// inner loop
}
}
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++)
{
printf("%d %d\n", i, j);
}
}
Here, i controls the outer loop and j controls the
inner loop.
Suppose the outer loop runs 3 times and the inner loop runs 3 times. The inner loop completes all 3 iterations for each outer iteration.
Outer 1 → Inner 1, 2, 3
Outer 2 → Inner 1, 2, 3
Outer 3 → Inner 1, 2, 3
Therefore, the inner loop executes a total of 9 times.
for (int i = 1; i <= 2; i++)
{
for (int j = 1; j <= 3; j++)
{
printf("i=%d j=%d\n", i, j);
}
}
Output:
i=1 j=1
i=1 j=2
i=1 j=3
i=2 j=1
i=2 j=2
i=2 j=3
int i = 1;
while (i <= 2)
{
int j = 1;
while (j <= 3)
{
printf("i=%d j=%d\n", i, j);
j++;
}
i++;
}
Nested loops can also be created using while loops.
int i = 1;
do
{
int j = 1;
do
{
printf("%d %d\n", i, j);
j++;
}
while (j <= 3);
i++;
}
while (i <= 2);
A do-while loop can also contain another loop.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
printf("* ");
}
printf("\n");
}
Output:
* * * * *
* * * * *
* * * * *
The outer loop controls rows and the inner loop controls columns.
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
printf("* ");
}
printf("\n");
}
Output:
* * * *
* * * *
* * * *
* * * *
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
printf("%d ", j);
}
printf("\n");
}
Output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
printf("%d ", i);
}
printf("\n");
}
Output:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
Output:
*
* *
* * *
* * * *
* * * * *
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
for (int i = 5; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
Output:
* * * * *
* * * *
* * *
* *
*
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 10; j++)
{
printf("%d x %d = %d\n",
i, j, i * j);
}
printf("\n");
}
The outer loop selects the table and the inner loop generates its multiplication values.
int matrix[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
The outer loop processes rows and the inner loop processes columns.
Nested loops are commonly used to work with row-and-column structures.
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 4; col++)
{
printf("(%d,%d) ", row, col);
}
printf("\n");
}
Here, row controls the outer loop and col controls
the inner loop.
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
if (i == j)
{
printf("* ");
}
else
{
printf("- ");
}
}
printf("\n");
}
The if statement can be used to create different patterns
inside nested loops.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j == 3)
{
break;
}
printf("%d ", j);
}
printf("\n");
}
The break exits only the nearest inner loop.
The outer loop continues.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j == 3)
{
continue;
}
printf("%d ", j);
}
printf("\n");
}
The continue skips only the current iteration of the inner
loop.
int rows, columns;
printf("Enter rows: ");
scanf("%d", &rows);
printf("Enter columns: ");
scanf("%d", &columns);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= columns; j++)
{
printf("* ");
}
printf("\n");
}
The user can decide the number of rows and columns.
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
printf("%4d", i * j);
}
printf("\n");
}
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 width 4 helps align the values.
If an outer loop executes 3 times and an inner loop executes 4 times for each outer iteration, the inner statement executes:
3 × 4 = 12 times
For example:
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 4; j++)
{
printf("*");
}
}
Each loop should normally have its own control variable.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
printf("%d %d\n", i, j);
}
}
Using separate variables such as i and j makes the
nested structure easier to understand.
Braces help clearly define which statements belong to each loop.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
printf("* ");
}
printf("\n");
}
Proper indentation and braces make nested loops easier to read and debug.
The inner loop must also have a condition and an update that allow it to finish.
for (int i = 1; i <= 3; i++)
{
int j = 1;
while (j <= 3)
{
printf("%d ", j);
j++;
}
}
Without j++, the inner loop could continue indefinitely.
for (int i = 1; i <= 3; i++)
{
for (int j = 5; j >= 1; j--)
{
printf("%d ", j);
}
printf("\n");
}
Output:
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
The outer loop counts upward while the inner loop counts downward.
#include <stdio.h>
int main()
{
int rows, columns;
printf("Enter rows: ");
scanf("%d", &rows);
printf("Enter columns: ");
scanf("%d", &columns);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= columns; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int n;
printf("Enter size of multiplication table: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
printf("%4d", i * j);
}
printf("\n");
}
return 0;
}
This program creates a multiplication grid based on the size entered by the user.
Practice the following programs:
break inside an inner loop.continue inside an inner loop.Practice changing the number of rows, columns, conditions, and update expressions to understand how nested loops work.
for, while, and do-while.break affects the nearest enclosing loop.continue affects the current iteration of the nearest enclosing loop.Question: What is a nested loop?