1 | initial version |
You can stop a for-loop when a specific condition is fulfilled by using the break statement. The break statement exits the loop immediately when it is executed.
Here's an example:
for i in range(10):
if i == 5:
break
print(i)
In this example, the for-loop will run from 0 to 9. However, when i
is equal to 5, the break
statement is executed and the loop is exited immediately. The program will output:
0
1
2
3
4
Note that if the condition is not met, the loop will continue until it reaches the end of its iteration.