C – Nested While Loop

Nested While loop: Defining a while loop inside another while loop. Loop execution terminates only when outer loop condition fails.

#include<stdio.h>
int main(){
            int x=1;
            while(x<=5)
            {
                        printf(“x : %d -> “, x);
                        int y=1;
                        while(y <= 5)
                        {
                                    printf(“y : %d \t “, y);
                                    ++y;
                        }
                        printf(“\n”);
                        ++x;
            }
            return 0;
}
Output:
     x : 1 -> y : 1   y : 2   y : 3   y : 4   y : 5
     x : 2 -> y : 1   y : 2   y : 3   y : 4   y : 5
     x : 3 -> y : 1   y : 2   y : 3   y : 4   y : 5
     x : 4 -> y : 1   y : 2   y : 3   y : 4   y : 5
     x : 5 -> y : 1   y : 2   y : 3   y : 4   y : 5
Scroll to Top