Python – While Loop

while loop:

  • We use while loop when we dont know the number of iterations.
  • Examples: Reading a File, Display records in dataabse.
Syntax:
 
            while(condition):
                        statements;

Check the even numbers until user quits:

while(True):
            n = int(input(“enter num : “))
            if(n%2==0):
                        print(n,”is even”)
            else:
                        print(n,”is odd”)
 
            print(“Do you check another num(y/n) :”)
            ch = input()
            if(ch==’n’):
                        break

Display Multiplication tables until user quits:

while(True):
            n = int(input(“Enter table num : “))
            for i in range(1,11):
                        print(n,’*’,i,’=’,n*i)
 
            print(“Do you print another table(y/n) :”)
            ch = input()
            if(ch==’n’):
                        break
Scroll to Top