This article will illustrate how to use else statements when using for in loop in Python. In most common programming languages like C/C++, Java, etc., we see that the use of else is only followed by an if statement. If an if statement is not present, then the else statement cannot be singularly used. However, in Python, users have the option of using else statements not only with if statements but also with for loops.
The else statement or else block is followed after the body of the loop. The implementation here is as follows: Under normal execution of the for loop, once the loop terminates naturally after finishing the required number of iterations, the else statement is executed along with the else block. The else block will not be implemented if the loop terminates unexpectedly with a break statement.
Using Else conditional Statement with for loop in Python
#A simple python program to show else block after a for loop being executed
for i in range (1, 4): print (i) else: #This statement will be executed because there is no break statement in the for loop print (“No Break”)
Now let us consider an example where the for loop has a break statement. In that case, the else statement written right after the for loop will not be executed.
#Program to show else block not being executed after for loop
for i in range (1, 4): print (i) break else: #This else block will not be executed because a break statement is present in the for loop print (“No break”)
Generally, if a break statement is present in the for loop, keeping an else statement after that is not helpful. It can only be of use if the loop has an if statement inside it and the if statement has a direct relation with the loop control variable. The else statement written thereafter will have some functionality if the if condition turns out to be false.
For the program below, the code is written such that the else statement will only be triggered if there is an even number in the array. If the else statement is not executed, it goes without saying that the if statement will also not be executed. If we take the array input to be [1, 9, 8], the if statement will only be executed in the third iteration. Since the if statement is triggered, the else block after the loop will not be executed. If instead the array input is [1, 3, 5], the if statement will not be triggered even once and the else statement will be triggered after the for loop.
#Sample Code to show the program described above
def contains_even_number(l): for ele in l: if else % 2 == 0: print (“List contains an even number”) break #this is the else block outside the for loop. If the if block inside the loop is not triggered even once, #then this else block will be executed else: print (“List does not contain an even number”) #main driver code print (“For list 1:”) contains_even_number ([1, 9, 8]) print (“For list 2:”) contains_even_number ([1, 3, 5])
For List 1:
list contains an even number
For List 2:
List does not contain an even number
#Sample Code to show break in loop
count = 0 while (count < 1): count = count + 1 print (count) break else: print (“No Break”)
Since there is a break statement in the for loop above, the else statement after the for loop will not be executed.