Quote:Original post by Daaark
That's exactly what your goto statement did. It jumps out of your loop to another address, just like a break would do.
What's the big deal? You must have read too deep into someone's anti-goto sentiments. That rule gets hammered into people way too much. People learn how to properly write functions and classes these days, we aren't 6 months out from people writing oldschool 'goto linenumber' BASIC code.
Um, sure you could use a goto. But *why*? Why not do the proper solution and put the loops in their own function, from which you can return. No need for goto, no need for multiple breaks. Just return when you want to exit the loops.
Nested loops are the most common justification for gotos I see. And it's still wrong. That's what functions are for. You should get used to those. They're there for a reason.
And this is exactly why use of goto is generally discouraged. Because it creates programmers whose solution to everything is "one long function, and use goto instead of factoring out into smaller functions".
I agree, the use of goto in this case isn't overly hard to follow, but it's still pointless, because you can achieve the same without using goto.
And the problem is that even if your use of goto is harmless, the programmer reading the code doesn't know that. He still has to read through the code verifying which gotos jump to which labels, and vice versa, to even figure out whether or not it's spaghetti code.
If you need to be able to jump out of a block of code, then that block should be a separate function.
If you need to be able to jump *to* a block of code, then it should most likely also be a separate function.
I know that almost every C/C++ programmer starts out cramming everything into the main() function. And then they start writing one or two other functions, and still cramming everything into those.
And then they do like you, saying "Oh, but my code doesn't fit, I'd better use some gotos", when they should just accept that they're going to have more than 3 functions in their program.