many time it is more easy to do iteration or looping with goto statment. but research on internet always adviced me to donot use goto statment. can anyone tell me what is the problem with goto statment
I forget who said this to me, but "You can use goto statements once you know enough to understand why goto statements are bad." But even then, use them sparingly.
In experience, readability is a big problem with goto statements. When you get a whole bunch of jumps to different areas of your code, it becomes impossible for someone else (and sometimes yourself) to follow the logic.
There's nothing you can do with goto statements that you can't do with other C++ conventions.
Goto statements are primitive. While they are enticing, it's not hard to turn your code into spaghetti with them.
So no, using a goto statement will not kill your program. But it's generally warned against, because you don't have to use them (at all) to accomplish the same task.
It comes down to program structure. The use of various control structures such as loops encourages a clear, easy to understand design.
The goto instruction can be used in a similarly well-structured design, but goto by itself does not imply any particular structure, and may instead lead to a somewhat chaotic lack of structure.
When reading code written by someone else, if you see the keyword while or switch etc., you can recognise immediately a particular structure. But if you see the keyword goto, you can deduce nothing at all.
In some languages, goto can be used, with care, to produce cleaner and better-structured code. But in C++ there are enough alternatives to make its use mostly unnecessary. I say mostly, because I wouldn't rule out its occasional use, but I would certainly consider other options first.
It's sad that everybody came to a consensus without actually knowing the proper uses for "goto". Implicitly, the compiler adds "goto" in some contexts. Spot the "goto":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main( )
int Input_( 0 );
while( Input_ >= 1 )
std::cout << "Enter a value greater or equal to 1: ";
std::cin >> Input_;
if( Input_ <= 0 )
std::cout << "That's not equal to or greater than 1...\n";
return( 0 );
Can you spot it? If not, then shame on you. The "continue" is secretly a "goto" in disguise.
Another use for "goto" is error handling in C, which lacks exception handling. This form of error handling is common in kernel implementations. Because C++ has its own way of handling exceptions, "goto" becomes a redundant form of error handling. However, "goto" still has its uses but I've not yet encountered any.
In addition, "goto" cannot be a deprecated form of control due to legacy dependencies.
Speaking of implied goto's, someone quite a while ago posted code which was an aid to help them learn assembler. The assignment was to write a simple C program using only if conditions & goto's for flow control. Presumably later they would translate the code to assembler. The conclusion was that, for this very specialised learning method, all flow control can be done with if and goto.