+ 1
I don’t understand break and continue
Can someone tell me?
3 Antworten
+ 6
You have defined a loop which will create an action each iteration.
Example:
for i in range(5):
print(i) #0 1 2 3 4
But you might not want all of the outputs of the loop, so you want to skip an iteration action
Example:
for i in range(5):
if i%2:
continue
else:
print(i) #0 2 4
Or you might want the iteration to stop when you reach a specific outcome
Example:
for i in range(5):
if i ==2:
break
else:
print(i) #0 1
You can combine these conditions to suit your requirements
+ 3
Thanks!
+ 2
use them in loops
break stops the loop
for example if your loop printing numbers from 1 to 5 and you did this
if (i==4):
break
the output will be 123
continue ignore just one step and continue the rest of the loop
if (i==4)
continue
the output will be 1235