C Language

Nested while in C programming language

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

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

Example

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

Example

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

 

Example – multiplication table
Output
Multiplication table
1*1=1
2*1=2
3*1=3
4*1=4
5*1=5
6*1=6
7*1=7
8*1=8
9*1=9
10*1=10
11*1=11
12*1=12
1*2=2
2*2=4
3*2=6
4*2=8
5*2=10
6*2=12
7*2=14
8*2=16
9*2=18
10*2=20
11*2=22
12*2=24
1*3=3
2*3=6
3*3=9
4*3=12
5*3=15
6*3=18
7*3=21
8*3=24
9*3=27
10*3=30
11*3=33
12*3=36
1*4=4
2*4=8
3*4=12
4*4=16
5*4=20
6*4=24
7*4=28
8*4=32
9*4=36
10*4=40
11*4=44
12*4=48
1*5=5
2*5=10
3*5=15
4*5=20
5*5=25
6*5=30
7*5=35
8*5=40
9*5=45
10*5=50
11*5=55
12*5=60
1*6=6
2*6=12
3*6=18
4*6=24
5*6=32
6*6=36
7*6=42
8*6=48
9*6=54
10*6=60
11*6=66
12*6=72
1*7=6
2*7=14
3*7=21
4*7=28
5*7=35
6*7=42
7*7=49
8*7=56
9*7=63
10*7=70
11*7=77
12*7=84
1*8=8
2*8=16
3*8=24
4*8=32
5*8=40
6*8=48
7*8=56
8*8=64
9*8=72
10*8=80
11*8=88
12*8=96
1*9=9
2*9=18
3*9=27
4*9=36
5*9=45
6*9=54
7*9=63
8*9=72
9*9=81
10*9=90
11*9=99
12*9=108
1*10=10
2*10=20
3*10=30
4*10=40
5*10=50
6*10=60
7*10=70
8*10=80
9*10=90
10*10=100
11*10=110
12*10=120
1*11=11
2*11=22
3*11=33
4*11=44
5*11=55
6*11=66
7*11=77
8*11=88
9*11=99
10*11=110
11*11=121
12*11=132
1*12=12
2*12=24
3*12=36
4*12=48
5*12=60
6*12=72
7*12=84
8*12=96
9*12=108
10*12=120
11*12=132
12*12=144
Related Links

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.