+ 2

Why is it not recomended to use goto loops??

Hello! So i kinda know my way around C++ and i am currently learning C#. Every programmer i have ever asked, told me not to use what is know as a "goto loop". However i do not understand why. They are very easy to use and they usually get the job done. I am not saying the goto loop should replace the for or the while loop (since each one has their own purpose ) but why can i not use them? Sometimes it would just be sooo much easier to get something done with a goto rather than a for or while loop.Thank you!

8th Jun 2017, 2:11 PM
Andrew Bratu
Andrew Bratu - avatar
4 Antworten
+ 9
They can make code confusing or hard to debug. I think they could be ok if they are very clear in thier intent.( dont jump heaps far ahead or away) I don't use them as I also have had that drilled into me by others. But think they could be useful. Otherwise they would have been removed long before now
8th Jun 2017, 2:16 PM
jay
jay - avatar
+ 6
😊 true. that is a good label if it is indeed on line 24. You could see how if the label was something like theMove and that label was 1000 lines away it would make code hard to read and a pain to debug. I think that is why the general advice is to avoid them if you can
8th Jun 2017, 3:22 PM
jay
jay - avatar
+ 3
If they are unclear i beleive i have a solution for that. I can call the point tha i am going to jump back to "Line(and the number of the line)" Like this : Line24: other code..... goto Line24; i believe that is very clear
8th Jun 2017, 3:15 PM
Andrew Bratu
Andrew Bratu - avatar
+ 3
Well, since i am just a beginner, i write an average of 70-80 LinessOfCode per program so i don't think it would be that confusing.
8th Jun 2017, 3:26 PM
Andrew Bratu
Andrew Bratu - avatar