Loops are an essential part of any programming language.
Loops allow programmers to set certain portions of their code to repeat through a number of loops which are referred to as iterations.
This article covers the construction and usage of While loops in Python.
While Loop In Python
A while statement iterates a block of code till the controlling expression evaluates to True. While loop favors indefinite iteration, which means we don't specify how many times the loop will run in advance.
In Python, a basic while loop looks like this:
while [a condition is True]: [do something]
The condition we provide to the while statement is the controlling expression, our loop will run until the controlling statement is no longer true.
Let's take an example;
x = 1while(x<=3): print(x) x = x+1
1 2 3
In the above example we first assigned the value 1 to the variable x, then we constructed a while loop which will run until the value of x is less than or equal to 3.
Inside the loop first, we are printing the current value of x then incrementing the value of x by 1.
In the fourth iteration, the while condition is no longer true as the value of x is now 4, therefore the while loop terminates.
A quick note - You should be really careful while constructing a while loop because if you assign a condition which will never turn into False, the loop will run forever resulting into the infinite loop.
Nevertheless, if you ever get stuck in an infinite loop in Python press
ctrl + c on Windows and
cmd + c on Mac to exit the loop.
The else Clause In While Loop
Python provides unique else clause to while loop to add statements after the loop termination.
This can be better understood by the following example,
x = 1 while(x<=3): print(x) x = x + 1 else: print("x is now greater than 3")
1 2 3 x is now greater than 3
We can see the else clause executes after the loop termination.
But wait! We can get a similar output even by just adding a print statement below, why use the else clause then?
else clause we will only execute when the loop gets terminated by itself, not by any interrupt like the break keyword.
Python comes with two inbuilt keywords to interrupt loop iteration, break and continue.
Break Keyword In While loop
The break keyword immediately terminates the while loop. Let's add a break statement to our existing code,
x = 1 while(x<=3): print(x) x = x + 1 if x == 3: break else: print("x is now greater than 3")
We added a break statement for condition x holding the value 2, which will immediately terminate the while loop from that point, thus 3 was not printed. Neither the else clause was executed because of the while loop was interrupted by the break keyword.
Continue keyword in While Loop
The continue keyword terminates the current iteration, not the entire loop when the compiler encounters the continue it goes straight back to the top of the while loop.
x = 0 while (x <= 3): x = x + 1 if x == 2: continue print(x) else: print("x is now greater than 3")
1 3 4 x is now greater than 3
2 isn't printed in the output because we introduced the continue keyword for this particular condition, when x holds the value 2 in it the Python interpreter hits the continue keyword and goes back to the top of while instead of printing it.
However, the else clause executed this time because in the end loop got terminated by itself when the controlling expression was no longer true.
Now that we know the basics of While loop we can dive into making a guessing game in Python with while loop.