for i in range(1,4): for j in range(1,4): print( 'Running i =', i, 'j =', j ) if i==2 and j==1 : print( 'Breaks inner loop at i=2 j=1' ) breakIn this code snippet
if
is still outside the second loop and also break
is out of the if block
, so the break
statement is executed always when the execution reaches that line.So when you start the code and enter the first loop
i=1
, then you enter the second loop and execute it entirely (i.e. j takes values 1, 2 3) and print the "Running..." string. Then you reach the if statement. When you reach it i=1 and j=3. so the first condition i==2
is evaluated to False
AND the second condition j==1
is evaluated to False
and thus the whole if statement is evaluated to False
(because you have AND i.e. both conditions must be True to evaluate the entire statement as True). Thus "Breaks..." is not printed. Finally you reach the break
statement and exit the loop in which it is (i.e. that is the outer loop, the one with i).