What is Nested for loop in C Programming Language

nested for loop in c

In this article we will talk about nested for loop in C in details, this article include following topics,

  • Nested for loop in C
  • Syntax of nested for loop in C
  • Example of nested for loop in C

Nested for loop in C


A nested for loop in C is nothing but, When one or more for loop are declared within block of another for loop such condition is called nested for loop in C.

The first for loop in nested loop is treated as outer for loop in another hand other for loops which are declared within the block of another loop is treated as inner for loop.

Syntax of Nested for loop in C

for(initialisation ; condition ; increment/decrement)
{
     statement 1;
     statement 2;
for(initialisation ; condition ; increment/decrement)
    {
     statement 1;
     statement 2;
    }
}

or

for(initialisation ; condition ; increment/decrement)
{
     for(initialisation ; condition ; increment/decrement)
       {   
          statement 1;
           statement 2;
       }
}

In nested for loop ,first of all outer loop is executed if the given condition is true then command goes to inner for loop but if the given condition is not true command skip the entire loop.

Example of Nested for loop

 
#include <stdio.h>
 
int main()
{
    int i,j;    /*Here, we will use i for outer loop counter
                  and j for inner loop counter*/
    int num;
  
    for(i=1; i<=20; i++) /*to print table 1 to 20*/
    {
        /*each number has 10 multiples*/
        num= i;     /*to initialize number with i ( 1 to 20)*/
        for(j=1; j<=10; j++)
        {
             /*values will be padded with 3 spaces*/
            printf("%3d\t",(num*j));
        }
  
        printf("\n"); /*after printing table of each number*/
    }
    return 0;
}
______________________________________________________________

nested for loop example
_____________________________________________________________

Conclusion

In this article we have learned about nested for loop in detail, I hope after reading this article you have no any doubt remaining in this topic but still if you have any doubt then feel free comment below.

Post a Comment (0)
Previous Post Next Post