The break statement is used to terminate the execution of the current loop. Break always breaks the current execution and transfer control to outside the current block. If the block is in a loop, it exits from the loop, and if the break is in a nested loop, it exits from the innermost loop.
Example:
- list_1 = ['X', 'Y', 'Z']
- list_2 = [11, 22, 33]
- for i in list_1:
- for j in list_2:
- print(i, j)
- if i == 'Y' and j == 33:
- print('BREAK')
- break
- else:
- continue
- break
Output:
2
X 11
X 22
X 33
Y 11
Y 22
Y 33
BREAK
Python Break statement flowchart.