Nested While Loops In Python With Examples

Loops are significant in python as without them we would have to repeat instructions all over again which could be time-consuming for a programmer. A while loop simply evaluates a given condition and if it is true then it executes a set of statements until that condition is true. But if the complexity of the problem increases, then it becomes necessary to insert a while loop inside another while loop. Therefore, in this tutorial, you will understand nested while loops in python.

Syntax of Python Nested While Loop

The basic syntax of nested while loop is as follows:

#outer  while loop
while condition:

         #inner while loop
         while condition:
                   block of code
block of code

Nested while loops contain two basic components:

  1. Outer While Loop
  2. Inner While Loop

The outer while loop can contain multiple inner while loops. Here the condition results in a boolean value and if it is true, only then the loop is executed. For every iteration of the outer while loop, the inner loop gets executed from the beginning and this process continues till the outer loop’s condition is true. Similarly, the inner while loop is executed only if its condition is true and the block of code gets executed.

Flowchart of nested while loops

Nested While Loops In Python

Flowchart of nested while loops

Firstly, the outer loop condition is evaluated. If it is false, then the control jumps to the end of the outer while loop. If the condition is true, then the control jumps to the inner while loop condition. Next, the inner while loop condition is evaluated. If it is false then control jumps back to the outer while loop condition. If it is true, then the block of code inside the inner while loop is executed.

A simple example of the nested while loops

In this example, we create a pattern of numbers using a nested while loop

while i<=5:
    while j<=i:
        print(j,end=" ")


1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

In the above code, the outer while loop keeps track of every new line inside the pattern, and the inner while loop displays the number according to the condition.
For example, if i=2
Outer loop:-

  1. Since 2<=5, the outer loop gets executed

Inner loop:-

  1. Initially j=1 and since 1<=2 inners for loop is executed
  2. Print j as 1 and increment j
  3. Again check the condition, 2<=2, and the inner loop is executed
  4. Print j as 2 and increment j
  5. Now since 3<=2 is False so we get out of the inner loop

A real-time example of the nested while loops

Problem statement

Consider an online quiz game where users have to write the synonyms of a given word and can have only three chances to attempt the question correctly.

while counter>0:
  answer=input("What is the synonym of 'Beautiful': ")
  while i<len(synonyms):
    print(f"You have {counter} chances")


What is the synonym of 'Beautiful': ugly
You have 2 chances
What is the synonym of 'Beautiful': bad
You have 1 chances
What is the synonym of 'Beautiful': pretty

In the above code, we use a counter variable to store the number of attempts, and the outer while loop verifies that only three attempts are given to a user. We also have a list called synonyms that stores the valid answers to the question. The inner while loop iterates through the list and checks whether the user has provided the correct answer or not. If the answer provided is correct then it simply breaks from the inner loop and exits the program as the counter variable is equal to -1. If the answer is not correct, then the counter variable is decremented and the inner while loop is executed again till the counter variable becomes zero.

This is the end of the tutorial for nested while loops in python.
You can check out nested for loops in python: Nested for Loop in Python


Leave a Reply

Your email address will not be published. Required fields are marked *