Nested loops in C programming

In the series of learning flow control statements we learned to iterate set of statements using loop. Here in this post we learn to write one loop inside other.

In programming there exists situations where you need to iterate single or a set of repetitive statement for a number of times. For example consider an example of a train. In train there are n compartments and each compartment has m seats. Ticket checker will check ticket for each passenger for each compartment. You can write the above statement in programming as.


for each compartment in compartments
{
    for each seat in compartment
    {
        check ticket of passenger on seat
    }
}

Such situations in C programming are handled using nested loops.

C programming language supports nesting of one loop inside another. You can define any number of loop inside another loop. You can also have any number of nesting level. You can put any type of loop in another type. For example, you can write a for loop inside while loop, while inside another while etc.

Syntax of nested loop

outer_loop
{
    inner_loop
    {
        // Inner loop statement/s
    }

    // Outer loop statement/s
}

outer_loop and inner_loop is one of the valid C loop i.e. either for loop or while loop or do...while loop.

Examples of nested loop

You can write one type of loop in any other loop. In addition you can have any number of loop nested inside other. Below are some examples of nested loops.

Nested for loop

for(initialization; condition; update)
{
    // statements 
    
    for(initialization; condition; update)
    {
        // Inner loop statements
    }

    // statements
}

Nested while loop

while(condition)
{
    // statements 
    
    while(condition)
    {
        // Inner loop statements
    }

    // statements
}

Nested do...while loop

do
{
    // statements 
    
    do
    {
        // Inner loop statements
    }while(condition);

    // statements
}while(condition);

Example program to demonstrate nested loop

Let us write a C program to print multiplication table from 1 to 5.

/**
 * C program to print multiplication table from 1 to 5
 */
#include <stdio.h>

int main()
{
    /* Loop counter variable declaration */
    int i, j;

    /* Outer loop */
    for(i=1; i<=10; i++)
    {
        /* Inner loop */
        for(j=1; j<=5; j++)
        {
            printf("%d\t", (i*j));
        }

        /* Print a new line */
        printf("\n");
    }

    return 0;
}

Let us take a note on above program.

To understand the above program easily let us first focus on inner loop.

  • First the initialization part executes initializing j=1. After initialization it transfer program control to loop condition part i.e. j<=5.
  • The loop condition checks if j<=5 then transfer program control to body of loop otherwise terminated the inner loop.
  • Body of loop contains single printf("%d\t", (i*j)); statement. For each iteration it print the product of i and j.
  • Next after loop body, the loop update part receives program control. It increment the value of j with 1 and transfer program control back to loop condition.

From the above description it is clear that the inner loop executes 5 times.
For i=1 it prints the product of i and j

1       2       3       4       5

Similarly for i=2 it prints

2       4       6       8       10

Next let us now concentrate on outer loop.

  • In the outer loop first variable i is initialized with 1. Then it transfer program control to loop condition i.e. i<=10.
  • The loop condition part checks if i<=10 then transfer program control to body of loop otherwise terminate from loop.
  • Body of loop does not contain any statement rather it contain another loop i.e. the inner loop we discussed above.
  • The program control is transferred to loop update part i.e. i++ after inner loop terminates. The loop update part increment the value of i with 1 and transfer the control to loop condition.

From the above description of outer loop, it is clear that outer loop executes 10 times. For each time outer loop repeats, inner loop is executed with different value of i printing the below 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
6       12      18      24      30
7       14      21      28      35
8       16      24      32      40
9       18      27      36      45
10      20      30      40      50

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>