The only reason 'goto' exists in C++ is for compatibility with libraries that are written in C. Everything that it does can be better accomplished with function calls and\or try, catch exceptions.
Is it good or bad to eat with your hands? Most people would say it's bad. Same with goto. Many people dislikes goto.
It is possible to eat a whole dinner with your bare hands and it is possible to write every loop in a big program using goto but it maybe it's not such a good idea.
goto is not bad it is just misused. Learn what it does and how to use it. There is often a better way than using goto but sometimes you will find that it is a simple way to achieve something.
PS: It is not worth asking for an example of a good time to use goto as such an example would be non-trivial and any trivial example would be met with a counter "do it without goto like this"
.... but sometimes you will find that it is a simple way to achieve something.
These "sometimes" occur only when the programmer has no brain that is when he has a very low qualification. The less qualification the more often such a programmer will find "a simple way". And the original post shows such a simple way.:)
Well vlad, my advice: Don't use goto until you have learnt enough to know why using goto is 'bad', then you will have learnt enough to us goto properly.
@Vlad.
So you're saying that a qualified programmer will never be in a situation where they need to, or where it would be best to, break out of a nested loop?
@ htirwin: I'm going to side with vlad here. Program flow control isn't something that should be ignored just because you have an instruction that can jump out of a loop.
This is your sample:
1 2 3 4 5
for (int i = 0; i < 10; ++i)
for (int j = 0; j < 10; ++j)
if ( (x = T[i][j].g()) < e )
goto end_for_for;
end_for_for:
This is my version of a solution that isn't context specific:
1 2 3 4 5 6 7 8 9
void* ArbitraryObject::MyNestedForLoops(int i, int j, ...)
{
for (i = 0; i < 10; ++i)
for (j = 0; j < 10; ++j)
if ( (x = T[i][j].g()) < e )
{
return T[i][j].g();
}
}
This is what Grey Wolf meant when he said "It is not worth asking for an example of a good time to use goto as such an example would be non-trivial and any trivial example would be met with a counter "do it without goto like this" ".
@Vlad.
So you're saying that a qualified programmer will never be in a situation where they need to, or where it would be best to, break out of a nested loop?
You are mistaken. I said that 1) a qualified programmer never use the goto statement.. And 2) the less qualification has a programmer the more often he will find a "simple way" with using the goto statement.
You are not the first and are not the last who are trying to reanimate the goto statement due to your own low qualification.
I advice you to reread the original post. There is written
I tried to solve this question again using only simple statements.
The goto statement proved here very useful.
What is the qualification of the author of the topic? He is a beginner. And you are not very far from him.
He "proved" here that his goto is very useful As for me I consider his code as a very and very bad code.
That may be the way to go many times, but in the one single use I have found for goto, in my 3 thousand line program, I opted not to make a separate function to avoid use of goto.
The reason being that the method where it is used, is small and the nested loop is the main part, and there are a few lines of code after the nested loop that need to get executed.
Breaking it up into multiple functions would only add a new method that by itself is meaningless, and that would not see reuse in any other context.
I'm not saying that you're wrong or that you're a bad programmer. I was only trying to illustrate Grey Wolfs point, and in fact I may even expand on it. Would you like to see make an argument that says a real programmer should never have to use if else statements? Because I'm fairly confident I can do it.
I dislike goto because it makes code hard to read, it's use actually involves more work then most of the alternative methods that accomplish the same thing and new users get complacent with using it.
1. Beginners should not use goto - there is almost always a better way. Learn to program properly.
2. Experts can use goto in situations that warrant it. Being an expert allows them (not me) to recognise when it is feasible or appropriate to use a goto.
The more dangerous, and damaging to maintainability, a particular practice is, then the more experienced a developer has to be before they should consider using it.
"goto" is such a dangerous construct, and can have such a negative impact on maintainability, that almost nobody who attempts to use it is experienced enough that they should be using it.