python Nesting of while loop and for loop in hindi by unitdiploma

Nesting of Loop

•Python programming language allows the usage of one loop inside another loop.

•A nested loop is a loop inside a loop.

•The “inner loop” will be executed one time for each iteration of the “outer loop”.

Syntax

while expression:

      while expression:

                statement(s)

      statement(s)

Example of Nested While loop

i = 2

while(i < 100):

  j = 2

  while(j <= (i/j)):

  if not(i%j):

  break

  j = j + 1

  if (j > i/j) :

  print i, ” is prime”

  i = i + 1

print “Good bye!”

Syntax for Nested for loop

for iterating_var in sequence:

          for iterating_var in sequence:

                   statements(s)

          statements(s)

Example of Nested for loop

for i in range(1,11):

          for j in range(1,11):

                  k = i*j

                  print (k, end=’ ‘)

           print()

•The print() function inner loop has end=’ ‘ which appends a space instead of default newline. Hence, the numbers will appear in one row.

•Last print() will be executed at the end of inner for loop.