Python While Loop

Introduction to While Loops

In Python, a while loop is used to repeatedly execute a block of code as long as a certain condition is true. The while loop is similar to the if statement in that it checks a condition and executes the code inside the block if the condition is true. However, unlike the if statement, the while loop will continue to execute the code inside the block as long as the condition remains true, rather than just executing it once.

Syntax of While Loops

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

while condition:
    # code to be executed while the condition is true

The condition is an expression that is evaluated each time the loop iterates. If the condition is true, the code inside the block will be executed. Once the code has finished executing, the condition will be re-evaluated. If the condition is still true, the code inside the block will be executed again, and the process will repeat until the condition is false.

Example of a While Loop

Here is an example of a simple while loop in Python:

# Example of a while loop
counter = 0
while counter < 5:
    print(counter)
    counter += 1

In this example, we initialize a counter variable to 0 and use a while loop to repeatedly print out the value of the counter variable as long as it is less than 5. Each time the code inside the loop is executed, we increment the counter variable by 1 using the += operator.

When we run this code, we should see the numbers 0 through 4 printed out on separate lines.

Using Break and Continue in While Loops

Just like with for loops, we can use the break and continue statements to control the flow of a while loop. The break statement can be used to exit the loop immediately, while the continue statement can be used to skip over the current iteration of the loop and move on to the next one.

Here is an example of a while loop that uses break and continue:

# Example of a while loop with break and continue
counter = 0
while counter < 10:
    if counter == 5:
        break
    if counter % 2 == 0:
        counter += 1
        continue
    print(counter)
    counter += 1

In this example, we use a while loop to print out all the odd numbers between 0 and 9, except for the number 5. Within the loop, we first check if the counter variable is equal to 5. If it is, we use break to exit the loop immediately.

If the counter variable is not equal to 5, we check if it is an even number using the modulus operator (%). If it is, we use continue to skip over the current iteration of the loop and move on to the next one.

If the counter variable is an odd number and not equal to 5, we print it out using the print() function.

When we run this code, we should see the numbers 1, 3, 7, and 9 printed out on separate lines.

Code example of a Python while loop:

# Example of a Python while loop

# Initialize a counter variable
counter = 0

# Loop while the counter is less than 5
while counter < 5:

    # Print the current value of the counter
    print(counter)

    # Increment the counter by 1
    counter += 1

In this example, we use a while loop to repeatedly execute a block of code as long as a certain condition is true. The condition is specified in the while statement, which checks if the counter variable is less than 5.

Within the loop, we first print out the current value of the counter variable using the print() function. We then increment the counter variable by 1 using the += operator.

The loop will continue to execute as long as the counter variable is less than 5. Once the counter reaches 5, the condition will evaluate to false, and the loop will stop.

When we run this code, we should see the numbers 0 through 4 printed out on separate lines, since the loop iterates 5 times and increments the counter by 1 each time.

Conclusion

In this tutorial, we have covered the basics of Python while loops, including the syntax, examples, and using break and continue. While loops can be very useful for executing code repeatedly as long as a certain condition is true.

Try the Python while loop quiz.

Follow us on social media
Follow Author