0
0

[–] Zesty 0 points 0 points (+0|-0) ago 

I have had many people sneer when they see a goto in my code. But usually it's in some triply nested loop that I just want to exit simply, and I always keep the goto within the scope of the function itself.

0
1

[–] MrBoneCrusher 0 points 1 points (+1|-0) ago  (edited ago)

Exiting out of nested loops is really the only excuse I've seen for using gotos, but it's still a valid reason to use a goto. The alternative is to use some sort of exit flag that you set when you want to exit and check at each loop, but that can get just as messy, if not more, than just setting a goto to break out of the loops. Or, if the language supports it, throw an exception - but sometimes the reason you want to break out of a nested loop isn't necessarily an exception but because some exit condition was reached.

Perhaps a valid alternative would be to provide a numerical variable to break to specify how many levels you want to break out of, e.g. break(2) to break out of a double-nested loop. I wonder if this has ever been attempted?

0
0

[–] Zesty 0 points 0 points (+0|-0) ago 

What a beautiful and simple solution.