Understanding While Loop in Python with Examples

In this article will talk about While Loop in Python. If you didn’t see our last article Python for Loop with Examples then click the link and know more about python.

When we’re dealing with Python for loops, we already know how many times we’ll have to repeat the statements. For example, suppose I need to print out the sum of the number system’s first 100 natural numbers. In this situation, we already know that we must iterate the sentences 100 times to achieve the required result. This type of difficulty is handle by Python’s for loop.

There may be times when the number of times a series of statements must be process is unknown ahead of time and is dependent on the fulfillment of some condition. In such cases, a while loop is use.

While Loops in Python

Let’s assume we’re working on a summing problem in Python. The user’s input is taken into account. The value of the input must be an integer. The total of all the numbers submit by the user until the number 7 is enter as input is what we’re looking for. We don’t know how many numbers the user will input before entering the integer 7 in this case because we don’t know how many numbers the user will enter before entering the integer 7. As a result, we’ll utilize the while loop here. Now it’s time to write the code.

Example code of while loop in python

total = 0
num = int(input("Enter the number: "))
while num != 7:
    total = total+num
    num = int(input("Enter the number: "))
print("Sum: ", total)

Output:

>>> Enter the number: 45
Enter the number: 45
Enter the number: 1
Enter the number: 7
Sum:  91

>>> Enter the number: 0
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 7
Sum:  6

We start our program by assigning the value 0 to the variable “total.” After that, take the user’s integer input. We initially mentioned a condition in the while loop that (the number is not equal to 7). When a user enters an integer, its value is added to the previous total value. This procedure will continue until the user enters the number 7. When we reach integer 7, the condition becomes false, the while loop ends, and control passes to the print statement, where we display the entire amount.

General Format of while loop in python

while < condition >:
      < Sequence of Statements >

Flow diagram of while structures

python-while-loop

while Loop vs for Loop

In a for loop, we know how many times the statements must iterate ahead of time. But there’s nothing like it in the while loop. The while loop will run until the condition is no longer true. The while loop ends when the condition becomes false. That is seen in the previous case.

When we consider the sum of the first few natural integers. The for loop algorithms function in the following ways:

for i in range(1, n+1):
    total +=i

And here is how the while loop algorithm works:

count = 1
while count < n+1:
    total += count
    count +=1

The for loop appears to be more elegant in this case. However, in other cases, the while loop is more elegant. It is up to the developers to decide which is the most efficient.

If you want to learn more about python then visit the official documentation of python.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected]

Leave a Comment