 Explore
Placement Prep Edit Edit

# Nested Loops In Python | FACE Prep

Published on 09 Mar 2020

One loop inside another loop is called a Nested Loop. In Python, different nested loops can be formed using for and while loops - a for-in loop inside another for-in loop, a while loop inside another while loop, a for-in loop inside a while loop, a while inside the for-in loop.

## Why do we need nested loops?

Similar to Loops in Python, nested loops execute a block of statements until the conditions return to be False. These loops are used when we need to execute a block of statements under multiple conditions. We can use these loops to traverse through multi-dimensional arrays, matrix values, tabular data.

## Flowchart to define the working of nested loops

Here is a flow diagram to demonstrate how nested loops work in Python. ## Python Nested for loop (for inside for)

One for loop inside another for loop is called a nested for loop. Nested for loops are used when the number of iterations to execute a block of statements are known beforehand.

### Syntax for nested for loop

for variable1 in sequence:
for variable2 in sequence:
statement (s)


### Example for nested for loop

Multiplication table for more than one number or range of numbers can be printed using a nested for loop as shown below.

# program to print the first three multiplication tables
for i in range(1, 4):
print("i =", i, "->", end = " ")
for j in range(1, 11):
print("{:2d}".format(i * j), end = " ")
print() # similar to new line printing after each multiplication table

﻿Output:
﻿i = 1 -> 1 2 3 4 5 6 7 8 9 10
i = 2 -> 2 4 6 8 10 12 14 16 18 20
﻿i = 3 -> 3 6 9 12 15 18 21 24 27 30


## Python Nested while loop (while inside while)

One while loop inside another while loop is called a nested while loop. Nested while loops are used when the number of iterations to execute a block of statements are not known beforehand.

### Syntax for nested while loop

while condition:
while condition:
statement (s)


### Example for nested while loop

# program using nested while loop
a = 1
while(a <= 3):
b = 1while(b <= 3):
print(b, end = ' ')
b = b + 1
print()
a = a + 1

Output:
1 2 3
1 2 3
1 2 3


## Other Nested Loops (for inside while and while inside for)

Python allows us to write a for loop inside a while loop and a while inside a for loop as shown below.

### for inside while

# program which has a for loop inside a while loop
list1 = [40, "FACEPrep"]
a = len(list1)
i = 0
while(i < a):
for j in list1:
print(j)
i = i + 1

Output:
40
FACEPrep
40
﻿FACEPrep


Here, the while loop executes until the length of the given sequence. For each iteration of the while loop, for loop prints all the elements of the given sequence.

### while inside for

# program which has a while loop inside a for loop
list1 = [40, "FACEPrep"]
a = len(list1)
for i in list1:
i = 0while(i < a):
print (list1[i]) # printing the element in the index i
i = i + 1

Output:
40
﻿FACEPrep
40
﻿FACEPrep


Since the given list contains 2 elements, the for loop executed two times and we got the output twice. While loop executes until the length of the given sequence.

## Control statements in nested loops

Python allows us to use control statements in nested loops to control the execution flow of the program.

### break in nested loop

In Python, break is used to terminate the current iteration and proceed with the next iteration. If a break statement is used inside an inner loop, then the current iteration of the inner loop gets stopped. If it is used inside an outer loop, then the current iteration of the outer loop gets stopped.

# program which has a break statement inside an inner loop
list1 = [40, "FACEPrep", 0, 30.45]
a = len(list1)
i = 0
while(i < a):
for j in list1:
if(j == 0):
break
print(j)
i = i + 1

Output:
40
﻿FACEPrep
40
﻿FACEPrep
40
﻿FACEPrep
40
﻿FACEPrep


Here, the while loop executes four times. Hence we got the output four times. Also for loop breaks when if condition becomes True.

### continue in nested loop

In Python, continue is used toskip the execution of the rest of the statements in that loop and proceed with the next iteration.If a continue statement is used inside an inner loop, then the rest of the statements in the inner loop gets skipped. If it is used inside an outer loop, then the rest of the statements in the outer loop gets skipped.

# program which a continue statement inside an inner loop
list1 = [40, "FACEPrep", 0, 30.45]
a = len(list1)
i = 0
while(i < a):
for j in list1:
if(j == 0):
continue
print(j)
i = i + 1

Output:
40
﻿FACEPrep
30.45
﻿40
﻿FACEPrep
30.45
﻿40
﻿FACEPrep
30.45
40
﻿FACEPrep
30.45


Here, the for loop skips the element zero and prints the remaining elements of the sequence until the length of the sequence.

## Nested Loop FAQs

1) What is a nested loop in python?

One loop inside another loop is called a nested loop. These nested loops are mainly used to traverse through multi-dimensional arrays, matrix values, tabular data.

2) How to break nested loops in python?

We can break nested loops in Python using the control statement ‘break’. Whenever an interpreter comes across the word ‘break’, it stops the current iteration and proceeds with the next iteration.

3) Can we use while loop inside for loop in python?

Yes, we can use a while loop inside for loop and for loop inside a while loop.  