+ 3

Operators "break" and "continue"

When we study the Python, we learn also new operators, such as "break" and "continue". They help proceed on the next loop iteration with the "continue" or stop loop iteration with the "break" instruction. But in several sources I read about the undesirability of using these operators. Because the use of these operators can harm the program. How are things really going? Should we use "break" and "continue" in the program or not?

17th Sep 2019, 6:35 PM
Слава Сибиряк
13 Respostas
+ 5
There are certain cases where using break and continue are neccessary. However, they should be avoided as they can alter the program flow in unpredictable ways if you are not careful...leading to particularly nasty bugs. Side-effects can also be produced In my opinion, these statement are like the goto-statement (the boogeyman of programming), albeit slightly less bad. But still bad nonetheless
17th Sep 2019, 6:44 PM
Trigger
Trigger - avatar
+ 2
Break and continue harmful? I hear that for the first time! I'd like to see examples of real-life scenarios where break and continue themselves are doing damage.
17th Sep 2019, 7:00 PM
HonFu
HonFu - avatar
+ 2
Well, but that's true for every single part of the language. Whenever you loop over any sort of container, there can be cases where you want to exclude certain items. So you continue. Or there's a condition where the procedure should abort. So you break. Calling 'break' risky sounds to me just like calling 'return' risky.
17th Sep 2019, 7:02 PM
HonFu
HonFu - avatar
0
HonFu Not harmful...dangerous/risky Like a chainsaw. Will screw you up if you don't know how to use it correctly
17th Sep 2019, 7:01 PM
Trigger
Trigger - avatar
0
Meh, I think the rest of Python is about as dangerous as a hammer. You might knock your thumb, or bonk someone else over the head A chainsaw (continue/break) is more powerful (greater control over how loops work), but more risky (don't wanna lose an arm) You are entitled to your own opinion, however. You earned it😁
17th Sep 2019, 7:04 PM
Trigger
Trigger - avatar
0
Yeah, I see how you like your chainsaw analogy, but the real-life scenario somehow is still missing.
17th Sep 2019, 7:05 PM
HonFu
HonFu - avatar
0
Mmm your right, HonFu. I think the best analogy is this Say you made a class method that does something and returns an array However, by some miraculous screw-up using a break statement, the array is now missing some values, or the operation you wanted to do on it wasnt completed Later on, when someone uses your code and accidentally triggers your break-statement, the are suddenly left with an almost untrackable error (as it shows up where the code is looking for values that are missing; ArrayOutOfBoundsError) Think this is an okay scenario, HonFu?
17th Sep 2019, 7:11 PM
Trigger
Trigger - avatar
0
It sounds like a spook story to me that you could tell about *any other* basic element of the language. You could try to replace something in a string, but mess up the indexing - bam, data destroyed. You could get some condition in an if-else branch just a little bit wrong. Again, the most terrible stuff could happen. Open a file, save to it - so many things can go wrong. Programming is dangerous, that's why we practice it and find ways to make it easier for us. I find it rather unconvincing to pick a random keyword (that you find in every Cish language) and demonize it.
17th Sep 2019, 7:24 PM
HonFu
HonFu - avatar
0
while True: inp = input() if inp in ('y', 'n'): break for el in array: if type(el) is not int: continue print(el*2) Oooh, I'm playing with fire here! 😁
17th Sep 2019, 7:27 PM
HonFu
HonFu - avatar
0
Lol. Yeah your right. They just gimme a bad feeling is all. A little bit too much like goto😖
17th Sep 2019, 7:29 PM
Trigger
Trigger - avatar
0
Goto is totally different. A loop has a body. It has a beginning and an end. Sure, you can create multiply indented loop monsters that throw stuff around for 100 lines, but that's not the way we use them, is it? Normally a loop body is a few lines of code, and that was it. You have to control what happens inside there. It can for sure trip you up, like everything else. But not like goto, which allows you to jump from every single point in your program to any other one! And even for goto you'll find one or two use cases where only the most paranoid person will see hell fire. (Like breaking a double loop without having to use a flag variable for the external loop.)
17th Sep 2019, 7:46 PM
HonFu
HonFu - avatar
0
Yes, Break and Continue is IMPORTANT for looping and conditional statement
18th Sep 2019, 1:59 PM
Praphull Kumar
Praphull Kumar - avatar
0
For(int i = 0; i < 10; i++) { If(i == 5) Break; } Here the for loop will stop iterating when i equals 5 But if you replace break with continue, and you add a Console.Write(i); It will display 012346789 . Continue is used when you want to skip that step
18th Sep 2019, 8:07 PM
Catalin Popinciuc
Catalin Popinciuc - avatar