The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
Using While loop within while loops is said to be nested while loop. In nested while loop one or more statements are included in the body of the loop. In nested while loop, the number of iterations will be equal to the number of iterations in the outer loop multiplies by the number of iterations in the inner loop which is most same as nested for loop.
Let us see how neat a syntax of nested while loop is
while (test condition) { while (test condition) { .// inner while loop } .// outer while loop }
The following example program will clearly explain the concept of nested while loop
#include <stdio.h> int main() { int a = 1, b = 1; while(a <= 5) { b = 1; while(b <= 5) { printf("%d ", b); b++; } printf("\n"); a++; } return 0; }
The variable a and b is initialized to 1 at the time of initializing variable. The condition a <= 5 is the outer test condition and b <= 5 is the inner test condition which is tested for every iteration. Here, the inner while loop will be executed for 5 times for every time the test condition in the outer loop is true.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019