Loops in programming allow you to execute a block of code repeatedly based on input conditions. There are multiple loops available in python. Along with loops, there are control statements which you can use to terminate the iteration of loops. In this article, we will discuss loops and control statements briefly. We will be going through the exit for loop in python, which is our main focus.
Loops in Python Programming
There are different loop types in python for various tasks. Let’s not deep dive into it. I will just brief them to refresh things.
- While Loop: You can use the while loop to execute a statement or the group of them if the condition is TRUE. Note that, the while loop checks the condition first before execution.
- For Loop: You can use for loop in python to execute a statement multiple or fixed times. In other words, you can iterate over the things and print them based on condition.
- Nested Loops: If you can use a loop inside another loop, then it becomes a nested loop. For example, for or do-while loops in python.
Control statements in Python
Like loops, there are three control statements are there in Python to control the loop operations.
- Break statement: It will terminate the loop and transfers the execution to the statement.
- Continue statement: It will allow you to skip the remainder and resets the condition.
- Pass statement: You can use this statement for syntax purposes and if you want any other code to execute.
A simple For Loop in Python
For loop in python is used to iterate over input data. You can find a simple example of for loop in python.
#Input data a = (5,4,3,2,1) #For loop for i in a: print(i)
Here, you can see that the for loop is iterating over the input data and then prints all of them.
Exit for loop in python using Break statement
So far so good. Loops are very handy in coding and getting desired results in seconds. But it is also important to know about the loop termination or exit loop in python. In this case, we are going to use the break statement to exit for the loop in python.
#For loop with break for Val in "Hackanons": if Val == "n": break print(Val) print('The End')
Here, you can see that the for loop will iterate over the elements in the input string until it encounters ‘n’. The break statement will play a role here and terminates the loop with a print statement.
Exit for loop with Continue statement
The continue statement in python is used to skip the current for loop iteration and jumps to the remaining blocks. Although it won’t terminate the loop, it will skip the current iteration.
#For loop with continue for Val in "Hackanons": if Val == "n": continue print(Val) print('The End')
In this snippet, you can see that the continue statement skips the specified iteration and executes rest of the iterations.
- There are three major loops in python – For loop, While loop, and Nested loops.
- Three control statements are there in python – Break, continue and Pass statements.
- For loop is used to iterate over the input data.
- The break statement will exit the for a loop when the condition is TRUE.
- The continue statement will skip the current iteration and executes the rest of the iterations.
Conclusion – Exit for loop
Loops are the major aspects of programming. Using Loops you can repeatedly execute the code block for fixed times and based on the conditions. You can use the control statements like break and continue to control the loop operations as shown in this article. I hope you got better at the exit for loop in python along with relative aspects. That’s all for now. Happy python!!!
Read: Python docs