0% found this document useful (0 votes)
12 views18 pages

Nested Loop

The document explains the flow of nested loops in Python using a code example that iterates through two ranges. It details how the outer loop and inner loop interact, incrementing values of 'i' and 'j' and printing their values at each step. The authors are Vinod Kumar Verma and Sachin Bhardwaj, both PGTs in Computer Science at different KVs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

Nested Loop

The document explains the flow of nested loops in Python using a code example that iterates through two ranges. It details how the outer loop and inner loop interact, incrementing values of 'i' and 'j' and printing their values at each step. The authors are Vinod Kumar Verma and Sachin Bhardwaj, both PGTs in Computer Science at different KVs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

for more updates visit: www.python4csip.

com

for i in range(1,5):
for j in range(1,5):
print("value of i = ",i," j = ",j)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5):
for j in range(1,5):
print("value of i = ",i," j = ",j)

Let us
understand the
flow of nested
loop

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5):
for j in range(1,5):
print("value of i = ",i," j = ",j)

Outer loop
starts with
value of i = 1

i 1

j
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5):
for j in range(1,5):
print("value of i = ",i," j = ",j)

For i =1, inner


loop begins
from j = 1

i 1

j 1

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5):
print("value of i = ",i," j = ",j)

Now value of i
and j will be
printed

i 1

j 1

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5):
print("value of i = ",i," j = ",j)

As first inner must


complete for value of
i=1, therefore j will
increase

i 1 1

j 1 2

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
print("value of i = ",i," j = ",j)

Value of i and j will


be printed

i 1 1

j 1 2

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
print("value of i = ",i," j = ",j)

Now j will increase


to 3

i 1 1 1

j 1 2 3

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)

Values of i and j will


be printed

i 1 1 1

j 1 2 3

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)

Value of j will be
incremented

i 1 1 1 1

j 1 2 3 4

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)
value of i=1 j = 4

Value of i and j will


be printed

i 1 1 1 1

j 1 2 3 4

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)
value of i=1 j = 4

Now the value of j


reached to 4 which is the
last value in sequence so
loop will end and control
moves to outer loop

i 1 1 1 1

j 1 2 3 4

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)
value of i=1 j = 4

Value of i will be
incremented i.e. 2

i 1 1 1 1 2

j 1 2 3 4

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)
value of i=1 j = 4

Now inner loop again


starts from 1 so j =1

i 1 1 1 1 2

j 1 2 3 4 1

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)
value of i=1 j = 4
value of i=2 j = 1

Value of i and j will be


printed

i 1 1 1 1 2

j 1 2 3 4 1

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)
value of i=1 j = 4
value of i=2 j = 1
value of i=2 j = 2
value of i=2 j = 3
In the same way j will be value of i=2 j = 4
incremented from 2 to 4

i 1 1 1 1 2 2 2 2

j 1 2 3 4 1 2 3 4

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

for i in range(1,5): value of i=1 j = 1


for j in range(1,5): value of i=1 j = 2
value of i=1 j = 3
print("value of i = ",i," j = ",j)
value of i=1 j = 4
value of i=2 j = 1
value of i=2 j = 2
Now, after inner loop is value of i=2 j = 3
over outer loop will value of i=2 j = 4
resume from i=3, and so
on

i 1 1 1 1 2 2 2 2

j 1 2 3 4 1 2 3 4

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

value of i=1 j = 1
for i in range(1,5): value of i=1 j = 2
for j in range(1,5): value of i=1 j = 3
value of i=1 j = 4
print("value of i = ",i," j = ",j) value of i=2 j = 1
value of i=2 j = 2
value of i=2 j = 3
value of i=2 j = 4
value of i=3 j = 1
value of i=3 j = 2
Final output will be value of i=3 j = 3
value of i=3 j = 4
value of i=4 j = 1
value of i=4 j = 2
value of i=4 j = 3
value of i=4 j = 4
i 1 1 1 1 2 2 2 2 -

j 1 2 3 4 1 2 3 4 -

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

You might also like