How to end For loop in Python

In this article, we will see how to end for
loop in Python.
In Python, break
and continue
statements can alter the flow of a normal loop. This break
statement can also be used for ending for
loop. Let’s see how this is done.
break
The break
statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
# use of break in # ending for loop for i in range(1, 10): if i%5 == 0: print("end") break print(i)
Output:
1 2 3 4 end
In this program, we iterate from 1 to 10 (excluding 10). We check if the value is divisible by 5, upon which we print “end” and break from the loop. Hence, we see in our output that all the value up till 5 gets printed. After that, the loop terminates.
Subscribe
Login
Please login to comment
0 Discussion