Python While loop

python while loop :

  • The while loop is also known an precondition loop.
  • While loop allows the part of code to be executed when the given condition is true.

  • while loop is mostly used where the number of iteration is not known in advance.
Syntax : 

                  While Expression(condition):
                          Statements.......

Example :

     >>>i =1
     >>> while i<=10:
     print(i)
     i = i+1

     1
     2 
     3
     4
     5
     6
     7
     8
     9
     10
     >>> 

printing the table of the given number using While loop :

     >>> i = 1
     >>> n = int(input("Enter number :"))
     Enter number :10
     >>> while i<=10:
         print(n * i)
         i = i+1

     10 
     20
     30
     40
     50
     60
     70
     80
     90
     100
     >>> 

Exercise : write a program to find even number in range 1 to 10 using while loop ,comments your answer. 


Comments

Post a Comment