How to end the while loop in Python?

As you would know, the while loop in Python continues to execute the block of code until the condition becomes false. However, there may be certain times when you want to end the while loop early, even if the condition that is being checked is still true.

This tutorial demonstrates all the possible solutions available to end the while loop in Python.

Using the break statement.

The break statement is a control flow statement that terminates the entire loop immediately. It causes the execution of the loop to jump to the first statement after the loop. You can use the break statement to end a while loop early if a specific condition is met.

Consider the following code:

# Using the break statement.
i = 0
while i < 10:
    print(i)
    i += 1
    if i == 5:
        break
"""
Output:1
2
3
4
"""

In the above example code, the while loop will continue until the value of i becomes reaches 5, at which point, the break statement will be executed, and the loop will terminate immediately.

Using the continue statement.

The continue statement is another control flow statement that is used to skip over certain parts of a loop. It skips to the next iteration of the loop immediately. You can use the continue statement to skip over certain iterations of a while loop if a specific condition is met.

Consider the following code:

i = 0
while i < 10:
    i += 1
    if i % 2 == 0:
        continue
    print(i)
"""Output:
1
3
5
7
9
"""

In this example, the while loop will skip over any even values of i and print only the odd values. The continue statement causes the loop to skip to the next iteration immediately.

Using the else clause.

The else clause can be used to execute a block of code when the condition of the while loop becomes false. It will only execute if the while loop is not terminated by a break statement.

Consider the following code:

# Using the else clause
i = 0
while i < 10:
    print(i)
    i += 1
else:
    print("Loop completed")
"""
Output:0
1
2
3
4
5
6
7
8
9
Loop completed
"""

In this example, the while loop will continue until the value of i becomes equal to 10. At that point, the else clause will be executed, and the message “Loop completed” will be printed.

Using the return statement.

If you are working with a function that has a while loop, you can use a return statement to terminate the loop and the function. The return statement will return a value (if specified) and terminate the function immediately.

Consider the following code:

# Using the return statement.
def countdown(n):
    while n > 0:
        print(n)
        n -= 1
        if n == 5:
            return "Blast off!"
    return "Countdown complete."

print(countdown(10))
"""
Output: 10
9
8
7
6
Blast off!
"""

In this example, the countdown function will continue to execute until the value of n becomes equal to 5. At that point, the return statement will be executed, and the function will terminate immediately, returning the message “Blast off!”.

To Conclude.

There are several ways to end a while loop in Python. You can use the break statement to terminate the loop immediately, the continue statement to skip certain iterations, the else clause to execute code when the condition becomes false, or the return statement to terminate the loop and the function. By understanding these methods, you can write more efficient and flexible code with while loops in Python.

Fun Fact: Did you know that you can skip iterations in a Python loop? Learn more here.

Explore more from this category at Python Loops. Alternatively, search and view other topics at All Tutorials.