Personally, the 'problem' with gotos is just that it breaks the structure of code. Usually, if you can code the same thing using if or for, then it will look better, and it will not interrupt your indentation.
While I don't think it should be illegal (feel free to use a goto if you really mean goto), be careful that you're not doing something that would look pretty much the same using if.
In my experience, there have been two reasons to use goto...
1)
Code:
for (;;)
{
scanf ("%d ", &x);
switch (x)
{
case 1: // blarg
break;
case 2: // bloo
break;
case 3: // exit for loop
break; // obviously not neccisary, but makes a point...
default: // boggiltyboo!
break;
}
}
exitfor: // BLEE
Ah yes, the forever loop. Typically, this is broken out of using a break. However, within a switch, or some other constructs, like maybe another loop, you can't use a break. In this case, it might be helpful to just use a goto exitfor; It could be done also by changing the structure of this code, but the code is already pretty clean, and it isn't that hard to figure the meaning of the line goto exitfor;
Also, sometimes I want to return from a function, but there are things that I absolutely must do every time I exit from the function. Consider the following code...
Code:
void doStuff (int i)
{
// Lots of code
// End of function...
ex:
popStuff();
freeMemories();
closeFiles();
hidePasswords();
return;
}
Now, without goto statements, there are two ways I can handle this. First off, I can just go ahead and perform these operations for every case where I want to escape the function:
Code:
// Code here.
if (something)
{
popStuff();
freeMemories();
closeFiles();
hidePasswords();
return;
}
// Code here too.
// End of function...
And actually, if we encapsulate all this in a macro, then it wouldn't really be all that hard to write. However, this also leads to having a lot of redundant code.
The other way would be just using if not statements...
Code:
//Code here.
if (!something)
{
//Code here too.
}
// End of function...
This is exactly the same as using the goto statement, except for needing a label. This method can result in problems however, since every catch is going to add one more indention to your code (this can clearly start a lot of problems if you have more than one of these), and it is in some situations just a strange construct, having a trivial test have a block over such a large piece of code. Also, this method just can't be used in all cases.
Even in light of these reasons, the goto command shows up rarely (practically never) in my code. It just doesn't happen all that often, and if it does often in your code, then it is possible that you are just writing programs in a bad way. If you are using a goto statement, just make sure that it makes sense to use a goto there, that you don't already have very many gotos in the function (if you are running out of ideas for label names, then that's a clue), and that the label makes the goto meaningful...
goto escapeFunction;
makes sense
goto label3;
means nothing.
quzah: I'm not sure if that's really a fair example... That code is obfuscated, uses incorrect syntax, and is an endless loop.
Not to mention, I get the feeling that that code would be even harder to understand without using gotos, which would mean it is a good example of when to use gotos.