Python - else in Loop
As you have learned before, the else clause is used along with the if
statement.
Python allows the else keyword to be used with the for
and while
loops too.
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.
for x in range(5):
print ("iteration no {} in for loop".format(x+1))
else:
print ("else block in loop")
print ("Out of loop")
The output of the above code confirms that the else block in the for loop will be executed while the number is in the range.
iteration no 2 in for loop
iteration no 3 in for loop
iteration no 4 in for loop
iteration no 5 in for loop
else block in loop
Out of loop
Use of else works well with the while loop too.
x=0
while x<5:
x=x+1
print ("iteration no {} in while loop".format(x))
else:
print ("else block in loop")
print ("Out of loop")
iteration no 2 in for loop
iteration no 3 in for loop
iteration no 4 in for loop
iteration no 5 in for loop
else block in loop
Out of loop