How Can We Help?
Iteration is used when we want a code block to execute multiple times. We can’t repeat the same statement many times in our code, but we need a method that makes this function possible in 2-3 lines. For this, we are using loops.
What are the loop statements in Python? Loop is a portion of code that would repeat a specified number of times or until some condition is satisfied. Several types like (for loop, while loop, nested loop) all do the same thing but with different syntax. We can control the loop by using break, continue, and pass statements.
Python Loop Types
Python provides the following types of loops to handle the looping requirements of the program.
Loops | Description |
Python For Loop | It executes a sequence of loop statements multiple times and manages the loop variable. |
While loop | It tests the condition before executing the loop body. |
Nested Loop | It executes one or more loops inside another while or for a loop. |
Python Loop Control Statements:
We use loop control statements when we want to break out of normal execution in a loop. Loop control statements change the execution of code from its typical sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python has three keywords for loop control break, continue, and pass as below.
Loop Control Statement | Description |
Break Statement | It terminates the loop statement and transfers execution to the statement immediately following the loop. |
Continue Statement | It causes the loop to skip the remainder of its body and immediately retest its condition before reiterating. |
Pass Statement | It is used when we do not want any command or code to execute. |
Using else Statement with Loops
i) With while loop:
We can use the else statement after a while loop. The block under the else statement is executed when the while loop condition becomes false.
Syntax:
while <condition>:
Body of while loop
else :
Body of else
Example (1)
a=2
while(a>0):
print(a)
a-=1
else:
print("Reached 0")
Output:
2
1
Reached 0
In the above example, the first and second outputs (‘2’ & ‘1’) results are from the first and second iterations (“a=2” & “a=1”) of the while loop. For the third iteration, ‘a=0’, which is not satisfying the condition, and the condition becomes false, so the loop is stopped, and else statement is executed as “print(‘Reached 0′)”, so the output is “Reached 0’.
The else statement doesn’t execute if the break statement is present in the body of the while loop or if an exception is raised.
Example (2)
a=2
while(a>0):
print(a)
a-=1
break
else:
print("Reached 0")
Output:
2
In the above example, The break is available in the loop body, so the loop stops executing after the first iteration. Hence, output ‘2’ shows the result of the first iteration ‘a=2’ of the while loop.
ii) With for loop:
We can use the else statement after a for a loop. The block under the else statement is executed when the for loop is exhausted, iterating the list.
Syntax:
for <iterating_var> in <sequence>:
Body of for loop
else :
Body of else
Example (3)
for i in range(3):
print(i)
else:
print("Reached else")
0
1
2 Reached else
In the above example, the first, second, and third outputs are ‘0’,’1′, and ‘2’, which shows the result of the first, second, and third iterations ‘i=0’, ‘i=1’, and ‘i=2’of for a loop. After the third iteration, the loop is stopped, and the else statement is executed as ” print(‘Reached else’)” so the output shows as “Reached else’.
The else statement doesn’t execute if the break statement is present in the body of the for loop or if an exception is raised.
Example (4)
for i in range(3):
print(i)
break
else:
print("Reached else")
In the above example, the break is available in the loop body, so the loop stops executing after the first iteration, so output ‘0’ shows the result of the first iteration ‘i=0’ of for loop.
Conclusion
In this tutorial, you have learned what a loop is in Python. What are the different types of loops? Also, we talked briefly about loop control statements, and last, we gave different examples of how to use the else statement with loops in Python.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.
But remember to understand the concept very well, you need to practice more.
Hopefully, it was clear and concise.
Python Tutorials List:
- Python tutorials list : access all python tutorials.