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; OK, i know it's concidered bad practace to use GOTO. but why?...

  1. #1
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87

    goto, why not

    OK, i know it's concidered bad practace to use GOTO. but why?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    It allows you to jump anywhere in the code of the project back, forward, this way, thatway. Its not that it doent work, its just it makes the code extreamly difficult to read, even if you are the original coder.......

    Its funny that whatever book you read on C/C++, they all dedicate a section as to why goto should not be used......I've seen the same in a Delphi book too...... I guess the reason why its still supported is that it works ok...i'ts just deemed bad practice....

  3. #3
    Registered User rick barclay's Avatar
    Join Date
    Aug 2001
    Posts
    835
    GOTO leads to spaghetti code.

    The only place I see goto anymore is action script, where your
    code can't turn into pasta.

    rick barclay
    No. Wait. Don't hang up!

    This is America calling!

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    It allows you to jump anywhere in the code of the project back, forward, this way, thatway.
    Not true....

    Goto only allows you to go to a label that is in the same function as the goto statement.

    It can be useful to escape deeply nested loops but has little or no other uses.
    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

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I don't think there is anything wrong with goto. I use it just as I would with any other code. I find it quite convenient. It is an easy way to 'jump'.

    --Garfield
    1978 Silver Anniversary Corvette

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    goto is okay as long as you don't overuse it and complicate the code. Personally I don't use it because I prefer to come up with more creative solutions to my problems so that I'll know another way to do it later on.
    C code. C code run. Run code, run...please!

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>Goto only allows you to go to a label that is in the same function as the goto statement.


    Really...didnt know that it goes out of scope.....suppose thats cuz the books dont give it coverage...

  8. #8
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    I've heard that a goto statement is exactly the same jmp in assembly, so, by using goto, you're making your program even more low-level (which can be good or not), but I learnt programming without goto, it's more challenging.

    Oskilian

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Well you can still can program structured programs with
    goto. You just have to be careful that you aren't jumping everywhere.

    So here would be a while loop

    Code:
     
         goto cond;
    loop:
         printf("Hello World!\n");
          x++;
    cond:
          if (x < 5) 
                 goto loop;
    This is only useful when you don't have a while loop
    in a language such as assembly.

  10. #10
    31173 h4x0r gnu-ehacks's Avatar
    Join Date
    Nov 2001
    Posts
    556
    Here are the reasons why I don't use gotos.

    First of all, something is wrong with my compiler, and it won't work correctly, so I can't.

    Second of all, if you wanted to call to a different location, just use a function call. Functions do just as much as gotos, and more.

    Third of all, if I wanted to loop, I'll do it the old fashioned way with a do, while, or for loop.
    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

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    First of all, something is wrong with my compiler, and it won't work correctly, so I can't
    Get a different compiler.
    Second of all, if you wanted to call to a different location, just use a function call. Functions do just as much as gotos, and more.
    Functions when finished return control back to the caller. Goto is an unconditional jump.Totally different.

    know a better way of doing this....

    for(......)
    for(......)
    for(......)
    for(......)
    {
    if(suchandsuch) goto OutOfLoop;
    ....
    ....
    ....
    }
    OutOfLoop : // do stuff
    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

  12. #12
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    It leads to code that jumps around - yes they may be labelled but if you have 8000 lines of code it is difficult to trace.
    It makes debugging very hard - when you use disassemblers and are faced with endless jmp commands

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    It leads to code that jumps around - yes they may be labelled but if you have 8000 lines of code it is difficult to trace.
    you are missing the point. with goto you can only jump to somewhere else that is within the same function as the goto. Used sparingly it is a powerful and helpful tool.It is not a keyword that i would like to see removed from the language. debugging is not made difficult because if you cant logically follow a function then chances are that its too large anyway and in need of further modularization.

    It makes debugging very hard - when you use disassemblers and are faced with endless jmp commands
    So you wouldn't like to see while loops, or do while loops or for loops etc. Do you think these are implemented in assembly any other way than with jmp or related instructions?
    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

  14. #14
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Exclamation

    Goto function is not good, beacuse source code is not easily "scanned".
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  15. #15
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Why not? If you have a line like this:

    ...code...
    goto label;
    ...code...

    And you absolutely can't find the "label", then there is a nice function to search for a word through source. Not too hard.

    --Garfield
    1978 Silver Anniversary Corvette

Page 1 of 3 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, 08: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, 08: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, 05:21 PM

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