The while loop in Python is one of the most general ways to perform iteration. While loop is a control loop statement that allows code to be executed repeatedly based on a given condition.

Syntax for a While Loop in Python

The else part is not necessary to end while loop, but when used then it gets executed when while loop terminates.

Python Example using While Loop


While loop using else:


Note: If the while loop condition never results false, then the loop will run endless times i.e infinity.

Comment for any suggestion or doubts.

Leave a Reply

2 × 4 =