What is nested loop in C language?

When we use any loop inside another loop then this is called nested loop in C language as well as other language. We can use a number of nested loop one inside another. It will not through any error if we use any other loop inside a for loop,  while loop or do while loop. Moreover it is needed sometimes to use more than one loop inside another loop. In this article we will discuss about nested loop in C programming. Lets see the syntax of C nested loop first.

Basic syntax of nested loop in C

Though we can use any loop inside any other type of loop, for example, we can use while loop or do while loop as well as for loop inside a for loop and so on for other loop, we will give here the general syntax of nested loop in C. Now, let’s see the syntax of C nested loop.

#include <stdio.h>
int main(){
    int a, b, c;     // variable declaration
    
    outer_loop( ){
        // code to execute for outer loop

        inner_loop( ){
            //code to execute for first inner loop

            another_inner_loop( ){
                // code to execute for this inner loop

            }
        }
    }
return 0;
}

A simple program using nested loop in C

Here we will see a very basic C program using nested loop. Try to understand this program to dominate over using C nested loop.

#include <stdio.h>
int main(){
    int a, b;

    for (a = 1; a <= 3; a++){
        printf("%d. First loop.\n", a);

        for (b = 1; b <= 4; b++){
            printf("\t%d. Second loop.\n", b);
        }
    }
return 0;
}

Output of nested loop program:

1. First loop.
        1. Second loop.
        2. Second loop.
        3. Second loop.
        4. Second loop.
2. First loop.
        1. Second loop.
        2. Second loop.
        3. Second loop.
        4. Second loop.
3. First loop.
        1. Second loop.
        2. Second loop.
        3. Second loop.
        4. Second loop.

Process returned 0 (0x0)   execution time : 0.029 s
Press any key to continue.

Analysis of this program

Here we see that first loop will start from a = 1 and will be end when a > 3. That means it will iterate 3 times.

But for second loop, it will iterate 4 times for every 1 iteration of first loop. You can learn it seeing the output which we have given here. If you have any doubt still now then you should see our previous article about C for loop, C while loop and C do while loop, then come to this article.

Example programs using C nested loop

See this example of C nested loop and try to do the same program using while loop or do while loop.

#include <stdio.h>
int main(){
  int num, a, b;

  printf("Enter the number of rows here : ");
  scanf("%d", &num);

  for (a = 1; a <= num; a++){     // outer loop for printing line number

    for(b = 1; b <= a; b++){      // inner loop for printing star ( * )
      printf("*");
    }
    printf("\n");
  }

  return 0;
}

Output of above program

Enter the number of rows here : 8
*
**
***
****
*****
******
*******
********

Another pattern using C nested loop. Although we have used only for loop here but you can use any other loop to write this program. So, try this.

#include <stdio.h>
int main(){
    int number, col, rows, i = 1;     // variable declaration
    printf("Enter how many rows you want to print : ");
    scanf("%d", &number);        // taking number of rows as input

    for (rows = 1; rows <= number; rows++) {
        for (col = 1; col <= number - rows; col++)
        printf("  ");

        i = rows;

            for (col = 1; col <= rows; col++) {
            printf("%d ", i);
            i++;
            }

            i = i - 2;

            for (col = 1 ; col < rows; col++) {
            printf("%d ", i);
            i--;
            }

        printf("\n");
    }

  return 0;
}

Output of this program

Enter how many rows you want to print : 6
          1
        2 3 2
      3 4 5 4 3
    4 5 6 7 6 5 4
  5 6 7 8 9 8 7 6 5
6 7 8 9 10 11 10 9 8 7 6

See our C practice and C projects section to practice more program using C programming. This will help you to get more knowledge about practical use of C programming.

Previous pagedo while loop in C

Next pagebreak statement in C