I don’t understand break and continue | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I don’t understand break and continue

Can someone tell me?

8th Oct 2021, 11:12 AM
Coldkid Technology
Coldkid Technology - avatar
3 Answers
+ 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
8th Oct 2021, 11:21 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Thanks!
8th Oct 2021, 11:22 AM
Coldkid Technology
Coldkid Technology - avatar
+ 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
8th Oct 2021, 11:22 AM
Amira Rajab Alghadaywi
Amira Rajab Alghadaywi - avatar