Nested Loop in C

Nested Loop in C

Nested loop in C

A nested loop in a C is when the body of the loop contains another loop then the inner loop is known as a nested loop with the outer loop.

Since the body of one loop contains another loop there is an option of the loop is nested.

For example, the body of for loop contains another for loop then it is called nested for loop.

Syntax of a nested loop in C

Outer loop

{

     Inner loop  

        {

        

         }

}

 

We commonly use nested for loop syntax for nested loop i.e.

for(initialization;condition;increase/decrease)

      {

           for(initialization; condition;increase/decrease)

                        statement of inner loop;

                  {

 

                  }

                     statement of the outer loop;

         }

 

The inner loop always occupied the body of the outer loop in nested loop


nested for loop in C

One or more than one for loop contains the body of for loop then it is called nested for loop.

Commonly nested for loop is used in nested loop program because it is easy to use than another nested loop.

Syntax of nested for loop in C

for(initialization;condition;increase/decrease)

      {

           for(initialization; condition;increase/decrease)

                      statement of inner loop;

                  {

 

                  }

                      statement of the outer loop;

         }

 

Example of nested for loop in C

Program

#include<stdio.h>

#include<conio.h>

void main()

{

     int i, j;

     for(i=1;i<=5;i++) // outer loop

     {

               for(j=1;j<=i;j++) // inner loop

          {

              // statement of inner loop

              printf("*\t"); // printing *

          }

              // statement of outer loop

          printf("\n"); // moving crosser in next line

     }

     getch();

}

Output

        
pattern_in_c_using_for_loop


nested while loop in C

When one or more while loops are used inside the while loop is known as nested while loop.

Syntax of nested while loop in C

while(condition)

{

    Initialization of outer loop;

    while(condition) 

    {

          statement of the inner loop;

          inc/dec of the inner loop;

    

     }

         statement of the outer loop

         inc/dec of the outer loop;

}

 

Example of nested while loop in C

Program

#include<stdio.h>

#include<conio.h>

void main()

{

     int i=1; // outer loop initialization

     int k=1;

     while(i<=5) // outer loop

     {

          int j=1; // inner loop initialization

          while(j<=5)// inner loop

          {

               // print the value of k

              printf("%d\t", k);

              j++; // inner loop inc/dec

              k=k+1;

          }

          printf("\n");

          i++; // outer loop inc/dec

     }

     getch();

}

 Output 

pattern_in_c_using_while_loop


nested do-while loop in C

We can write an entire do-while loop structure inside another do-while loop structure. So do-while loop inside the do-while loop is called the nested do-while loop.

Syntax of nested do while loop in C

Outer loop initialization;

do

{

         Inner loop initialization;

        do

        {

          inner loop statement;

       }

         while(condition);

        outer loop statement;

}

  while(condition);

 

Example of nested while loop in C

Program

#include<stdio.h>

#include<conio.h>

void main()

{

     int i=1; //initialization for outer loop

     do

     {

          int j=1; // initialization for inner loop

          do

          {

               // inner loop statement

              printf("* ");

              //inner loop inc/dec

              j++;

     }

     while(j<=5); //inner loop condition

     //outer loop statement

     printf("\n");

     //outer loop inc/dec

     i++;

    }

    while(i<=5);// outer loop condition

    getch();

}

Output

pattern_in_c_using_do_while

Download PDF of this page

 

Post a Comment

0 Comments