Repeating a block of code based on condition – while loop in Python

In a program, code is executed sequentially (line by line) but what if you want to do something repeatedly. For this functionality, loops are provided by different programming languages.

There are two types of loops in Python, while loop and for loop. In today’s post, we will be focusing on while loop.

A while loop executes a block of code repeatedly until a condition is met. The general syntax is as follows:

while ([condition]):
  line or block of code

The line or block of code will continue to execute until the condition evaluates to true and when it becomes false, the next line immediately following the loop is exected. If the condition evaluates to false once, the program control will immediately pass to the next line.

Following is a simple example of while loop:

age = 1
while (age <= 10):
    print(“I am” + age + “years old”)
    age = age + 1

This will produce the following output:

I am 1 years old
I am 2 years old
I am 3 years old
I am 4 years old
I am 5 years old
I am 6 years old
I am 7 years old
I am 8 years old
I am 9 years old
I am 10 years old

The block consisting of the print and increment statements is executed repeatedly until age is no longer less than or equal to 10. With each iteration, the current value of the variable age is displayed and then increased by 1.

Using else with a while loop

If you want your program to execute a different statement or block of code if the condition of while loop is false, you can use else keyword for it.

Let’s modify the above code to understand this.

age = 1
while (age <= 10):
    print(“I am” + age + “years old”)
    age = age + 1
else:
    print(“I am more than 10 years old”)

When the above code is executed, it produces the following result:

I am 1 years old
I am 2 years old
I am 3 years old
I am 4 years old
I am 5 years old
I am 6 years old
I am 7 years old
I am 8 years old
I am 9 years old
I am 10 years old
I am more than 10 years old

Infinite loops

One interesting thing when it comes to loops is finiteness. A loop can become an infinite loop if you forget to add a stopping criterion to a loop or if your condition always evaluates to true.

An infinite loop, however, might be useful in client/server programming where the server needs to run continuously so that client programs can communicate with it.

Following is an example of an infinite loop:

number = 1
while (number == 1):
    print(“The number is ” + number)

This will keep on printing the same statement until you press Ctrl + C to kill the program.

The number is 1
The number is 1
The number is 1
The number is 1
The number is 1
The number is 1
The number is 1Traceback (most recent call last):
   File "loop.py", line 3, in <module>
      print(“The number is ” + number)
KeyboardInterrupt

These are all the basics of while loops which will help you run a functionality while a certain condition evaluates to true. Ask away in the comments in case of any queries.

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest