Using loops in Python automates and repeats tasks efficiently. But sometimes a condition may arise where you want to exit the loop completely, skip the iteration, or ignore this condition. This can be done with loop control statements . Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects created in that scope are destroyed. Python supports the following control statements.
In this article the focus will be on the
The Python break statement is used to take a control out of a loop when some external condition is triggered. The break statement is placed inside the body of the loop (usually after the if condition).
< code class = "undefined spaces">
ge Out of for loop ge Out of while loop
In the above example, both loops iterate over the line “pythonengineering”, and as soon as they meet the character “e” or “s”, the if condition becomes true, and the thread is exited from the loop.