You are on page 1of 1

Else Clause

The else block appears after the body of the loop. The statements in the
else block will be executed after all iterations are completed. The program
exits the loop only after the else block is executed.  The else
clause executes after the loop completes normally. This means that
the loop did not encounter a break statement.

There are two types of Else Clauses:


(i)  else in an if statement
(ii) else in a loop statement
The else clause of an if statement is executed when the condition of
the if statement results into false.
The else clause of a loop is executed when the loop is terminating
normally i.e. when its test-condition has gone false for a while loop or
when the for loop has executed the last value in sequence.

for i in range(1, 4):


    print(i)
else:  # Executed because no break in for
    print("No Break")

Output :
1
2
3
No Break

You might also like