by Dinesh Thakur Category: Control Structures

There are several situations where more than one parameter need to be varied over a range of values to obtain the desired results. For example, there may be a function having two variables, say x and y and it is desired to evaluate the function for different values of x and y. In such cases, for every value of x the values of y are varied over the range of values of y. This calls for nested while expressions as illustrated below.

int x = 0, n, m ;

while ( x < n)

{

   int y = O;

          while (y < m)

     {

        statements

         y++;

      }

         x++;

}

Program illustrates the functioning of a nested while loop.

#include <stdio.h>

main()
{
   int x =0 , Z ;
   printf("x\ty\tZ\n");
   while (x<3)
   {
      int y = 0;
      while (y <=3)
        {
           Z = x*x*x+ 2*x*y + y*y*y;
           printf("%d\t%d\t%d\n", x, y,Z);
           y++ ;
        }
           x++;
   }
}

Nested While Statements in C



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article