Loop statements in python



Loop is a specific block executed continuously until the given condition is false.








Types



  1. while
  2. for, for each







1. While loop statement



"while" loop is one of the looping statement.Using while loop we can iterate the value for the given number of times.




Syntax:




while(condition):

    #looping




Example-1
n = 0

while(n<5):

    print(n)

    n += 1


Output



0
1
2
3
4







Example-2
n = "mycrazycoding"

i = 0

while(i<5):

    print(n)

    i += 1


Output



mycrazycoding
mycrazycoding
mycrazycoding
mycrazycoding
mycrazycoding







2. for loop statement



"for" loop is one of the looping statement.Using for loop we can iterate the value for the given number of times.




Syntax:




for variable in range(starting number,ending number):

     #looping



Example-1
for i in range(0,5):

    print(i)


Output



0
1
2
3
4