Loops can be nested in Python similar to nested loops in other programming languages.
Nested loop allows us to create one loop inside another loop.
It is similar to nested conditional statements like nested if statement.
Nesting of loop can be implemented on both for loop and while loop.
We can use any loop inside loop for example, for loop can have while loop in it.
Nested for loop
For loop can hold another for loop inside it .
In above situation inside for loop will finish its execution first and the control will be returned back to outside for loop. Syntax
Here is the simple syntax of nested for loop in python
for iterator in iterable:
for iterator2 in iterable2:
statement(s) of inside for loop
statement(s) of outside for loop
In this first for loop will initiate the iteration and later second for loop will start its first iteration and till second for loop complete its all iterations the control will not be given to first for loop and statements of inside forloop will be executed.
Once all iterations of inside for loop are completed then statements of outside for loop will be executed and next iteration from first for loop will begin.
1. Example of nested for loop in python
""" Author : ITVoyagers (itvoyagers.in)
Date :24th August 2020
Description : Program to show nested for loop in python """
for i in range(1,11):
for j in range(1,11):
m=i*j
print(m,end=' ')
print("Table of ",i)
Output
<strong>Output of nested for loop in python</strong>
2. Example of nested for loop in python
""" Author : ITVoyagers (itvoyagers.in)
Date :24th August 2020
Description : Program to show nested for loop in python """
for i in range(15):
for j in range(i):
print("*",end=' ')
print(" ")
Output
<strong>Output of example 2: nested for loop in python</strong>
Nested while loop
While loop can hold another while loop inside it . In above situation inside while loop will finish its execution first and the control will be returned back to outside while loop. Syntax
Here is the simple syntax of nested while loop in python
while expression:
while expression2:
statement(s) of inside while loop
statement(s) of outside while loop
In this first while loop will initiate the iteration and later second while loop will start its first iteration and till second while loop complete its all iterations the control will not be given to first while loop and statements of inside while loop will be executed. Once all iterations of inside while loop are completed than statements of outside while loop will be executed and next iteration from first while loop will begin.
It is also possible that if first condition in while loop expression is False then second while loop will never be executed.
1. Example of nested while loop in python
""" Author : ITVoyagers (itvoyagers.in)
Date :24th August 2020
Description : Program to show nested while loop in python """
p=1
while p<10:
q=1
while q<=p:
print(p, end=" ")
q+=1
p+=1
print(" ")
Output
<strong>Output of nested while loop in python</strong>
2. Example of nested while loop in python
""" Author : ITVoyagers (itvoyagers.in)
Date :24th August 2020
Description : Program to show nested while loop in python """
x=10
while x>1:
y=10
while y>=x:
print(x, end=" ")
y-=1
x-=1
print(" ")
Output
<strong>Output of example 2 : nested while loop in python</strong>
Combination of nested for loop and while loop
Just as we can have for loop with in for loop and while loop within while loop, we can also have nested loops with combination of for loop with in while loop and vice versa.
Example of nested for and while loop in python
""" Author : ITVoyagers (itvoyagers.in)
Date :24th August 2020
Description : Program to show nested for and while loop in python """
for i in range(15):
j=14
while j>=i:
print("*",end=' ')
j-=1
print(" ")
Output
<strong>Output of nested for while loop in python</strong>