0 votes
in Python by
What is the use of break statement?

1 Answer

0 votes
by

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:

  1. list_1 = ['X''Y''Z']  
  2. list_2 = [112233]  
  3. for i in list_1:  
  4.     for j in list_2:  
  5.         print(i, j)  
  6.         if i == 'Y' and j == 33:  
  7.             print('BREAK')  
  8.             break  
  9.     else:  
  10.         continue  
  11.     break  

Output:

2
X 11
X 22
X 33
Y 11
Y 22
Y 33
BREAK


Python Interview Questions

Python Break statement flowchart.

...