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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-11-18 11:00:00 +0000
Seen: 13 times
Last updated: May 28 '21
How can a DateTime Zone be changed to another DateTime Zone?
How can we direct to a randomly selected entry from a list using Nginx?
How can I load a text file in gensim?
How can CSS variables in Angular Material 15 be replaced?
How can you obtain several parameters using a single GET request?
How can double quotes be added to strings within an array?
"How can a Boolean variable be saved in SQFLite?"
How to make a Mockito mock method return null?
How can multiple sections be conditionally displayed in Hugo?