Nested While Loop in C Language Tutorial

This “Nested while loop C language Tutorial ” post providing syntax, flow chart and example programs to understand while loop clearly.


 

Nested while loop :

  • Defining while loop inside another while loop.
  • Inner while loop executes only when outer loop condition is true.
  • Loop execution terminates when outer condition has failed.

Syntax:
while(outer_cond)
{
Outer while statements ;
while(inner_cond)
{
Inner while statements ;
}
Outer while statements ;
}

 

#include<stdio.h>
main()
{
int i=0, j=0;
while(++i <= 5)
{
while(++j <= 5)
{
printf(“%d\n”,j);
}
printf(“%d\n”,j);
}
}

 

#include<stdio.h>
main()
{
int i=0, j=0;
while(i++ <= 4)
{
while(j++ <= 4)
{
}
}
printf(“final i : %d\n”,i);
printf(“final j : %d\n”,j);
}

Note : Nested loops used to process the data which is two dimensional format(rows and columns).

Outer loop always represents number of rows.
Inner loop always represents number of columns.

 

#include<stdio.h>
main()
{
int i=1;
while(i<=5)
{
int j=1;
while(j<=5)
{
printf(“*”);
j++;
}
printf(“\n”);
i++;
}
}

Share this