Python while Loops

Python while Loops

This is a detailed tutorial on Python While Loops. Learn how you can use a while loop to do different kinds of iterations in your python program.

Use of Python While Loop

The While loop is used to do iteration according to a condition. The loop keeps on running as long as the condition specified for it remains true. As soon as the condition becomes False, the control comes out of the while loop and the iteration procedures stop at that point.

I’ve already explained how you can perform different kinds of iterations in your python program using Python For Loops. So, make sure you have a look at that article before reading this article further for better understanding.

while Syntax

The syntax of the while loop is very simple. The code to iterate is written inside a block that is created by writing the keyword while followed by the condition. You can create a condition by using different Python Operators such as the Comparison Operators.

while condition:
    #The code to iterate

The code will keep on iterating as long as the condition is True.

Note. The boolean values True or False can also be written directly instead of the conditions. In case, you’re writing True instead of the condition, it will act as an infinite loop, so in such cases, you might have to make use of the control statements like break and continue to have control over the while loop. We’ll learn about them in the examples section.

Examples

To better understand the usage of a while loop, have a look at the following examples.

Simple while Loop

Example 1. This illustrates the simplest usage of a while loop.

i = 5;
while i < 10:
    print("Value of i is " + str(i))
    i = i + 1

The while loop in the above code will keep on the iteration as long as the variable i holds a value that is less than 10. As you can see initially we’ve defined the value of the variable i to be 5. Then inside the while block code, we’re first printing the value of i to the console screen and then simply incrementing its value by 1. So as soon as the value of the variable i is incremented up to 10, the control comes out of the while loop, and the program stops.

Note. If the value of the variable i might have not been incremented, the loop will keep on running and it will be an infinite loop.

break statement in while loop

The break statement is used to stop the loop and take the control out of the loop.

Example 2. This example illustrates the use of while loop and the break statement.

#While Loop with break statement
i = 5;
while True:
    print("Value of i is " + str(i))
    i = i + 1
    if i >= 10:
        break

Observe the code carefully. This code will also give the same output as the example already demonstrated above. But there are two differences in the code. Instead of writing a condition with the while keyword, I’ve used the boolean value True directly, so it behaves as an infinite loop now and the loop has to halted from the code inside the while block using the break statement and that’s what I did in the above code.

I’ve used an If Statement inside the while loop block and have written the break keyword inside the condition block. Now, as soon as the value of i will becomes equal to 10, the loop will break automatically and the program will be halted.

continue statement usage with a while loop

The continue statement is the opposite of the break statement. It is used to continue the iteration. Whenever inside a loop, the continue statement is encountered, the current iteration is stopped at that point and the control is shifted to the next iteration of the same loop.

Example 3. This illustrates the use of the continue statement in a while loop. It is basically a program to print all the numbers that are not divisible by 3 up to 15.

#While Loop with continue statement
i = 0;
while i < 15:
    i = i + 1
    if i % 3 == 0:
        continue
    print(i)

In the above code, the while loop will keep on the iteration as long as the value of the variable i is less than 15. Inside the block code, we’re first incrementing the value of the variable i, then if the value of the variable i is divisible by 3, we’re using the continue statement. Now as you can see in the output, it is simply shifting the program to the next iteration wherever it encounters the continue statement.

You can see in the output, the print statement is executed only when the value of i is not divisible by 3. Therefore, it is skipping the execution of the current block code after the occurrence with the continue statement.

else statement with the while loop

You can also use the else statement along with the while loop. The else statement written along with a while block code is executed immediately after the condition becomes False.

#While Loop with else statement
i = 5;
while i < 10:
    print("Value of i is " + str(i))
    i = i + 1
else:
    print("The value of i becomes 10.")

Leave a Reply

Your email address will not be published. Required fields are marked *