+ 8

Are break and continue statements discouraged from being used in loops and if so why?

With goto statements in older languages that depend on a line number that the program has to jump to I can understand why gotos are discouraged, because the line number or label that the program was meant to jump to can later be changed resulting in a jump to a wrong instruction. But why are breaks and continues in C, C++, Java and other more modern languages discouraged? It can't be due to code readability. Is it about elegance of an algorithm? If so, why is that important?

25th Aug 2018, 12:38 AM
Sonic
Sonic - avatar
15 Réponses
+ 9
Goto itself is not dangerous. In general, the people who use goto statements form the base of the problem - Beginners who rely on shortcuts to make things work. If you are an expert, and you are sober, I wouldn't worry about you placing a few goto statements into a project. Similarly, break and continue statements can be abused to produce poorly written codes. Instead of discouraging the use of these statements, it would be better to encourage *proper use. However, the former is easier to execute, because, out of frustration, slapping someone with a book is easier than opening the book and then explaining for the 20th time why and what is done wrong.
25th Aug 2018, 4:58 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
A discussion at Hacker News linking to a short article discussing why banning GOTO made things worse, from the Association for Computing Machinery (ACM). “GOTO Considered Harmful” Considered Harmful (1987) https://news.ycombinator.com/item?id=11056434 The Linux kernel uses GOTOs to a fair degree; Linus Torvalds' opinion on them leads to more interesting discussion.
25th Aug 2018, 2:23 AM
Kirk Schafer
Kirk Schafer - avatar
+ 6
Kirk Schafer thanks. I particularly like the title "'"GOTO Considered Harmful" Considered Harmful' Considered Harmful?" and the nesting of its quotes 😁.
25th Aug 2018, 2:54 AM
Sonic
Sonic - avatar
+ 6
Lol Hatsy Rei love your graphic analogy. Are you OK though? Why not have a beverage and relax a little. All this moderation need not take its toll on you 😁.
25th Aug 2018, 5:58 AM
Sonic
Sonic - avatar
+ 5
Eduardo Costa e Silva, Hatsy Rei, Deepak Gautam, Kirk Schafer , Noa , thank you for your valuable input to my question. Do you mind upvoting my original question? I'm having difficulty getting likes on my questions 😂.
27th Aug 2018, 4:05 AM
Sonic
Sonic - avatar
+ 5
I'm hungry and tired, I think I'll just goto a diner and grab something to eat, take a little break and continue after my battery's been recharged LOL I love this discussion ...
27th Aug 2018, 11:55 AM
Ipang
+ 3
Great response Ipang and so relevant too that you managed to cover goto, break and continue in one sentence! Enjoy your meal at the diner, and while you're at it, don't forget to give us a little up-vote 😉.
27th Aug 2018, 2:27 PM
Sonic
Sonic - avatar
+ 2
I have seen this policy too but I don't agree with it. Maybe I'm old school but I think that sometimes you have to go to such lengths in order to be purist that you loose instead of gaining readability. Obviously it will depend on the situation. If you have a long code inside a loop, you have several breaks and continues and either you need to know how you exited the loop or have difficulty tracing your code, something is very wrong. On the other hand, if you need extra code to avoid the break or continue, are you gaining anything?
25th Aug 2018, 1:09 AM
Eduardo Costa e Silva
+ 2
Deepak, you remind me of the famous discussion between Djikstra and Knuth over "Goto considered harmful".
25th Aug 2018, 1:30 AM
Eduardo Costa e Silva
+ 2
Besides, the keyword may be "discouraged". It means you shouldn't use it everyday everywhere. But sometimes it may be the best solution.
25th Aug 2018, 1:33 AM
Eduardo Costa e Silva
+ 1
I do know that. I haven't used Goto in ... ages. There are very specific situations where you may have to abandon two or more loops at once and to do it with break may need more complex mechanisms. As you said, it must be used very carefully, as a last resource. After all, the computer science evolved a lot since the discussion I mentioned, in the 70s.
25th Aug 2018, 1:43 AM
Eduardo Costa e Silva
+ 1
One day I was asked to write in the board (blackboard?) my solution to a problem. It was a wide board and you had two columns to write in it. Just for fun, after writing in the first board, I wrote Goto there; in the last line of the first column and there: at the first line of the second column. I was almost crucified and apparently nobody noticed that the two lines were completely irrelevant and could be ignored.🤣🤣🤣
25th Aug 2018, 10:47 AM
Eduardo Costa e Silva