goto statment

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.
Last edited on
And also, because the processor just jumps to the label, variables might not have the value you think they have, which can result in segmentation faults or strange behaviour of your program.
i prefer u use while loop
because goto is not a good practice and alot developer
are proposed that to erase these command
The proper thing to do is to use loops, and if you feel you need conditions to move out of said loop. don't use goto, use booleans.

exemple;

1
2
3
4
5
6
7
8
9
while (running)
{
    if (condition)
    {
          // stuff
    }
    else
        running = false;
}
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.
goto: spaghettiCode;
In all seriousness, it's best to avoid them.
closed account (zb0S216C)
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";
      continue;
    }
  }

  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.

Wazzak
Last edited on
The difference between continue and goto is that it's very easy to determine where continue is going - it always goes to the start of the loop, a definite point.

With goto, that responsibility is left with the programmer. Let's not assume that all programmers are responsible. ;-)

There are lots of gotos implied in just about every program.

An ordinary if statement, with or without an else, uses a goto. A while loop, with or without break and continue also uses a goto. There are other examples but that will suffice.

What is important is whether the goto is part of a proper control structure, or whether it is used without any form of constraint.
Last edited on
Thanks All now my understandings are more clear
Just 1 more thing .....

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.
Topic archived. No new replies allowed.