goto, why not

This is a discussion on goto, why not within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I think goto statements are okay, just as long as they are not too overly used. Because if they are, ...

  1. #16
    31173 h4x0r gnu-ehacks's Avatar
    Join Date
    Nov 2001
    Posts
    556
    I think goto statements are okay, just as long as they are not too overly used. Because if they are, then it leads to crazy code. Also, the goto should be close to the label, so you don't have to look through thousands of lines of code to find the label.
    What will people say if they hear that I'm a Jesus freak?
    What will people do if they find that it's true?
    I don't really care if they label me a Jesus freak, there is no disguising the truth!

    Jesus Freak, D.C. Talk

    -gnu-ehacks

  2. #17
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, I'd say a limit of 3 goto's in one source (.c) file.
    1978 Silver Anniversary Corvette

  3. #18
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    857
    Yeah, I agree that goto makes the code hard to understand, but it is the only way for me to get out of the code.
    -------------------
    322reenignE
    Yoshi

  4. #19
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    You have to write progs without goto function. It is possible!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #20
    Sayeh
    Guest
    You don't throw a tool away just because you don't a) know what it's for, or b) how to properly use it.

    'goto' is a necessary and important part of the C/C++ language. The whole anti-goto mindset stemmed from so-called 'purist' pascal programmers, back in the day.

    That cr@p wasn't appreciated then, and still isn't now. If you aren't smart enough to understand the importance to 'goto' in the C language, then you are still either a novice or an amateur developer.

  6. #21
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Once again Sayeh, we bow at your feet...........

  7. #22
    31173 h4x0r gnu-ehacks's Avatar
    Join Date
    Nov 2001
    Posts
    556
    Well said Sayeh. But, if goto's are used TOO much, then it will just lead to wierd code...Although sometimes I use them. I try not to use them too much though.
    What will people say if they hear that I'm a Jesus freak?
    What will people do if they find that it's true?
    I don't really care if they label me a Jesus freak, there is no disguising the truth!

    Jesus Freak, D.C. Talk

    -gnu-ehacks

  8. #23
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    I disagree with you, I think that you have a program that uses GOTO's, or you have a program which doesn't, but there is no such thing as a 'tolerable' limit to the amount of GOTO's

    GOTO's are low-level, but they still suck, and Big time, so, I never use GOTO's, and if I have a problem whose only solution is using GOTO (I still haven't found one), I would say it's impossible and give up before trying GOTO

    the only time I use GOTO is in VB to deactivate errors (I son't use this one too often, I'd rather deactivate the error lookup, not all errors) like this:

    On Error Goto 0

    but I normally use

    On Error Resume Next

    Oskilian

  9. #24
    31173 h4x0r gnu-ehacks's Avatar
    Join Date
    Nov 2001
    Posts
    556
    Well, let's say you've written a REALLY long piece of code. Now, suppose that you just realised that you are going to have to loop it. You don't want to build a while, for, or do loop, because the conditional statement exists within the middle of the entire section you would like to loop. Now, this is part of a larger function, so you can't just call the whole function over again. Suppose this happens, you will most definitely need to use a goto.
    What will people say if they hear that I'm a Jesus freak?
    What will people do if they find that it's true?
    I don't really care if they label me a Jesus freak, there is no disguising the truth!

    Jesus Freak, D.C. Talk

    -gnu-ehacks

  10. #25
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499

    Ah the goto!!

    I think a main reason the goto is looked down upon today is because it is rarely needed. Older versions of BASIC and FORTRAN were dependent upon this statement. Today, with more powerful languages (C/C++) we have the tools to replace the goto statement with much easier methods in ALMOST any situation. Easier to understand as well as code.

    The only situation that I have ever seen where you would need to use the goto in C is to escape the nasty clutches of a nested set of loops when something goes wrong. The break and continue statements are just specialized forms of the goto, but neither will help you in this situation.

    I think that K&R said it best when they said that this statement is "infinitely abusable" and should "be used sparingly if at all".

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    22
    Because some guy proved that all code that "thought" they needed goto could actually be written without them. It was his college thesis, and they more than accepted it.

    After all, the while loop is just a special goto, if you think about it.

    Every case mentioned here by people can be done without the use of goto. EVERY CASE

    -Vulcan

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    22
    Originally posted by Sayeh
    You don't throw a tool away just because you don't a) know what it's for, or b) how to properly use it.

    'goto' is a necessary and important part of the C/C++ language. The whole anti-goto mindset stemmed from so-called 'purist' pascal programmers, back in the day.

    That cr@p wasn't appreciated then, and still isn't now. If you aren't smart enough to understand the importance to 'goto' in the C language, then you are still either a novice or an amateur developer.
    Thats not true. Goto is for lazy programmers who don't program the right way the first time. There is no case when goto should be used. EVER

    -Vulcan

  13. #28
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> Well, let's say you've written a REALLY long piece of code.

    I would suggest that this is indicative of a poor design and that you are using a poor language construct to get around it.

    I have never used a goto statement in a C or C++ program. The last language I worked in where I used a goto statement was FORTRAN 66 in the late 70's. After FORTRAN 77 arrived, I never had need of it again.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #29
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Thats not true. Goto is for lazy programmers who don't program the right way the first time. There is no case when goto should be used. EVER
    You are soooooo wrong there.

    If you want to quickly exit deeply nested loops there is NO BETTER WAY!

    look....

    for(.....)
    for(.....)
    for(.....)
    for(.....)
    {
    if(something) goto label;
    .
    .
    .
    }

    label: .......

    without the goto you would have to do this.....
    for(.....)
    {
    for(.....)
    {
    for(.....)
    {
    for(.....)
    {
    if(something) break;
    .
    .
    .
    .
    }
    if(something) break;
    }
    if(something) break;
    }
    if(something) break;
    }
    .
    .
    .


    Now which version produces the tighter faster code..... goto.
    which version is easier to read and follow ...... goto

    The defense rests!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #30
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> Now which version produces the tighter faster code..... goto.

    I would expect a decent optimising compiler to produce the same, or at least very similar results.

    I would not code the nested loops in the way you have. I think I would probably use a while() and have a boolean flag which I could set at any point in any of the loops to signal a total escape. Doing the same with goto would require at least one goto in each of the loops.

    Continue = TRUE;
    while(Continue)
    while(Continue)
    while(Continue)
    while(Continue)
    {
    if(something) Continue = FALSE;
    .
    .
    .
    }

    I do not believe goto is necessary. At my current employer, use of the goto statement would violate the installation standards and hence fail the quality departments tests.

    Err, the "other" defence rests.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Page 2 of 3 FirstFirst 123 LastLast
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, 11:32 AM
  3. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 09: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

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