Lesson 33 of 60 – Nested Loops in C
55%

Nested Loops in C

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.

Note: Nested loops are commonly used for tables, patterns, matrices, grids, and problems that require repeated work inside another repetition.

1. What is a Nested 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.

2. Basic Structure of Nested Loops

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.

3. How Nested Loops Execute

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.

4. Simple Nested for Loop

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

5. Nested while Loops

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.

6. Nested do-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.

7. Printing a Rectangle Pattern

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.

8. Printing a Square Pattern

for (int i = 1; i <= 4; i++)
{
    for (int j = 1; j <= 4; j++)
    {
        printf("* ");
    }

    printf("\n");
}

Output:

* * * *
* * * *
* * * *
* * * *

9. Printing a Number Pattern

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

10. Printing Row Numbers

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

11. Increasing Star Pattern

for (int i = 1; i <= 5; i++)
{
    for (int j = 1; j <= i; j++)
    {
        printf("* ");
    }

    printf("\n");
}

Output:

*
* *
* * *
* * * *
* * * * *

12. Increasing Number Pattern

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

13. Decreasing Star Pattern

for (int i = 5; i >= 1; i--)
{
    for (int j = 1; j <= i; j++)
    {
        printf("* ");
    }

    printf("\n");
}

Output:

* * * * *
* * * *
* * *
* *
*

14. Nested Loop for Multiplication Tables

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.

15. Nested Loop with Array

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.

16. Rows and 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.

17. Nested Loop with if Statement

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.

18. break in 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.

19. continue in Nested Loops

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.

20. Nested Loop with User Input

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.

21. Multiplication Table Grid

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.

22. Nested Loop Execution Count

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("*");
    }
}

23. Common Mistake: Wrong Loop Variable

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.

24. Common Mistake: Missing Braces

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.

25. Common Mistake: Infinite Inner Loop

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.

26. Nested Loops with Different Directions

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.

27. Complete Rectangle Pattern Program

#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;
}

28. Complete Number Pattern Program

#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;
}

29. Complete Multiplication Grid Program

#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.

30. Practice Programs Using Nested Loops

Practice the following programs:

  1. Print a rectangle of stars.
  2. Print a square of stars.
  3. Print an increasing star triangle.
  4. Print a decreasing star triangle.
  5. Print an increasing number triangle.
  6. Create a multiplication table grid.
  7. Print row and column numbers.
  8. Display all elements of a 2D array.
  9. Use break inside an inner loop.
  10. Use continue inside an inner loop.

Practice changing the number of rows, columns, conditions, and update expressions to understand how nested loops work.

📌 Key Points

  • A loop inside another loop is called a nested loop.
  • The outer loop controls the larger repetition.
  • The inner loop completes its iterations for each outer iteration.
  • Nested loops can be created with for, while, and do-while.
  • They are commonly used for patterns, tables, grids, and matrices.
  • Separate loop variables make nested loops easier to understand.
  • break affects the nearest enclosing loop.
  • continue affects the current iteration of the nearest enclosing loop.
  • Every loop should have a clear condition and update.
  • Nested loops require careful attention to execution order.

🧠 Quick Quiz

Question: What is a nested loop?