Saturday 16 December 2017

Nested Loop


Nested Loop Banner


When a loop resides inside of other loop .Nested loops are useful while if there is a need of looping for each count of other turn of a loop.


Syntax : 



When we learn C language then we print many types of pattern using nested loop. as follows
  • Handling Matrix/2-D Array input/output
  • Processing Range of similar Data like array
  • Printing Series
  • Structure operation
Now I am going to print following pattern using all three types of loop.



While loop example


     int i,j;
     i=1;
     while(i<=5)
     {
          j=1;
          while(j<=i)
          {
              printf("*");
              j++;
          }
          printf("\n");
          i++;

     }

For loop example

    for(i=1;i<=5;i++)
     {
          for(j=1;j<=i;j++)
              printf("*");
          printf("\n");

     }

Do-while loop

     i=1;
     do
     {
          j=1;
          do
          {
              printf("*");
              j++;
          }while(j<=i);
          printf("\n");

     }while(j<=5);


No comments:

Post a Comment