USING ELSE WITH LOOPS IN PYTHON

Python Else Loop

In many different programming languages, the else is often seen to be used with the if statements. In python as well, we use the else with if statements, but also, the else clause can be used with the looping statements, like with the for loops and the while loops.

The thing is that when the else is used with the if statements, it executes if the expression with the if evaluates to False. But the thing is that when the else clause is used with the loops (for loop and while loop), it would execute if the loop terminated through normal execution (in case of for loop), or when the condition becomes False (in case of while loop), but not when the loop termination is caused by the execution of the break keyword.

Now, to understand the things in a much better way, Let’s see an example, which demonstrates us about the else clause, to be used with the for loop. In the below example, we have a list of numbers, and we are searching for some number in that list. For that, we would be iterating through the elements, finding our required number. Once we got that number, we will break out of the loop.

The thing is that if we break out of the loop, the else with the loop won’t execute. Let’s have a look at the example now –

As you can see in the above program, we have a list of numbers, and then we have a search_key, which we are going to search in the list. Then, we are iterating through the list, searching for the number. If the number is found, we just print found! and break out of the loop. You can also see that the else clause is written with the for loop, and not the if statement.

In order to understand when the else will execute with the for loop, we need to understand in what conditions does the loop terminate. One way that we have is the normal way, that the iteration completely exhausted, and then we are out of the loop, or another way is by the execution of the break keyword. Now, the else clause with the for loop would execute if the loop executed completely, which means that the loop did not terminate due to the break keyword.

In the above program, we can see that the key that we are searching for is present in the list. So, in the loop, when we come to that particular element while iterating, the break statement would execute, and so, the else clause won’t execute in this case.

But if we were searching for some key, which is not there in the list, then the loop will completely execute, but since the key wasn’t found, the break statement won’t execute, and the loop will terminate normally, resulting the else clause to execute in this case. For example, the else clause will execute in the below case.

Now, you can see that we are trying to search for some key, which is not there in the list, so the loop will completely execute, and the break statement won’t execute, so the else will execute here. You can try to execute both the programs by yourselves.
Now, Let’s have a look at using the else clause with the while loop. Note that here as well, if the loop was terminated due to execution of break statement, the else clause with the while loop won’t execute. The else clause with the while loop would execute if the while loop was terminated because the expression evaluated to False.

Let’s have a look at a simple program, which demonstrates us about the else block, used with the while loop.

Here, you can see in the above program, we have a variable number with a value 1, and then we have a loop, which iterates while the value of number is less than 15(which means we will go till 14, and not 15). In there, we are checking if some number is divisible by 15. If we find some number divisible by 15, we will print that number divisible by 15 found, and then break out of the loop. Now, since the break statement caused the loop termination, the else with the while loop won’t execute.

But here, since we are iterating to the last value of number as 14, there is no number which is divisible by 15, and the while loop terminates with the expression evaluating to False, and not with the break statement. So, in this case, the else clause will execute.

The above example from the while loop may not make complete sense, but it demonstrates us about the execution of else clause with the while loop.

So, this was about using the else clause with the for loop and the while loop. We would use the else clause with the loops, as and when required in our Python programs. Just remember that the else clause with the loops, would execute if the loop was terminated normally (through exhaustion), and not due to execution of the break statement.