We generally use Else statement along with an If statement in almost all programming languages such as C, C++, Php, Java, Python etc. But what makes Python different from other programming languages is that it allows a programmer to use an else statement along with for loop and while loop as well.
This is the rarest feature that will be seen in fewer programming languages like Python. However, one should know how to use and where to apply Else Conditional Statement to make python codes worth it.
If you want to learn Python with professional instructor, then feel free to be a part of our Python course. We have team of experts who can help you to learn all about Python programming.
Let’s now understand how to use an else statement with the for/while loops in python programming language. We will first start with the for loop followed by the while loop.
Using Else Statement With For Loop
Before letting you know how to use use an else statement with for loop, let’s have a quick look into the basic concept of for loop with an example in order to ease your way of understanding.
[1] For Loop
In python, for loop is used to iterate over a sequence of elements. The sequence may be a list, tuple, string etc.
Example: marks=[24,7,5,35,17,11,8,13,44,29] for m in marks: print m Output: 24 7 5 35 17 11 8 13 44 29 |
[2] For Loop with an If Statement
In python, you can use an if conditional statement with for loop in the following way.
Example: marks=[24,7,5,35,17,11,8,13,44,29] for m in marks: if m<17: print m Output:7 5 11 8 13 |
[3] For Loop with an Else Statement
You can also use an else statement with for loop in python. This is the beauty of the python programming language.
The block of codes inside an else statement when using it with a for loop will only be executed in either of the following two conditions.
- If there is no break statement inside the for loop, or
- If the break statement is present, but it is not called.
Example 1: For Else without Break Statement marks=[24,7,5,35,17,11,8,13,44,29] for m in marks: if m<17: print m else : print “No Break” Output 1: 7 5 11 8 13 No Break |
As there is no break statement inside for loop in the above python program, therefore , block of codes inside else statement will be executed.
Example 2: For Else with Break Statement marks=[24,7,5,35,17,11,8,13,44,29] for m in marks: if m<17: print m break else : print “No Break” Output 2: 7 |
Here, there is a break statement inside for loop, therefore, block of codes inside else statement will not be executed.
Example 3: For Else with Break Statement marks=[24,7,5,35,17,11,8,13,44,29] for m in marks: if m>44: print m break else : print “No Break” Output 3: No Break |
In this example, we have used break statement inside for loop. But the else statement still gets executed. This is because, break statement is not called as there is no number greater than 44 in the list of marks.
Using Else Statement with While loop
Like we have used an else statement with a for loop, we can also use it with a while loop as well. But first, let’s have a look on how a simple while loop works.
[1] While Loop
In python, while loop is used to iterate over a block of statements as long as a given condition is true.
Example: Simple while loop sum = 0 while (sum < 3): sum = sum+1 print(sum) Output: 1 2 3 |
[2] While Loop with an Else Statement
Like in for else loop, the block of codes inside else statement when using it with a while loop will only be executed either if there is no break statement or if the break statement is present but it is not called.
Let’s have a look on the following examples.
Example 1: While else loop without break statement sum = 0 while (sum < 3): sum = sum+1 print(sum) else: print “No Break” Output 1: 1 2 3 No Break |
Clearly, as there is no break statement inside while loop, therefore, block of codes inside else statement will be executed.
Example 2: While else loop with break statement sum = 0 while (sum < 3): sum = sum+1 print(sum) break else: print “No Break” Output 2: 1 |
Here, due to the presence of break statement , the block of codes inside else statement will not be executed.
Example 3: While else loop with break statement sum = 5 while (sum < 3): sum = sum+1 print(sum) break else: print “No Break” Output 3: No Break |
Here, the presence of break statement will not affect the execution of else statement because the given conditional expression inside while loop is False.
We have shown you how to use else statement with for and while loop. Hope this post help you guys to make your concepts more clear.