We know Python loops have extra features that are not available in most other programming languages. You can put an else
block immediately after a loop's repeated interior block.
for i in range(3):
print('Loops', i)
else:
print('Else block!')
Output
Loops 0
Loops 1
Loops 2
Else block!
Surprisingly, the else block runs immediately after the loop finishes. Now if I set a break
condition then the following output is showing.
for i in range(3):
print('Loops', i)
if i == 1:
break
else:
print('Else block!')
Output
Loops 0
Loops 1
My question is, why else
is skipped. In that case, The loops aren't completed?