Thread: goto

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    goto

    I was reading the first c book I ever read through, for fun, and found this passage.
    Originally said by Dan Gookin, C for dummies volume 2.
    Most C language books don't teach anything about the goto, other than a passing mention. One programmer, however, set out to find virtue for the vilified goto. He conccted a program that used a goto and could not be written any other way. Keep in mind that this guy was a top-notch C programmer and he had to work at it to get a justifiable goto program. For us mere mortals, this isn't an option.
    After reading that I was curious, what could the program be?

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I'm guessing that it was something that, if originally designed better, could have been programed without a goto I don't like the Dummies programming books - I own a few, and they're horrible. Anyway, I think there's contact info inside the book in the first few pages - email Dan Gookin If he responds with the program, post it here.
    Away.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    ok, e-mailing him now

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It was likely something where speed really really matters, or in a deep nested loop. Those are really the only two uses for them.

    I personally have never had the need, unless I'm intentionally obfuscating someone's would-be homework.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>and could not be written any other way
    I find that statement hard to believe. There are always many ways of achieving the same task in programming, some ways good, some ways bads, and some ways are just.... ways.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    I absolutely agree with Hammer. There is a classic paper in which the renowed computer scientist Dijkstra proves the harmful effects of GOTO and advocates the need to abolish them from all "higher level" programming languages.The paper is named "GOTO Statement considered Harmful".

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The paper is named "GOTO Statement considered Harmful".
    A good read, if a little dated. My personal opinion on goto is that if it is such a terrible, evil spawn of satan, why do so many modern languages support it or reserve the identifier for possible inclusion in future updates? I believe that like everything else, goto is a tool. It can be misused just like any other tool and in the right hands (at the right time) it can be a world of help. I've not used goto in anything but trivial examples, but it is nice to know that it is there should I need it.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Goto is like the mace that you carry in your pocket when hiking through wilderness with grizzly bears. You hope you'll never need it, and you probably never will. Chances are good that you'll never have to use it. But Prelude is right - it is nice that it is there.
    Away.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    If memory served, Donald Knuth developed an algorithm that required a goto on a dare. I've never seen it, though.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212

    'goto' is perfectly valid

    There is nothing inherently 'evil' or 'wrong' about using goto. Like any other tool in one's programming toolbox, it has a specific place and use it is best suited for.

    There is no intelligent basis whatsoever behind this unwarranted discrimination against 'goto'. The Pascal Purist who came up with a dislike for goto didn't know assembly, and only made such a discrimination because seeing 'goto' in code wasn't 'pretty'.

    Well, I got news-- high-performance code usually isn't pretty. The faster something is, the simpler and more straightforward it is. It's a simple law of physics.

    And 'goto' doesn't require a test-- it's the equivalent of a jump instruction in assembly.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: 'goto' is perfectly valid

    Originally posted by Sayeh
    There is nothing inherently 'evil' or 'wrong' about using goto. Like any other tool in one's programming toolbox, it has a specific place and use it is best suited for.
    This is very true

    There is no intelligent basis whatsoever behind this unwarranted discrimination against 'goto'. The Pascal Purist who came up with a dislike for goto didn't know assembly, and only made such a discrimination because seeing 'goto' in code wasn't 'pretty'.
    This I will disagree with. I started with languages that had no structure and block concepts, these had to be shoehorned into the language. The discrimination against 'goto' has to do not with the 'prettiness' of the code but the processing flow -- the 'spaghettiness' of the code. It's very difficult to follow code with a multitude of goto's whereas blocks are much easier on the understanding. Now don't get me wrong, for small programs the goto does not necessarily create poor code, it's the programmer. But for complex code with a lot of branching (algorithms that are difficult to understand and implement) the goto generally will make the code more difficult to follow.

    Well, I got news-- high-performance code usually isn't pretty. The faster something is, the simpler and more straightforward it is. It's a simple law of physics.
    This is old news. But you are using terms here that can be extremely contradictary. Generally, if you program for speed, the code will not be easy to understand and maintain, whereas if you code for maintainability and simplicity, the more higher-level concepts you will need to use, slowing down the processing. Why else do many compilers have two modes -- compile for speed (larger size) or size (slower processing)?

    And 'goto' doesn't require a test-- it's the equivalent of a jump instruction in assembly.
    Duh! But neither does a defined block. But in general, the reason for a goto is that previously a test has been done and we are at the conclusion of the test. The goto simply jumps around code that is written for a different test condition. Identical to a switch.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    WaltP--

    If memory served, Donald Knuth developed an algorithm that required a goto on a dare. I've never seen it, though.
    If you are referring to the one I know of, it wasn't that the algorithm required a goto, it was just that the use of the goto eliminated one of the condition tests, which allowed the algorithm to run about 25% more efficiently. This is what caused Dijkstra to flinch a little on the "abolishment" of goto. At least, until machines increased in speed, and reduced the pressure on the programmer to squeeze every last ounce of efficiency out of a program.

    Basically, Knuth wrote two versions of Hoare's Quicksort, one without the goto (extra test in the inner loops), and one with (both implemented in assembly as well as compiled code). The increased efficiency was observed in both the assembly and compiled forms.

    If you would like to read the paper, here is a link:

    Structured Programming with goto statements

    One interesting thing I found in that paper, was that Knuth actually disproved some of these "proofs for the need of goto". Despite the name, I got the understanding that Knuth was showing that unfettered use of goto was indeed harmful, and open to abuse, but it shouldn't be abolished at the expense of programs that used it responsibly. Even then, only after the program had already been proven correct.

    Another point, beyond those suggested already in this thread and Knuth's paper, is that code generators can produce better, smaller code more efficiently with the goto, instead of trying to decide which iteration or selection statement to use. I'm not sure that is going to sway someone's opinion one way or the other, it's just one of those "ways".

    Will

  13. #13
    troublecauser
    Guest
    I'm taking a stand.

    I used to agree with the 'NEVER use goto' group. However, after seeing it frequently in open source, msdn examples and elsewhere and trying to live without it I changed my mind.

    I now realise that 'real world' code makes use of goto where appropriate.

    I now sometimes use the 'goto cleanup' line in my code. When you are using WinApi, COM or other apis where every line may fail I believe this is by far the cleanest, most efficient solution.

    Other solutions involve massive levels of if nesting or other dirty code. I've even seen a do, while loop with a break substituted for goto. This makes goto look nice.

    If anyone disagrees I challenge them to produce a function with fifteen lines of code that may fail and five lines of cleanup code that looks nicer (and is at least nearly as efficient) than the use of goto.
    Code:
    void Function() {
    	if (!function1) goto cleanup;
    	if (!function2) goto cleanup;
    	if (!function3) goto cleanup;
    	if (cond) {
    		if (!function4) goto cleanup;
    	}
    	.......
    
    cleanup:
    	cleanup line 1
    	cleanup line 2
    	cleanup line 3
    	cleanup line 4
    }
    Alternative as clean and easy to follow as this. I don't think so!
    Of course I agree that goto outside this context should be rare.

    P.S Do a search: ' "goto cleanup" php OR apache cvs ' or try this:
    http://www.google.com/search?q=goto+...ww.linuxhq.com

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>If anyone disagrees I challenge them to produce a function with fifteen lines of code that may fail and five lines of cleanup code that looks nicer (and is at least nearly as efficient) than the use of goto.


    If you are coding in windows and you want a way to ensure certain code is run before the function ends, you can use Structured Exception Handling

    Code:
    #include <iostream>
    
    const int MAX_CALLS = 10;
    
    
    void cleanup()
    {
    	std::cout << "Cleanup called" << std::endl;
    }
    
    bool bar()
    {
    	static int i = 0;
    	if(++i > MAX_CALLS - 1)
    	{
    		std::cout << "Unsuccessful call to bar" << std::endl;
    		return false;
    	}
    
    	std::cout << "Call to bar...AOK" << std::endl;
    	return true;
    }
    
    
    void foo()
    {
    	__try
    	{
    		for(int i = 0;i < MAX_CALLS;++i)
    		{
    			if(!bar())
    			{
    				return;
    			}
    		}
    	}
    	__finally
    	{
    		cleanup();
    	}
    }
    
    
    int main()
    {
          foo();
    }

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>If anyone disagrees I challenge them to produce a function with fifteen lines of code that may fail and five lines of cleanup code that looks nicer (and is at least nearly as efficient) than the use of goto.<<
    Your 5 if statements and 4 goto's can be rewritten in 1 if statement like so:
    Code:
    void Function() {
    	if (   function1 
    	    && function2
    	    && function3
    	    && cond
    	    && function4)
    	{
    	    /* Do work */
    	}
    	
      /* Clean up */
    }
    At least, I think it replicates your code, I did it a bit quick.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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