While loop in Python programming | Hands on coding

Syntax:

while condition:
    body of loop
  • while loop body will execute until the given condition is true.


n=1
while n<=5:
    print(n)
    n=n+1
/*
1
2
3
4
5
*/


/*
factorial of 5=1x2x3x4x5
factorial of 6=1x2x3x4x5x6
factorial of N=1x2x3x....xN
*/
n =1
fact=1
no=int(input("Enter number:"))
while n<=no:
    fact=fact*n
    n= n+1
print("Factorial of ",no," is ",fact)
/*
Enter number:6
Factorial of  6  is  716
*/
  •  
n=1
while n<=5:
    print(n)
    n=n+1
else:
    print("This is else part")
/*
1
2
3
4
5
This is else part
*/