January 13, 2019

Nesting of Loops in C


  When a loop is inside the body of another loop, it is known as nesting of loops. Any type of loop can be nested inside any other type of loop.

For example a for loop may be nested inside another for loop or inside a while or do-while loop. 

Nesting of loops
Nesting of Loops

As you can see, the outer loop encloses the inner loop. The inner loop is a part of the outer loop and must start and finish within the body of outer loop.

On each iteration of outer loop, the inner loop is executed completely.



Nested for loop

A for loop inside another for loop is called nested for loop.

Example 1:

The following program uses a for loop inside another for loop to create a simple pattern.

#include <stdio.h>

int main(void)
{
    int i,j;
   
    int n=5; /*number of lines*/
  
    for(i=1; i<=n; i++) /*loop for number of lines*/
    {
        for(j=1; j<=i; j++) /*loop for number of stars on a line*/
        {
            printf("* ");
        }
      
        printf("\n"); /*for next line of pyramid*/
    }
  
    return 0;
}


Output:

*
* *
* * *
* * * *
* * * * *


Explanation:

The number of lines is 5, so we will make the outer loop iterate 5 times. The inner loop will execute once for first line(i=1), twice for second line(i=2), thrice for third line(i=3) and so on. So the inner loop will print 1 star for first line, 2 stars for second line and so on. After full execution of the inner for loop we need to write printf("\n"), so that the stars for next line are printed on a new line.

Example 2:

The following program uses for loops inside another for loop to create a simple pattern.

#include <stdio.h>

int main(void)
{
    int i,j;
    int n=5; /*number of lines*/
   
    for(i=1; i<=n; i++) /*loop for number of lines*/
    {
        for(j=1; j<=n-i; j++) /*loop for printing spaces*/
        {
            printf(" ");
        }
       
        for(j=1; j<=i; j++) /*loop for printing stars*/
        {
            printf("* ");
        }
       
       
        printf("\n"); /*for next line of pyramid*/
    }
   
    return 0;
}


Output:
    *
   * *
  * * *
 * * * *
* * * * *


Explanation:

The number of lines is 5, so we will make the outer loop iterate 5 times. The first inner loop will iterate for n-i times and second inner loop will iterate for i times. For first line(i=1) the first inner loop will print 4 spaces and the second inner loop will print 1 star, for second line(i=2) the first inner loop will print 3 spaces and the second inner loop will print 2 stars and so on. After full execution of the inner for loop we need to write printf("\n"), so that the spaces and stars for next line are printed on a new line.


Nested while loop

A while loop inside another while loop is called nested while loop.

Example 3:

The following program uses a while loop inside another while loop to create a simple pattern.

#include <stdio.h>

int main(void)
{
    int i=1,j=1;
 
    int n=5; /*number of lines*/

    while(i<=n)
    {
        while(j<=i)
        {
            printf("%d", i);
          
            j++;
        }
    
        printf("\n"); /*for next line*/
       
        i++;
        j=1;
    }

    return 0;
}


Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5


Explanation:

The number of lines is 5, so we will make the outer loop iterate 5 times. The inner loop will execute once for first line(i=1), twice for second line(i=2), thrice for third line(i=3) and so on. So the inner loop will print 1 for first line, 2 2 for second line and so on.



Nested do-while loop

A do-while loop inside another do-while loop is called nested do-while loop.

Example 4:

The following program uses a do-while loop inside another do-while loop to create a simple pattern.

#include <stdio.h>

int
main(void)
{
    int i = 1, j = 1;

    int n = 5; /*number of lines*/

    do
    {
        do
        {
            printf("* ");

            j++;
        }while(j <= n + 1 - i);

            printf("\n"); /*for next line of pyramid*/

        i++;
        j = 1;

    }while(i <= n);

    return 0;
}


Output:

* * * * *
* * * *
* * *
* *
*


Explanation:

The number of lines is 5, so we will make the outer loop iterate 5 times. Now let us see how many times the inner loop should iterate. There are 5 stars in line 1, 4 stars in line 2, 3 stars in line 3 and so on. If we talk in terms of n where n is number of lines, then there are n stars in line 1, n-1 stars in line 2, n-2 stars in line 3, n-3 stars in line 4, n-4 stars in line 5. In general we can say that there are n+1-i stars in line i. So the inner loop should iterate n+1-i times.


Different inner and outer nested loops

The power of nesting is that you are not limited to nesting only for loops, or only while loops. We can put a for loop inside a while loop or a do-while loop inside a for loop.

Example 5:

The following program prints Armstrong number.

#include <stdio.h>

int main(void)
{
    int num, n, cube, d, sum;
   
    printf("Armstrong numbers are :\n");
   
    for(num=100; num<=999; num++) /*outer loop to generate numbers*/
    {
        n=num;
        sum=0;
       
        while(n>0) /*inner loop to calculate sum of cube of digits*/
        {
            d=n%10;
            n/=10;
            cube=d*d*d;
            sum=sum+cube;
        }/*End of while loop*/
       
        if(num==sum)
            printf("%d\n",num);
    }/*End of for loop*/
   
    return 0;

}


Output:
Armstrong numbers are :

153
370
371
407


Explanation:

Armstrong number is a three digit number in which the sum of cube of all digits is equal to the number itself, for example 371 is an Armstrong number since 371 = (3*3*3) + (7*7*7) + (1*1*1) = 27+343+1.



next    No Next Tutorial
top    Index
prev    The do-while Loop in C



No comments:

Post a Comment