Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Nested loops

C Programming Language Nested Loops

In C programming, a loop of one type can be used inside any another or same type of loop. That is a loop inside another loop. Such loops are known as nested loops.

Example

The while loop can be used inside another for loop

The for loop can be used inside another for loop

The do-while loop can be used inside another while loop

The for loop can be used inside another do-while loop

The for loop can be used inside another while loop, etc.

Example No 1

#include<stdio.h>
int main()
{
    int x,y;
    for(x=1;x<=3;x++)
    {
        y=1;
        while(y<=3)
        {
            printf("\n %d %d",x,y);
            y++;
        }
        printf("\n");
    }
    return 0;
}

Output

 1 1
 1 2
 1 3

 2 1
 2 2
 2 3

 3 1
 3 2
 3 3

Example No 2

#include<stdio.h>
int main()
{
    int x,y;
    for(x=1;x<=3;x++)
    {
        for(y=1;y<=3;y++)
        {
            printf("\n %d %d",x,y);
        }
        printf("\n");
    }
    return 0;
}

Output

 1 1
 1 2
 1 3

 2 1
 2 2
 2 3

 3 1
 3 2
 3 3

 

Leave a reply