Nested Loops in C with Examples

Nested loop means a loop statement inside another loop statement. That is why nested loops are also called as “loop inside loop“.

Syntax for Nested For loop:

for ( initialization; condition; increment ) {

   for ( initialization; condition; increment ) {
      
      // statement of inside loop
   }

   // statement of outer loop
}

Syntax for Nested While loop:



while(condition) {

   while(condition) {
      
      // statement of inside loop
   }

   // statement of outer loop
}

Syntax for Nested Do-While loop:

do{

   do{
      
      // statement of inside loop
   }while(condition);

   // statement of outer loop
}while(condition);

Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any type of loop nested inside any type and to any level.

Syntax:

do{

   while(condition) {
      
      for ( initialization; condition; increment ) {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while(condition);

Below are some examples to demonstrate the use of Nested Loops:

Example 1: Below program uses a nested for loop to print a 2D matrix of 3×3.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program that uses nested for loop
// to print a 2D matrix
  
#include <stdio.h>
#include <stdlib.h>
  
#define ROW 3
#define COL 3
  
// Driver program
int main()
{
  
    int i, j;
  
    // Declare the matrix
    int matrix[ROW][COL] = { { 1, 2, 3 },
                             { 4, 5, 6 },
                             { 7, 8, 9 } };
    printf("Given matrix is \n");
  
    // Print the matrix using nested loops
    for (i = 0; i < ROW; i++) {
  
        for (j = 0; j < COL; j++)
            printf("%d ", matrix[i][j]);
  
        printf("\n");
    }
  
    return 0;
}

chevron_right


Output:

Given matrix is 
1 2 3 
4 5 6 
7 8 9

Example 2: Below program uses a nested for loop to print all prime factors of a number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C Program to print all prime factors
// of a number using nested loop
  
#include <math.h>
#include <stdio.h>
  
// A function to print all prime factors of a given number n
void primeFactors(int n)
{
    // Print the number of 2s that divide n
    while (n % 2 == 0) {
        printf("%d ", 2);
        n = n / 2;
    }
  
    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2) {
        // While i divides n, print i and divide n
        while (n % i == 0) {
            printf("%d ", i);
            n = n / i;
        }
    }
  
    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        printf("%d ", n);
}
  
/* Driver program to test above function */
int main()
{
    int n = 315;
    primeFactors(n);
    return 0;
}

chevron_right


Output:

3 3 5 7

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.