DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

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)
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Enter fullscreen mode Exit fullscreen mode

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.")   
Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
The else statement is executed.
Enter fullscreen mode Exit fullscreen mode

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.")   
Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple
Flexiple
Flexiple
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Output:

Flexiple
Flexiple
Flexiple...
Enter fullscreen mode Exit fullscreen mode

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 with the while loop to change how the loop behaves. One can read about other Python concepts here.

Top comments (0)