nested loops in C



In the programming context, the term “nesting” refers to enclosing a particular programming element inside the similar element. For example, nested loops, nested structures, nested conditional statements, etc. When a looping construct in C is employed inside the body of another loop, we call it as nested loop in C.

C Nested loops

The general form of a nested loop is as follows −

/*outer loop*/
{
   /*inner loop*/
   {
      . . . 
   }
   . . . 
}

The loop that encloses the other loop is called as the outer loop. The one that is enclosed, is called as the inner loop. C provides three keywords for loop formation, while, do – while and for. Nesting can be done of any types of loops. That means you can put a while loop inside a for loop, a for loop inside a do – while loop, or any other combination.

General explanation of behaviour of nested loops is that, for each iteration of the outer loop, the inner loop completes all the iterations.

Nested for Loop

Nested for loops are very common. If both the outer and inner loops are expected to perform three iterations each, the total number of iterations of the innermost statement will be 3*3=9.

Example

#include <stdio.h>
 
int main () {

   int i, j;
	

   /*outer loop*/
   for (i=1; i<=3; i++)
   {
      /*inner loop*/
      for (j=1; j<=3; j++)
      {
         printf("i: %d j: %d\n", i, j);
      }
      printf("End of inner Loop\n");
   }
   printf("End of outer loop");
 
   return 0;
}

Output

i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
End of inner Loop
i: 2 j: 1
i: 2 j: 2
i: 2 j: 3
End of inner Loop
i: 3 j: 1
i: 3 j: 2
i: 3 j: 3
End of inner Loop
End of outer loop

Here is how the above program works −

  • As the outer loop is encountered, I – the looping variable for the outer loop is initialized to 1.
  • Since the test condition (a<=3) is true, the program enters the outer loop body.
  • Program reaches the inner loop, and j – the variable that controls it, is initialized to 1.
  • Since the test condition of the inner loop (j<=3) is true, the program enters the inner loop.
  • The values of a and b are printed.
  • The program reaches the end of inner loop. Its variable j is incremented. The control jumps to step 4 until the condition (j<=3) is true.
  • As the test condition becomes false (because j becomes 4), the control comes out of the inner loop
  • The end of outer loop is encountered.
  • The variable i that controls the outer variable is incremented and the control jumps to step 3.
  • Since it is the start of the inner loop, j is again set to 1.
  • The inner loop completes its iteration and ends again.
  • Steps 4 to 8 will be repeated until the test condition of outer loop (i<=3) becomes false.
  • At the end of outer loop, I and j have become 4 and 4 respectively.

The result shows that for each value of the outer looping variable. Inner looping variable takes all the values. The total lines printed are 3*3=9.

Nesting for and while loop

As mentioned earlier, any type of loop can be placed in any other type. Let us rewrite the above example by putting a while loop inside the outer for loop.

Example

#include <stdio.h>
 
int main () {

   int i, j;
	
   /*outer for loop*/
   for (i=1; i<=3; i++)
   {
   /*inner while loop*/
      j = 1;
      while (j<=3)
      {
         printf("i: %d j: %d\n", i, j);
         j++;
      }
      printf("End of inner while Loop\n");
   }
   printf("End of outer for loop");
 
   return 0;
}

Output

i: 1 j: 1
i: 1 j: 2
i: 1 j: 3
End of inner while Loop
i: 2 j: 1
i: 2 j: 2
i: 2 j: 3
End of inner while Loop
i: 3 j: 1
i: 3 j: 2
i: 3 j: 3
End of inner while Loop
End of outer for loop

Nested loops is an important tool in the armoury of the programmer, and used in a lot of applications. Let us take a look at some examples of nested loops.

Types of Examples

Example

The following program prints tables of 1 to 10 with the help of two nested for loops.

#include <stdio.h>
 
int main () {

   int i, j;
	
   printf("program to print tables 1 to 10\n");
   /*outer loop*/
   for (i=1; i<=10; i++)
   {
   /*inner loop*/
      for (j=1; j<=10; j++)
      {
         printf(" %4d", i*j);
      }
      printf("\n");
   }
 
   return 0;
}

Output

program to print tables 1 to 10
    1    2    3    4    5    6    7    8    9   10
    2    4    6    8   10   12   14   16   18   20
    3    6    9   12   15   18   21   24   27   30
    4    8   12   16   20   24   28   32   36   40
    5   10   15   20   25   30   35   40   45   50
    6   12   18   24   30   36   42   48   54   60
    7   14   21   28   35   42   49   56   63   70
    8   16   24   32   40   48   56   64   72   80
    9   18   27   36   45   54   63   72   81   90
   10   20   30   40   50   60   70   80   90  100

Example

The code below prints increasing number of characters from a string.

#include <stdio.h>
#include <string.h>

int main () {

   int i, j, l;
   char x[] = "TutorialsPoint";
   l = strlen(x);

   /*outer loop*/
   for (i=0; i<l; i++)
   {
      /*inner loop*/
      for (j=0; j<=i; j++)
      {
         printf("%c", x[j]);
      }
      printf("\n");
   }

   return 0;
}

Output

T
Tu
Tut
Tuto
Tutor
Tutori
Tutoria
Tutorial
Tutorials
TutorialsP
TutorialsPo
TutorialsPoi
TutorialsPoin
TutorialsPoint

Example

The program below displays a two dimensional array of integers. The outer loop controls the row number, and the inner loop controls the columns.

#include <stdio.h>

int main () {

   int i, j;
   int x[4][4] =
   {
      {1,2,3,4},
      {11,22,33,44},
      {9, 99, 999, 9999},
      {10, 20, 30, 40}
   };

   /*outer loop*/
   for (i=0; i<=3; i++)
   {
   /*inner loop*/
      for (j=0; j<=3; j++)
      {
         printf("%5d", x[i][j]);
      }
      printf("\n");
   }

   return 0;
}

Output

 1    2    3    4
11   22   33   44
 9   99  999 9999
10   20   30   40
c_loops.htm
Advertisements