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++; } }