Why is goto bad?

This is a discussion on Why is goto bad? within the C++ Programming forums, part of the General Programming Boards category; I've been looking around the boards and have seen that using goto is bad. Why? Also, is there a way ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105

    Why is goto bad?

    I've been looking around the boards and have seen that using goto is bad. Why? Also, is there a way to go to the middle of a loop from lower down in the loop without using goto? This question is not on the FAQ (I looked).

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've been looking around the boards and have seen that using goto is bad. Why?
    Put simply: there's nothing that requires goto, and it's easy to abuse it. The real problem is people who don't know how to use goto, not goto itself.

    >Also, is there a way to go to the middle of a loop from lower down in the loop without using goto?
    Restructure your loop so that you can you can use the iteration mechanism rather than forced jumps. Give an example of your goto and we can show you an equivalent example without it.
    My best code is written with the delete key.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Put simply: there's nothing that requires goto, and it's easy to abuse it. The real problem is people who don't know how to use goto, not goto itself.
    If theres nothing that requires goto then why should people need to know how to use it?

    Sorry I'm just being annoying

    Also, is there a way to go to the middle of a loop from lower down in the loop without using goto?
    You could use an put all the stuff to skip in an 'if' statement that is results as false when you don't want to run it. Theres also a 'continue' keyword that jumps back to the beginning of the loop.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    I use it in switch starements if the user gives invalid input.
    Code:
    int main()
    {
        int choice;
        cout << "Game Name Here v 1.0\n";
        while (choice != 4)
        {
              cout << "Main Menu:\n";
              cout << "1. Play Game\n";
              cout << "2. Instructions\n";
              cout << "3. Credits\n";
              cout << "4. Quit\n";
              choose:
              cin >> choice;
              switch (choice)
              {
               case 1:
                    play();
                    cout << "Game Name Here v 1.0\n";
                    break;
               case 2:
                    instruct();
                    cout << "Game Name Here v 1.0\n";
                    break;
               case 3:
                    credit();
                    cout << "Game Name Here v 1.0\n";
                    break;
               case 4:
                    break;
               default:
                       cout << "\a";
                       goto choose;
               }
         }
        return 0;
    }
    In that example, the goto redirects the user back to the cin statement if they don't input a menu choice.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You could probably keep the 'cout's that come before 'choose' out of the loop. Or better yet wrap them in a function (along with your loop and switch statement) that gets called when you want to run your title screen.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I'd make it a while loop from choose: to the end of the switch, then add a bool variable set to true if the default section of the switch is hit. You can also check the state of cin to see if it failed and set the bool to true in that case as well (and of course clean up cin using clear and ignore). It will protect you from the user typing a non-digit character instead of a number.

    Also, don't forget to initialize choice. It might accidentally have the value 4 some time when the user runs your program and then your loop would never be executed.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    The variable choice is initialized at the top of the function. Thanks for the help.
    The top of the while loop would look like:
    Code:
    while (invalid==true)
    with invalid initialized as a bool variable right?

    And the default would look like:
    Code:
    default:
                invalid=true;
                break;
    right?

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    First you would need to initailise invalid as true, otherwise your loop may never run.

    Remember that would print your main menu out again each time a bad entry was made. From your original code it did not look as if you wanted to do that, but if you put the menu printing code outside the loop that should work fine.

    Also it would be good to do what Daved said about deal with non-digit input.
    Last edited by mike_g; 12-01-2007 at 09:05 PM.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Can't I just put the while start where I have the goto label?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,678
    Quote Originally Posted by mike_g View Post
    If theres nothing that requires goto then why should people need to know how to use it?
    Logical thinking would be a reason.

    A lot of (most?) high level languages provide several features (looping constructs, etc) that can be used to achieve the same effects that can be achieved with a goto. Those looping constructs can also be substituted for each other, with suitable restructuring of code.

    When code with looping constructs is compiled (translated into a set of machine instructions), the translation often makes use of machine-specific jump instructions which are formally the equivalent of a humble goto statement in the higher level language. So understanding the notion of a goto can help with better understanding how looping constructs (eg for, do/while, or while in C++) work and using them more effectively. It is also rather fundamental to being able to write things like compilers.

    There are also some circumstances in which the use of a humble goto can be used to do something much more simply (with translates into code that is easier to understand, get right, and maintain) than is possible with other language constructs.

    Like all coding constructs, goto is a tool. And like any tool, there are circumstances when it is beneficial to use it and other circumstances where it can be misused.
    Quote Originally Posted by mike_g View Post
    You could use an put all the stuff to skip in an 'if' statement that is results as false when you don't want to run it. Theres also a 'continue' keyword that jumps back to the beginning of the loop.
    Think about how if blocks work. In practice, some compilers will translate compound if statements of the form;
    Code:
    if (condition_1)
       code_block_1();
    else if (condition_2)
       code_block_2();
    else
       code_block_3();
    into machine instructions that are effectively the same as;
    Code:
      if (!condition_1) goto skip_code_block_1;
      code_block_1();
    skip_code_block_1:
      if (!condition_2) goto skip_code_block_2;
      code_block_2();
    skip_code_block_2:
      if (!condition_2) goto skip_code_block_3;
      code_block_3();
    skip_code_block_3:
    It may not look as pretty (I certainly don't suggest writing your higher level code this way) but it is one valid way of translating the code, depending on how limited a set of machine instructions is provided by the target machine. Particularly as few machines support anything like an "else" statement directly as a machine instruction.....
    Last edited by grumpy; 12-01-2007 at 09:35 PM.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    So should I use goto? Or should I use a while around my cin+switch statements?

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    What you should do is put a do..while loop around your cin and switch.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by DarkAlex View Post
    I've been looking around the boards and have seen that using goto is bad. Why? Also, is there a way to go to the middle of a loop from lower down in the loop without using goto? This question is not on the FAQ (I looked).
    It's undisciplined. It lets you easily modify code in ways that are hard to understand later. It makes flow of control obscure. When you see a goto label in code, you do not know all the locations within that function which might transfer control to that label, without looking through all of the code.

    There are no programming problems that can be solved only by goto.

    goto exists because there are a limited set of situations where it might be the most understandable way to solve a certain problem. But using goto is sort of like writing a sentence fragment instead of a full sentence. A skilled writer can do it for literary effect and get away with it, but an unskilled writer just comes across sounding uneducated.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Okay, that makes sense. Thanks for all the help.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    My philosophy on goto is this:

    If you run into a situation in which you are tempted to use goto to solve your problem, spend some time to see if you can restructure your code to avoid it.
    If that fails, ask a more experienced developer if they can see a better solution than using goto.
    If that still fails to find an alternative (or at least one that makes the code more readable) then use goto, but put comments in the code that say exactly what you're doing and why you felt the need to use goto...

    If you find yourself using goto often (more than a few times a year), you probably aren't looking hard enough for better alternatives.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. goto statement interpretation in VC 6 and 7
    By w262 in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2005, 10:37 PM
  2. Not important, only a curiosity...
    By BrownB in forum C Programming
    Replies: 35
    Last Post: 12-27-2004, 03:32 PM
  3. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 09:26 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21