Overview of While Loop in Python
The While Loop is a type of entry-level control statement that can be used for executing a set of program code repeatedly based on a condition set for the loop. This conditional statement starts with the ‘While’ keyword and a condition next to it, followed by a code block fragment. This instructs the execution control to fetch the while statement, read the condition that is set for the loop, then keep executing the upcoming code logic until the while condition is met. This is one of the prominently used infinite loop statement in the Python programming language for web-based application development.
body of the loop
The flow of execution for the while loop is shown below. This flow chart gives us information about how the instructions are executed in a while loop.
How While Loop works in Python?
After going through the syntax and flow, we will now understand how the flow actually works. Before we enter the while loop, there is a condition check; basically, it is an expression that returns the Boolean result, which means the output of the expression will either be true or false. If and only the expression returns true, the control can enter inside the loop and execute the instructions present inside the loop. Once the instructions in the body of the loop are executed for the first time, the control again goes to the top of the loop where the entry expression or condition is present; if the expression returns true, the control executes the same instructions present in the body of the loop again, and if the expression returns false the control comes out of the loop. The while loop is also called the entry controlled loop, as the entry of the control inside the loop firmly depends on whether the expression returns true or false.
Since we have discussed how the control flows inside the while loop, let’s see some examples.
The first example is a simple one. Here the problem statement is to add the first 10 natural numbers.
This means we will add numbers starting from 1 and end at 10 since we will use a while loop, so we will provide a condition and also have to keep a variable as a counter as we have to increase the counter by one after each addition.
n=10 # upper limit
# initializes the variable for sum
sum= sum+ i
i=i+1 #increment the counter
# print the sum
print("the sum :", sum)
So, now as you can see, first of all, the expression checks whether the condition is true or false. In our case, it is true the first time as 1 is less than 10. So it goes inside and adds zero with one and stores the value in the sum variable. It then updates the counter by one. This process continues until the counter increases to 11, as at that point, the expression returns false, and the control comes out of the loop and prints the sum.
The next example is on infinite while loop, which means that it will infinitely go on executing the loop. We should always try to avoid this situation as this situation will not allow the program to terminate. It generally happens due to the expression statement, in this case, always returns true.
print("Infinite loop starts")
If we take a close look at the above example, we will see that the conditional expression n>0 is the true first time as an initial value of n is 1. The control then enters the loop and increases n by 1, and then it executes the two print statements. Upon execution, it again goes to the top of the loop and checks the expression, which is again true as the value of n is now 2. This flow repeats itself again and again as the starting expression is always true, and the value of n keeps incrementing by 1 each time. This leads us to the scenario of an infinite loop as it never terminates.
Let us now look into a different example in which we will also use conditional statements like if-else along with while loop. The below screenshot shows an infinite loop.
Let us print the even and odd numbers present between 1 and 10. This means the lower limit is 1 and the upper limit is 10. The counter will have an initial value as one since we will start at 1 and then keep on incrementing the counter by one.
print("Even Number" , i)
print("Odd Number" , i)
So, as you can see in the above code, we have the expression to check whether the number is less than 10 or not. In the first case, the expression will return true, and hence the control will flow inside the body of the loop where it contains conditional statements if-else. During the first case, the value of i is one, so the modulus will not be zero; hence if a condition will fail and the else condition statement will get executed. After that, the value of the counter will be incremented by one. Suppose a condition will be true in the second case, the statement under if a condition will be executed. The value of the counter will again be incremented by one. This will be continued until the value of I becomes 11. In that case, the while expression fails and returns false. Immediately the control goes out of the loop.
This is a guide to While Loop in Python. Here we discuss the overview of while loop in python and how while loop works in python along with its example. You may also look at the following articles to learn more-