Python break Statement
Python break statement is used to change the flow of Python loop. Loops iterate over a block of code until test expression is false, However sometimes
we want to terminate the loop in the middle of iteration.
Python break statement terminates the loop containing it.
Syntax of break statement is very simple
break
Let's understand the working of
break by examples.
Python Break statement
Click to view code
You can see the above examples.
for loop is used to print the all heroes of avengers. But we wish, when
Captain appears loop will break.
So we use
break statement here.
Now, see the next example with Nested loop.
Python Break in nested loop
Click to view code
In nested loop the
break statement breaks only the inner loop, which contained the break statement.