What is a while loop in Python?

Table of Content

While loop in Python

The while loop in python is used to iterate over a block of code till the given condition is True. And as soon as the condition turns False, the next statement immediately after the loop is executed. It comes under indefinite iteration.

Indefinite iteration means that the number of times the loop is supposed to be executed is not specified in advance.

Syntax of while loop in Python:

while expression:
       statement(s)

The expression is executed in the Boolean context and if it is true, then the statement is executed. The expression is checked again when the loop is executed and will continue this process until the expression returns False.

Input:

count = 0
while (count < 5):
    count = count + 1
    print("Flexiple")

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple

The expression count<5 is True till count=4 but as soon as the value of count becomes 5, the expression is not satisfied and the loop ends.

Single statement

Similar to the syntax of the if statement, if the while block consists of a single statement then it may be declared in the same line as the while header. And if there are multiple statements, then each statement can be separated by semicolons.

Input:

count = 0
while (count < 5): count += 1; print("Flexiple")

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple

While loop with else statement in Python

Python also allows programmers to use the else statement within the while loop. Like in the for loop, if the expression becomes false in the while loop, the else block is executed.

Input:

count = 0
while (count < 5):
    count = count + 1
    print("Flexiple")

else:  
    print("The else statement is executed.")   

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
The else statement is executed.

If the break control statement is used with the while loop, then the else statement will not be executed. As soon as the condition becomes false, the break statement will ignore the else statement and end the loop and the statement after the loop will be executed.

Input:

count = 0
while (count < 5):
    count = count + 1
    print("Flexiple")
    break

else:  
    print("The else statement is executed.")   

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple

Infinite while loop in Python

If the expression in the while loop never becomes False, then the loop will never end and it becomes an infinite while loop.

Input:

count = 1
while (count > 0):
    count = count + 1
    print("Flexiple")

Output:

Flexiple
Flexiple
Flexiple...

Since the value of count will always be greater than 0, this loop will never end and will keep printing ‘Flexiple’ infinitely.

Closing Thoughts

In this tutorial, we read about the while loop in Python and various situations where the while loop can be used. We can use the [control statements](In this tutorial, we read about the while loop in Python and various situations where the while loop can be used. We can use the control statements with the while loop to change how the loop behaves. One can read about other Python concepts here.) with the while loop to change how the loop behaves. One can read about other Python concepts here.