Shay Harding, 23 years programming in languages from ASM to JS
Goto is a kind of "low level" construct. If you want to, you could go the other way and rewrite all your for loops and while loops etc using gotos.
E.g a for loop in c
for(i=0;i<100;i++) { // some code }
could be rewritten as i=0; start: { // some code i++; if( i<100) goto start; }
Since it is exactly the same structure of code, then - the only thing you can say about the version with the goto is that it isn't so easy to read if you are used to reading code with for loops in it. And it needs a few extra statements. And that the version without the goto has certain levels of build in error checking. And that you don't have to keep defining new labels every time you build a for loop.
So - actually that's lots of benefits in fact. You can understand why programmers typically use for loops or while loops.
But very occasionally you may find yourself in a situation that is not easily covered by for and while loops.
So what you do next depends on whether you are a high or a low level programmer. I'm a low level programmer by inclination, so I quite freely use gotos.
One place I often do this is in subroutines.
I may need to initialize a lot of stuff at the start of the routine, including perhaps allocating memory etc - I work at a very low level so this is stuff that in higher level coding is done automatically. Anyway means I need to clean it all up at the end of the routine.
But might be that half way through the routine, you discover that the rest of the coding is not needed, it's an exceptional case of some sort.
So then I just do
goto to_return;
And then at the end of the routine
to_return: // clean up code return return_value;
To my mind that is far easier to read than other more elaborate solutions to this situation.
It is all about readability and maintainability. And error checking, which is easier to do if the code is easy to read.
So if it is more readable and easier to understand with a goto, then use a goto. That's what I'd say.
However as a beginner programmer, then you also need to learn to read code. And what may seem more readable to you may seem obscure and confusing to programmers with more experience.
So - especially for beginner programmers, avoid gotos as it is harder to make readable code with them.
As you continue, you learn a few exceptions and situations where it actually may help to use them.
And there are some places where you should almost never use a goto. Unless working at a really low level, then never use them at all.
GOTO JUMP BACKWARDS
It's generally frowned on to do a goto to an earlier place in your code. Always go forwards. Otherwise it is hard to keep track of things.
However - that's again a matter of readability. In our rewritten for loop:
then we did have a goto to an earlier point in the code.
So the for loops have an implicit jump backwards in them. Jumping back in the code is not by itself bad, indeed it is essential. It is just that it is usually done using higher level constructions.
I have actually very rarely done a goto backwards in my own code. With great care in a situation where I felt it added to the clarity of the code and made it easier to maintain. But that's a programmer working at a very low level typically. High level programmers will probably never do this at all.
GOTO FORWARDS INTO A BLOCK
Something like this is very much frowned on. You can maybe think of some unusual situations where it would be useful working at a low level but normally, no, never done.
i=0; // some instructions
go to middle_of_loop;
for(i=0;i<100;i++) { int x=0; int y=0; middle_of_loop: // more instructions }
The problem with a forward jump into a block like that is that the variables there aren't initialized. The x, y and i there could have any values.
If you do a goto like that you need to redo the initialization:
i=0; // some instructions
i=0; // make sure that i is at a sensible value before you jump into the for loop goto middle_of_loop;
for(i=0;i<100;i++) { int x=0; int y=0; middle_of_loop: x=0; // Need to initialize x and y again in case got here from the previous goto middle_of_loop y=0; // more instructions }
With careful thought it could be done safely. But it is hard to think of any example where this actually makes the code easier to read and maintain. And many ways it could be confusing and lead to hard to fix bugs.
Unless you are totally sure, very experienced programmer and know exactly what you are doing and why, never do this. And even then think very hard and you probably still won't do it.
And backward gotos again - almost never do it.
Forward gotos always into the same block or jumping out of a block - well I think it is a matter of style and taste partly and what it is that you find easiest to read, and how low level your programming is - but for beginners, again, is good advice to avoid it, and to learn how to write code without doing this. Once you know how to write code without gotos, then you may find a few situations where they actually help.
As a low level programmer I see nothing wrong at all with continue and break, use them all the time.
But that again is a matter of style and some may prefer to not use them. And if someone is teaching you - and they themselves write good easy to read code or teach people who do - well you are learning from them, best to start by following the methods they suggest and you may come to write good code yourself.