Python else clause on loop

We have seen the if and else() statement in Python. They can help which code to execute under certain condition and another code at other conditions. Python has another else clause also known as Python else clause on loop or Python else clause for loop or Python else clause while loop.

Unlike the conditional ‘else statement’ the else clause on loop is used with loop statement like ‘for’ or ‘while’ statement. And this ‘else clause on loop’ is executed only under two conditions:
 
i)When the loop terminates due to exhaustion(or complete iteration) of the sequence in for loop.
 
ii)When the condition is not met in while loop.
 
It is not executed when the loop terminates by calling ‘break’ statement.

Link :Python break continue statement

Let us discuss the case of calling else clause on for loop and while loop seperately.


Python else clause for loop

For the condition to call else clause in for loop is, if the iteration ends due to completion of the sequence. An example is given below. The example below does not terminate by calling break.

>>> ls=[12 ,89 , 90 , 11]
>>> for item in ls:
	if item >100:
		print('greater than',item,'found')
else: #else clause
	print( 'Else clause executed' )

	
Else clause executed

In the example no ‘item’ greater than 100 is found so the ‘else clause’ is executed.

Let us take another example where the condition is fulfilled under ‘if statement’. Let us see if the else clause is executed or not.

>>> ls=[12 ,89 , 90 , 11]
>>> for item in ls:
	if item>=90:
		print('Item greater than or equal to 90 is found!')
else:
	print('Else clause executed')

	
Item greater than or equal to 90 is found!
Else clause executed

In the example we found an item>=90 but still the ‘else clause for loop’ is executed.

Let us take another example where the iteration ends due to execution of break statement.

>>> ls=[12 ,89 , 90 , 11]
>>> for item in ls:
	if item>=90:
		print('Item greater than or equal to 90 is found!')
		break
else:
	print('Else clause executed')

	
Item greater than or equal to 90 is found!

Now the iteration broke due to the execution of ‘break’ and hence the ‘else clause for loop’ is not called.

***Note: else clause for loop is always called until and unless the iteration ends due to the execution of break statement.



Python else clause while loop

The else clause while loop is executed if the condition is not met. Let us now try to examine the behaviour of the else clause in various condition. In the first example no break statment is called.

>>> ls=[12 ,99 , 10 , 1]
>>> n=0
>>> while n<len(ls):
	if ls[n]<100:
		print(ls[n],'is smaller than 100')
		n=n+1
else:
	print("Else clause executed")

	
12 is smaller than 100
99 is smaller than 100
10 is smaller than 100
1 is smaller than 100
Else clause executed

The loop ended due to completion of the ‘ls’ sequence and since the break statement is not called the ‘else clause’ is executed.

If you are wondering what len() does, it determine the number of items in a sequence, here it gives the number of items in ls sequence.

Here is another example where the break statement is called.

>>> n=0
>>> ls=[12 ,99 , 10 , 1]
>>> while n<len(ls):
	if ls[n]<100:
		print(ls[n],'is smaller than 100')
		n=n+1
		break
else:
	print('Else clause executed')

	
12 is smaller than 100

Since break is called the else clause while loop is not executed.

Conclusion: The only time else clause is not called is when break is executed, on all other cases else clause is always called.




Leave a Reply

Your email address will not be published. Required fields are marked *