nested while loop

Nested while loop in Python programming language

Nested while loop in Python programming language

Python programming language allows the use of a while loop inside another while loop, it is known as nested while loop in Python programming language

Nested while loop in Python

Declaration

The syntax of the nested- while loop in Python as follows:

Syntax

while expression:             
         while expression:    
                 statement(s) 
statement(s)

 

Flowchat of nested while loop

Flowchart of nested while in Python

 

How works nested while loop

In the nested-while loop in Python, Two type of while statements are available:

  • Outer while loop
  • Inner while loop

Initially, Outer loop test expression is evaluated only once.

When its return true, the flow of control jumps to the inner while loop. the inner while loop executes to completion. However, when the test expression is false, the flow of control comes out of inner while loop and executes again from the outer while loop only once. This flow of control persists until test expression of the outer loop is false.

Thereafter, if test expression of the outer loop is false, the flow of control skips the execution and goes to rest.

Example

Program 1

i=1
while i<=3 :
    print(i,"Outer loop is executed only once")
    j=1
    while j<=3:
        print(j,"Inner loop is executed until to completion")
        j+=1
    i+=1;

When the above code is executed, it produces the following results:

1, 'Outer loop is executed only once')
(1, 'Inner loop is executed until to completion')
(2, 'Inner loop is executed until to completion')
(3, 'Inner loop is executed until to completion')
(2, 'Outer loop is executed only once')
(1, 'Inner loop is executed until to completion')
(2, 'Inner loop is executed until to completion')
(3, 'Inner loop is executed until to completion')
(3, 'Outer loop is executed only once')
(1, 'Inner loop is executed until to completion')
(2, 'Inner loop is executed until to completion')
(3, 'Inner loop is executed until to completion')

 

The infinite while loop in Python

while(True):
    print("Statements of outer loop is executed only once");
    //#then flow of control enter the inner loop
    while(True):
        print("Statements of inner loop is executed until to complition");
        //#the loop executed until the execution is false
        //#this is a infinite loop

When the above code is executed, it produces the following results:

Statements of outer loop is executed only once
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
Statements of inner loop is executed until to complition
.....................
.....................

 

More Example for nested while loop in Python

Program 1

Display multiplication table using nested while  in Python language

//#this is a program to display multiplication table
//#example for nested-while loop in Python
i=1
while i<=10:
  j=1
  while j<=12:
        print i*j,
        j+=1
  i+=1
  print "\n"

 

When the above code is executed, it produces the following results:

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

 

Program 2

Display multiplication table using nested while  in Python language

#this is a program to display multiplication table
#example for nested while loop in Python
i=1
while i<=5:
  j=1
  while j<=5:
        print i,"*",j,'=',i*j
        j+=1
  i+=1
  print "\n"

 

When the above code is executed, it produces the following results:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5


2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10


3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15


4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20


5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25

 

 

Nested while loop in Cpp programming language
Nested do while loop in C language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago