Thread: goto

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    goto

    could someone please explain to me in simple terms as to why goto should not be used in a program. i have tried to use some of the links you guys have shown other people butthey are so long that i cant follow them. dont call me a fool cuz i already know i am
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    No....

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    here is an article about "when you could use goto"

    http://www.cprogramming.com/tutorial/goto.html

    edit:: in simple terms you should not use goto because it makes the code (spaghetti-code) confusing to read; the program typed out structure does not follow its execution when using goto's.
    Last edited by axon; 01-26-2005 at 11:36 AM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Using goto is small programs probly wont do any harm but its good to get into good habbits if you use goto in larger programs it can make code hard to read so its not recomended.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    basically, it often results in unstructured control-flow that is difficult to maintain and understand. there are very few situations where it is justified to replace normal loop-control mechanisms with it. on a side note, if C/C++ would incorporate nested breaks/continues it could eliminate goto from the language completely.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Do a board search. This has been beat to death several times on these forums.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one of the latest discussions (from the C board) :
    http://cboard.cprogramming.com/showthread.php?t=59954

  8. #8
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    thanks swoopy, but i still dont see why it is any more "spaghetti-er" than using a function to go to a part of code.
    Keyboard Not Found! Press any key to continue. . .

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I was reading an issue of "Software Development" issue January 2005. The article that starts on page 49 called Pillars of Pretty Code discusses goto as one of the ways to keep your code to the left as much as possible (more readable).
    Quote Originally Posted by Mr.Seiwald
    "Forcibly align the main flow of control down the left side, with one level of indentation for if/while/for/do/switch statements. Use break, continue, return, even goto to coerce the code into left-side alignment. Rearrange conditionals so that the block with the quickest exit comes first...."
    So in other words, depending on your style of programming, it could mean putting your code readability in jeopardy if you have no choice but to use goto to move the code to a more left-side alignment.

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    > thanks swoopy, but i still dont see why it is any more "spaghetti-er" than using a function to go to a part of code.

    a function is defined in a block of code in one specified place. Even if you have one huge monolithic file (and its designed well), you can follow function calls with ease...if, in that same file you introduce multiple gotos that jump from place to place, you will get confused very quickly.
    Last edited by axon; 01-26-2005 at 10:40 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  11. #11
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >could someone please explain to me in simple terms as to why goto should not be used in a program.
    goto is the "easy" solution. The problem with "easy" solutions is that the temptation for abuse is overwhelming. People will describe that as spaghetti-code or non-structured, but in the end it's about the overuse of a feature to the point that it makes code hard to follow.

    You would have the same effect if you overused functions, but for historical reasons, programmers tend to be afraid to use a great many small functions, so overuse isn't as tempting as with goto.
    Kampai!

  12. #12
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    well if its the same block of code that you are using goto to go to every time then all you have to do is analyze it the first time and just skim it to see if it is different the next
    Keyboard Not Found! Press any key to continue. . .

  13. #13
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    That's why there's nothing wrong with goto. It's how you use it that can be a problem. Of course, there are few (if any) good reasons for using goto these days because there are other (more conventional) ways to solve the problem. When given the choice between conventional and oddball, you should choose conventional.

    For example, this is conventional for a counted loop:
    Code:
    for (int i = start; i < end; i++) {
      // Stuff
    }
    While this is not:
    Code:
    int i = start;
    while (i < end) {
      // Stuff
      ++i;
    }
    The former is immediately obvious because it's a common idiom. The latter may take you a moment to figure out because it isn't as common.
    Kampai!

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Sebastiani
    on a side note, if C/C++ would incorporate nested breaks/continues it could eliminate goto from the language completely.
    If you want to, you could use exceptions to break from a nested loop. Requires a little more code, though.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #15
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by Sebastiani
    basically, it often results in unstructured control-flow that is difficult to maintain and understand. there are very few situations where it is justified to replace normal loop-control mechanisms with it. on a side note, if C/C++ would incorporate nested breaks/continues it could eliminate goto from the language completely.
    I think someone in the '70s proved that given a while loops and if statements you can construct any control flow you can obtain using goto.

    DISCLAIMER: while and if might be enough to do anything but it doesn't mean it's going to be pretty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. goto command
    By jhwebster1 in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 12:32 PM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM