Python Breaking a While Loop With Continue
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 will keep 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:
See online demo and code
This is how the while loop is used:
x = 1 while x <= 10 : print ( x ) x = x+1 |
An example of using break statement in while loop
The break statement is used to exit the current loop (while and for loops). The scope of break statement is only the current 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 that will exit the loop:
See online demo and code
This is how the break statement is used:
x = 10 while x <= 100 : if x == 30 : break print ( x ) x = x + 10 |
An example of using continue statement in while loop
If you need to exit just the current iteration of the loop rather 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. See the demo online:
See online demo and code
The code:
x = 10 while x <= 100 : x = x + 10 if x == 30 : continue print ( "The value of variable is :" , x ) |
You can see, the value 30 is not displayed as running the while loop.
Using else clause in 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 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.
See online demo and code
This is how you may use the else statement in while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | x = 5 while x <= 20 : print ( x ) x = x+5 else : print ( "While loop condition became false!" ) |
Note that, the else clause will not execute if you exit the loop by using the break statement. See the following example.
A demo of using else with break statement in while loop
If you exit the loop by using a break statement, as shown in above example, the else clause will not execute.
See this example, where I just extended the above example with break statement and added an else clause. See the output:
See online demo and code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | x = 10 while x <= 100 : if x == 30 : break print ( x ) x = x + 10 else : print ( "The condition became false!" ) |
You can see, it only displayed 20,30 and the loop exited as the break statement executed.
Source: https://www.jquery-az.com/python-while-loop-5-examples-with-break-continue-and-else-clause/
0 Response to "Python Breaking a While Loop With Continue"
Post a Comment