break in python
- Break is used to escape from a loop structure. We’ve just seen a usage example with while but it also can be used under for.
- It is not easy at first to realize where using a break statement actually makes sense.
- A Python break statement ends the present loop and instructs the interpreter to starts executing the next statement after the loop.
- It can be used in both ‘for’ and ‘while’ loops. Besides leading the program to the statement after the loop, a break statement also prevents the execution of the ‘else’statement.
Syntax
break
Flow Diagram
Example
>>> a=10
>>> while True:
if a<40:
print a
else:
break
a += 10
Output
10
20
30