Python While Loop [With Examples]

As one of the fundamental concepts in any programming language, loops are a critical component for automating repetitive tasks. In Python, one such construct is the while loop. In this blog post, we’ll delve deep into the Python while loop, providing a detailed explanation and a variety of examples. Also, we will see, infinite while loop in Python.

Introduction to Python while Loop

In Python, the while loop allows a set of instructions to be executed repeatedly based on a given condition. The loop will continue to execute as long as the condition remains true. As soon as the condition is false, the loop terminates.

The syntax of a while loop in Python is as follows:

while condition:
    statement(s)
  • The condition is evaluated, and if it returns True, the loop’s body (the indented statement(s)) is executed.
  • After one iteration, the condition is evaluated again. This process repeats until the condition becomes false.
  • Once the condition returns False, the loop terminates, and control is passed to the next line of code after the while loop.

Let’s look at a basic example to see the while loop in action.

i = 0
while i < 5:
    print(i)
    i = i + 1

In this example, the loop will keep printing the value of i and incrementing it by 1, as long as i is less than 5. Once i is no longer less than 5, the loop stops. The output will be:

0
1
2
3
4

Check out the execution of the code like below:

python while loop
python while loop

Use of else with while Loop in Python

An interesting feature in Python is the ability to pair else with a while loop, something you don’t find in many other programming languages. The else block will be executed once the while condition becomes False. Here’s the syntax:

while condition:
    statement(s)
else:
    statement(s)

Let us check out an example:

i = 0
while i < 5:
    print(i)
    i = i + 1
else:
    print("Loop has ended")

In this code, once i becomes 5, the condition for the while loop becomes False, and “Loop has ended” is printed. The output will be:

0
1
2
3
4
Loop has ended

Check out the screenshot below the execution of the code:

python while loop examples

How to Control Loop Execution: break, continue, and pass

Python offers a few handy keywords to control the execution of the loop: break, continue, and pass.

The break statement

The break statement allows you to terminate the loop prematurely when a certain condition is met. Once the loop encounters the break, it immediately stops execution, and control is passed to the next line of code after the loop.

i = 0
while i < 5:
    if i == 3:
        break
    print(i)
    i = i + 1

In this example, the loop stops when i equals 3, and so the output will be:

0
1
2

The continue statement

The continue statement is used to skip the rest of the code inside the current iteration and move on to the next iteration of the loop.

i = 0
while i < 5:
    i = i + 1
    if i== 3:
continue
print(i)

In this example, the print(i) statement is skipped when i equals 3, and so the output will be:

1
2
4
5

The pass statement

The pass statement is a bit unique; it does nothing at all! It’s used when you need a statement for syntax purposes, but you don’t want any command or code to be executed. Let’s look at an example.

i = 0
while i < 5:
    if i == 3:
        pass
    print(i)
    i = i + 1

In this case, the pass statement does nothing and the print(i) line is executed every iteration, so the output will be:

Infinite while Loop in Python

An infinite loop is a loop that never ends. This usually happens when the condition in a while loop never becomes False. Here’s a simple example of infinite while loop in Python:

i = 0
while i < 5:
    print(i)
    # forgot to increment i

In this example, since we forgot to increment i, the value of i remains 0, which is always less than 5. Hence, the loop never stops and keeps printing 0.

An infinite loop isn’t always a bad thing. Sometimes they are intentionally used to keep a script running indefinitely, usually until the user decides to stop it (often by using a break statement).

Conclusion

The Python while loop is a flexible and powerful tool that allows you to control the repeated execution of a block of code. This guide explored how to use it, its syntax, its combination with else, how to control its execution with break and continue, and how to handle infinite loops.

You may also like: