Skip to content
StudyFame

Nested Loops in C

A clock has two hands: the minute hand goes all the way around for every single step of the hour hand. A nested loop works the same way: an inner loop runs completely for each iteration of an outer loop.

Nested loops are used for tables, grids, matrices and patterns. In this lesson you will understand exactly how the two loops move, with a trace table, and write two programs.

What You Will Learn

  • What a nested loop is.
  • How the inner and outer loops move, step by step.
  • How to count the total number of iterations.
  • Rows and columns: the outer loop for rows, the inner loop for columns.
  • A multiplication grid and a star rectangle.

Nested Loops in C Explained

Exam definition: A nested loop is a loop placed inside the body of another loop. For every single iteration of the outer loop, the inner loop runs completely from start to end.

Memory trick: Outer loop = hour hand, inner loop = minute hand. For each hour, the minute hand goes all the way round. In grids: outer = rows, inner = columns.

Total iterations

If the outer loop runs m times and the inner loop runs n times each time, the inner body runs m × n times in total. Example: 3 rows × 4 columns = 12 runs.

Syntax of a Nested for Loop

for (i = 1; i <= rows; i++)          /* outer loop: rows */
{
    for (j = 1; j <= cols; j++)      /* inner loop: columns */
    {
        /* runs rows × cols times */
    }
    /* runs once per row (e.g. printf("\n")) */
}

Any loop can be nested inside any loop: for in while, while in for, and so on.

Example 1: Trace How the Loops Move

#include <stdio.h>

int main(void)
{
    for (int i = 1; i <= 2; i++)
    {
        for (int j = 1; j <= 3; j++)
        {
            printf("i=%d j=%d\n", i, j);
        }
        printf("-- end of row %d --\n", i);
    }
    return 0;
}

Output

i=1 j=1
i=1 j=2
i=1 j=3
-- end of row 1 --
i=2 j=1
i=2 j=2
i=2 j=3
-- end of row 2 --

Explanation: The Inner Loop Restarts Every Time

Outer iInner j valuesWhat happens
11, 2, 3, then 4 (stop)Inner loop runs fully; "end of row 1"
21, 2, 3, then 4 (stop)j starts again at 1; "end of row 2"
3–Outer condition false → finished
  1. The outer loop sets i = 1.
  2. The inner loop runs j = 1, 2, 3 completely while i stays 1.
  3. The line after the inner loop runs once ("end of row").
  4. The outer loop moves to i = 2, and the inner loop restarts from j = 1.
  5. Total inner runs: 2 × 3 = 6.

How Nested Loops Work

  • The inner loop's initialization (j = 1) runs again every time the outer loop enters its body. That's why j restarts.
  • i changes slowly (once per row); j changes fast (every step).
  • Use different variable names (i and j). Reusing the same name breaks the outer loop.
  • Nested loops multiply work: two loops of 1000 each mean 1,000,000 runs, so be careful with very large sizes.

Example 2: Multiplication Grid and a Star Rectangle

#include <stdio.h>

int main(void)
{
    /* 3 x 4 multiplication grid */
    for (int i = 1; i <= 3; i++)
    {
        for (int j = 1; j <= 4; j++)
            printf("%4d", i * j);
        printf("\n");
    }

    printf("\n");

    /* 3 rows x 5 columns of stars */
    for (int r = 1; r <= 3; r++)
    {
        for (int c = 1; c <= 5; c++)
            printf("* ");
        printf("\n");
    }

    return 0;
}

Output

   1   2   3   4
   2   4   6   8
   3   6   9  12

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

%4d gives every number a width of 4, so the columns line up. For a single table, see our C program to display a multiplication table.

Important Points to Remember

  • A nested loop is a loop inside another loop.
  • The inner loop runs completely for each outer iteration.
  • Total inner runs = outer count × inner count.
  • Outer = rows, inner = columns.
  • Print \n after the inner loop to start a new row.

Common Mistakes Beginners Make

MistakeWhat happensFix
Same variable for both loops (i and i)The outer loop is disturbed, wrong outputUse i and j
printf("\n") inside the inner loopEvery item on its own linePut it after the inner loop
Initializing j once before both loopsInner loop runs only for the first rowInitialize j in the inner for
Wrong bracesCode runs at the wrong levelIndent each level clearly

Nested Loops Practice Questions

  1. What is a nested loop?
  2. If the outer loop runs 5 times and the inner loop runs 4 times, how many times does the inner body run?
  3. Why does the inner loop variable start again each time?
  4. In a grid, which loop controls rows and which controls columns?
  5. Where should printf("\n") be placed to print a grid?
Show answers
  1. A loop placed inside the body of another loop.
  2. 20 times (5 × 4).
  3. Because the inner loop's initialization runs every time the outer loop body starts.
  4. The outer loop controls rows; the inner loop controls columns.
  5. After the inner loop, inside the outer loop.

Coding Exercise

Print this number grid using nested loops (each row repeats the row number):

1 1 1
2 2 2
3 3 3
Show solution
#include <stdio.h>

int main(void)
{
    for (int i = 1; i <= 3; i++)
    {
        for (int j = 1; j <= 3; j++)
            printf("%d ", i);
        printf("\n");
    }
    return 0;
}

Quick Quiz: Nested Loops in C (MCQ)

  1. How many times is "Hi" printed? for(i=0;i<3;i++) for(j=0;j<2;j++) printf("Hi");

    A) 3   B) 5   C) 6   D) 2

  2. In nested loops, which loop finishes all its iterations first?

    A) Outer   B) Inner (for each outer step)   C) Both together   D) Neither

  3. In a grid program, the outer loop usually controls:

    A) Columns   B) Rows   C) Colours   D) Nothing

  4. Which can be nested?

    A) Only for inside for   B) Only while inside while   C) Any loop inside any loop   D) None

  5. Outer runs 10 times, inner runs 10 times. Total inner runs:

    A) 10   B) 20   C) 100   D) 1000

Show answers
  1. C: 6 (3 × 2).
  2. B: the inner loop, for each outer step.
  3. B: rows.
  4. C: any loop inside any loop.
  5. C: 100.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Nested loopLoop inside another loop
Inner loopRuns fully for each outer iteration
Total runsouter × inner
GridOuter = rows, inner = columns
New lineAfter the inner loop
VariablesDifferent names (i, j)

Frequently asked questions

How does a nested loop work?
For every single iteration of the outer loop, the inner loop runs from start to end.
How many times does the inner body run?
Outer iterations × inner iterations.
Where are nested loops used?
Tables, matrices (2D arrays), patterns and comparing every pair of items.

Remember

  • Outer = hour hand, inner = minute hand.
  • Rows outside, columns inside.

Next Topic

Next lesson: break and continue in C. You will learn how to stop a loop early or skip one round.