The while loop in Python
In the for loop chapter, we learned how to use the for loop with examples. I also explained the for loop is used when you know the number of iterations.
The while loop is used to iterate through the given code for an infinite number. You may also use for loop in that scenario, however, the while loop is designed for this.
The while loop keeps on executing the given block of code until the given condition is true.
As the condition becomes false, the execution moves outside of the while loop or Python also allows using the else statement as the condition becomes false.
The else statement executes once only (see an example in the later part).
Structure of Python while loop
Following is the general structure of using the while loop:
while condition: Write statements here to execute
An example of using while loop
- In this simple example of using the Python while loop, a variable is declared and assigned a value.
- After that, a while loop is used where a condition is set to keep on executing the while loop until the value of the variable is 10.
- In each iteration, the variable’s value is displayed by using the print function.
This is how the while loop is used:
x = 1 while x <= 10: print (x) x = x+1
Output:
An example of using break statement in while loop
See the following example, where a variable x is initiated with the value of 10.
The value in each iteration is incremented by 10.
An if statement is used where it checks the value = 30. As it is true, the break statement will execute, which will exit the loop.
This is how the break statement is used:
x = 10 while x <= 100: if x == 30: break print (x) x = x + 10
Output:
20
An example of using continue statement in while loop
If you need to exit just the current iteration of the loop rather than exiting the loop entirely, you may use the continue statement of Python.
To demonstrate the continue statement, an if statement is used to check the value of a variable x = 30.
As it is true, the continue statement will execute that will omit the current loop. So the value of 30 will not be displayed.
The code:
x = 10 while x <= 100: x = x + 10 if x == 30: continue print ("The value of variable is :", x)
Output:
You can see, the value 30 is not displayed as running the while loop.
Using else clause in the while loop example
In Python, you may use the else clause with the while and for loops. Just like in if statement, the else statement in the while loop executes as the condition becomes false.
See the following example, where a message is displayed as the condition becomes false in a while loop.
x = 5 while x <= 20: print (x) x = x+5 else: print ("While loop condition became false!")
Output:
10
15
20
While loop condition became false!
A demo of using else with break statement in while loop
If you exit the loop by using a break statement, as shown in the above example, the else clause will not execute.
See this example, where I just extended the above example with a break statement and added an else clause. See the output:
x = 10 while x <= 100: if x == 30: break print (x) x = x + 10 else: print ("The condition became false!")
Output:
You can see, it only displayed 20,30 and the loop exited as the break statement executed.