The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C. The most common use for break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops.
The syntax for a break statement in Python is as follows −
break
Example:
Use of break statement inside the loop
for val in "string":
if val == "i":
break
print(val)
print("The end")
Output:
s
t
r
The end
In this program, we iterate through the “string” sequence. We check if the letter is i, upon which we break from the loop. Hence, we see in our output that all the letters up till i gets printed. After that, the loop terminates.