Python Nested Loop

Here, we have learnt about the nested loop with the help flowchart and about break and continue nested loop. In python, we can write one loop inside..

Loops allows us to iterate over or repeat a particular element or sequence. 

Python nested loop

In this article, we will discuss about the Python nested loop with the help flowchart and about Python break and continue nested loop.

Python Nested loop

In python, we can write one loop inside the other loop, it is known as nesting and that loop is known as the Nested loop in python. Simply, we can say that nested loop is one loop inside the another loop. The inside and outside loop can be either for loop or while loop. And we can write more than one loop inside another loop, there is no limitation. To count the number of iterations in nested loop, we just have to multiple the iteration of outer loop with the iteration of inner loop.

Python Nested loop flowchart

Python Nested for loop

For loop is the used to iterate over or repeat sequence and other iterable elements like list, tuple, structure, etc. 

As we know that the inside and outside loop can be either for loop or while loop. So, with the help of following illustration we will discuss about Nested for loop. Here, we have taken for loop in both inside as well as outside loop.

num1 = [1, 2, 3]

num2 = [4, 5, 6]

for x in num1:

 for y in num2:

    print(x, y)

Output

1 4

1 5

1 6

2 4

2 5

2 6

3 4

3 5

3 6

While loop inside For loop

Now, in the following illustration we have taken for loop as outside loop and while loop as inside loop.

numbers = [1, 2, 3]

for number in numbers:

    count = 0

  while count < 3:

     print(number, end=' ')

  count = count + 1

    print()

Output

1 1 1 

2 2 2

3 3 3

Python Nested while loop 

While loop is used to execute a block of code or multiple items statements multiple times until the satisfaction of the condition.

So above we have learnt that the inside and outside loop can be either for loop or while loop. So now with the following illustration, we understand the Nested while loop. Here, we have taken while loop in both inside and outside loop.

i = 2

while i <= 7:

    j = 2

    while j <= 9:

        print(j, end='')

        j = j + 2

    i = i + 2

    print()

Output

2468

2468

2468

For loop inside While loop

Now, in the following illustration we have taken while loop as outside loop and for loop as inside loop.

print('Show favourite number from 1 to 1000')

n = 2

while n <= 1000:

    x_sum = 0

    for i in range(1, n):

        if n % i == 0:

            x_sum += i

    if x_sum == n:

        print('favourite number:', n)

    n += 1

Output

Show favourite number from 1 to 1000

favourite number: 6

favourite number: 28

favourite number: 496

Python Break nested loop

To stop or terminate the execution and to bring out the control from a loop, python break statement is used. So, if we have used the break statement in the inside loop, then it will stop the execution of the innermost loop.

for i in range(3):

    for j in range(3):

        if j == i:

            break

        print(i, j)

Output

1 0

2 0

2 1

Python Continue nested loop

To stop the current repetition and to immediately start the next iteration or repetition, python continue statement is used. So, if we have used the continue statement then it will stop the current repetition and to immediately start the next iteration. So, let's understand the Python continue nested loop with the help of following illustration;

for i in range(3):

    for j in range(3):

        if j == i:

            print("Answersjet")

            continue

        print(i, j)

Output

Answersjet

0 1

0 2

1 0

Answersjet

1 2

2 0

2 1

Answersjet

Conclusion 

From above we have learnt about the nested loop with the help flowchart and about break and continue nested loop. In python, we can write one loop inside the other loop, it is known as nesting and that loop is known as the Nested loop is python.