Table of Contents
Nested while in C programming language
In this tutorial, we will discuss the Nested while in C programming language.
When a while loop exists inside the body of another while loop, it is known as nested while loop in java.
flow diagram
initially, the outer while evaluates the test Expression
When the condition is true, it executes the body of statements of the outer loop and the loop control move to the inner loop and statements inside the body of the inner loop is executed.This process repeats until the test expression of the inner while loop is false.
Then, when the test expression of the inner while loop is false. control exits from the inner loop and moves to outer loop again
then the loop-control evaluates the test condition of the outer while loop
The test condition of outer loop returns true, the loop control executes the body of while loop statements and move to the inner loop
Outer while loop return as false the loop control exit from the loop and goes to rest
Syntax
while(expression)
{
statements;
while(expression)
{
statements;
}
}
Here we can see a while loop inside the body of another while loop.
Program 1
Output
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
- The int variable I,j declared and initialized as zero.
- initially, the outer while evaluate the test Expression(i<10);
- when the test condition is true, loop move to inner loop and then checks the inner loop test-condition(j<10)
- Here the inner while loop will be executed 10 times and print 1 2 3 4 5 6 7 8 9 during this time.
- nest, when the test-expression of the inner loop is false, the control move to the outer loop.
- Here the inner while loop will be executed 10 time and inner loop executes 100 times to print the above output
- Finally, when the test expression of the outer loop is false, the loop control exits from the outer loop and goes to rest
Program 2
Output
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
Prpgram 3
Print a multiplication table using nested while loop