Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. goto statement [moved]

goto statement [moved]

Scheduled Pinned Locked Moved The Lounge
questiondiscussion
27 Posts 16 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C CPallini

    In fact, I use it outside the programs: "Goto hell!" is one of my favourite expressions. Am I wrong? :-D

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #6

    outside programming languages "goto" takes two words, isn't it?

    Luc Pattyn [Forum Guidelines] [My Articles]


    this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


    C 1 Reply Last reply
    0
    • L Luc Pattyn

      outside programming languages "goto" takes two words, isn't it?

      Luc Pattyn [Forum Guidelines] [My Articles]


      this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #7

      Well, I think the message is clear anyway...:-D

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      1 Reply Last reply
      0
      • C CPallini

        RomTibi wrote:

        Is it wrong to use goto into a program?

        Usually you should avoid using goto but it isn't a dogma.

        RomTibi wrote:

        Why?

        RomTibi wrote:

        When?

        See, for instance, here [^]. :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        F Offline
        F Offline
        FyreWyrm
        wrote on last edited by
        #8

        Here's a good reason why not to use goto statements. http://xkcd.com/292/[^]

        C 1 Reply Last reply
        0
        • F FyreWyrm

          Here's a good reason why not to use goto statements. http://xkcd.com/292/[^]

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #9

          :-D:laugh::-D

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          U 1 Reply Last reply
          0
          • R RomTibi

            Is it wrong to use goto into a program? Why? When?:)

            36. When you surround an army, leave an outlet free. ... Do not press a desperate foe too hard. SUN-TZU - Art of War

            Moved by Admin on Saturday, December 08, 2007 6:11:58 PM. This is not a programming question. This is a programming discussion

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #10

            Yes, but not as wrong as asking a programming question in a non-programming forum.

            C D 2 Replies Last reply
            0
            • R RomTibi

              Is it wrong to use goto into a program? Why? When?:)

              36. When you surround an army, leave an outlet free. ... Do not press a desperate foe too hard. SUN-TZU - Art of War

              Moved by Admin on Saturday, December 08, 2007 6:11:58 PM. This is not a programming question. This is a programming discussion

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #11

              This forum is for discussing coding horrors that one may have run across. goto's have the tendency to make code very unreadable if abused.

              "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

              1 Reply Last reply
              0
              • P PIEBALDconsult

                Yes, but not as wrong as asking a programming question in a non-programming forum.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #12

                PIEBALDconsult wrote:

                non-programming forum.

                :confused: :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                1 Reply Last reply
                0
                • N NimitySSJ

                  It is generally considered wrong to use goto. This is because widespread use of goto resulted in unreadable "spaghetti" code, thanks to lazy programmers. Lot's of things act funny when goto's are around, such as OOP, side effects, type checking, etc. If used improperly, it can have very weird effects. That said, I've used goto statements for a long time in certain situations, because they were far more readable. For instance, instead of using a bunch of variables, conditionals, etc. to exit a program under certain circumstances, I would just use a goto. One line to one label. Done. (Note: I took care of cleanup and stuff too). However, I recently stopped using it for one reason: compilers can't optimize properly. In his great book "Write Great Code Vol. 2," Randall Hyde delves deep into how a program is transformed into assembler (i.e. processor instructions), and especially focuses on what effects certain tactics, data structures, and algorithms cause. He notes that, due to how compilers analyze your code, you want to keep it from "branching" (i.e. jump from line A to line B, like a conditional might) as much as possible. Goto's are so unpredictable and hard to analyze that they defeat the compiler's optimization technology. So, I might have used goto to make my program more efficient, but I lost the critical optimization phase, resulting in code that was slower. So, regardless of the religious wars about goto, there is one empirical consideration: if you want your compiler to generate the best code it can, don't use goto. (Hence, I bid farewell to the ever-useful construct) If you don't care about a possible performance loss, use it very carefully and sparingly. Also, watch your goto's whenever you update your source code: gotos' control flows can be disrupted, and your program could fail. -Nick P.

                  T Offline
                  T Offline
                  Tony Wesley
                  wrote on last edited by
                  #13

                  NimitySSJ wrote:

                  He notes that, due to how compilers analyze your code, you want to keep it from "branching" (i.e. jump from line A to line B, like a conditional might) as much as possible. Goto's are so unpredictable and hard to analyze that they defeat the compiler's optimization technology.

                  Nick, I'm going to disagree with you here. goto's are completely predictable and unconditional. Consider the following psuedo-code:

                  if (a > b)
                  then
                  c = a
                  else
                  c = b
                  endif
                  '
                  '
                  if (a > b) goto 10 else goto 20
                  10 c = a
                  goto 30
                  20 c = b
                  30 continue

                  I contend that there is no difference in what the compiler can do in optimization. The difference is the human readability. While I've made the case here recently that some limited use of goto's are okay, I certainly never again want to deal with code that looks like the second case above. The one thing that you said that I will agree with 100% is this:

                  use it [goto] very carefully and sparingly

                  1 Reply Last reply
                  0
                  • N NimitySSJ

                    It is generally considered wrong to use goto. This is because widespread use of goto resulted in unreadable "spaghetti" code, thanks to lazy programmers. Lot's of things act funny when goto's are around, such as OOP, side effects, type checking, etc. If used improperly, it can have very weird effects. That said, I've used goto statements for a long time in certain situations, because they were far more readable. For instance, instead of using a bunch of variables, conditionals, etc. to exit a program under certain circumstances, I would just use a goto. One line to one label. Done. (Note: I took care of cleanup and stuff too). However, I recently stopped using it for one reason: compilers can't optimize properly. In his great book "Write Great Code Vol. 2," Randall Hyde delves deep into how a program is transformed into assembler (i.e. processor instructions), and especially focuses on what effects certain tactics, data structures, and algorithms cause. He notes that, due to how compilers analyze your code, you want to keep it from "branching" (i.e. jump from line A to line B, like a conditional might) as much as possible. Goto's are so unpredictable and hard to analyze that they defeat the compiler's optimization technology. So, I might have used goto to make my program more efficient, but I lost the critical optimization phase, resulting in code that was slower. So, regardless of the religious wars about goto, there is one empirical consideration: if you want your compiler to generate the best code it can, don't use goto. (Hence, I bid farewell to the ever-useful construct) If you don't care about a possible performance loss, use it very carefully and sparingly. Also, watch your goto's whenever you update your source code: gotos' control flows can be disrupted, and your program could fail. -Nick P.

                    D Offline
                    D Offline
                    Daniel Grunwald
                    wrote on last edited by
                    #14

                    Most optimizations are done on intermediate representations of the code where high-level constructs like loops, if-else etc. no longer exist - those are translated to "goto"s. Compilers surely have no problem optimizing it.

                    1 Reply Last reply
                    0
                    • R RomTibi

                      Is it wrong to use goto into a program? Why? When?:)

                      36. When you surround an army, leave an outlet free. ... Do not press a desperate foe too hard. SUN-TZU - Art of War

                      Moved by Admin on Saturday, December 08, 2007 6:11:58 PM. This is not a programming question. This is a programming discussion

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #15

                      Just by posting the question in the Coding Horrors forum you've told us that you think it's wrong. I haven't used a Goto in, probably, 7-8 years. I really see no use for them any more.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      1 Reply Last reply
                      0
                      • N NimitySSJ

                        It is generally considered wrong to use goto. This is because widespread use of goto resulted in unreadable "spaghetti" code, thanks to lazy programmers. Lot's of things act funny when goto's are around, such as OOP, side effects, type checking, etc. If used improperly, it can have very weird effects. That said, I've used goto statements for a long time in certain situations, because they were far more readable. For instance, instead of using a bunch of variables, conditionals, etc. to exit a program under certain circumstances, I would just use a goto. One line to one label. Done. (Note: I took care of cleanup and stuff too). However, I recently stopped using it for one reason: compilers can't optimize properly. In his great book "Write Great Code Vol. 2," Randall Hyde delves deep into how a program is transformed into assembler (i.e. processor instructions), and especially focuses on what effects certain tactics, data structures, and algorithms cause. He notes that, due to how compilers analyze your code, you want to keep it from "branching" (i.e. jump from line A to line B, like a conditional might) as much as possible. Goto's are so unpredictable and hard to analyze that they defeat the compiler's optimization technology. So, I might have used goto to make my program more efficient, but I lost the critical optimization phase, resulting in code that was slower. So, regardless of the religious wars about goto, there is one empirical consideration: if you want your compiler to generate the best code it can, don't use goto. (Hence, I bid farewell to the ever-useful construct) If you don't care about a possible performance loss, use it very carefully and sparingly. Also, watch your goto's whenever you update your source code: gotos' control flows can be disrupted, and your program could fail. -Nick P.

                        C Offline
                        C Offline
                        cp9876
                        wrote on last edited by
                        #16

                        NimitySSJ wrote:

                        It is generally considered wrong to use goto. This is because widespread use of goto resulted in unreadable "spaghetti" code, thanks to lazy programmers.

                        Agreed, but from here on I have some problems:

                        NimitySSJ wrote:

                        Lot's of things act funny when goto's are around, such as OOP, side effects, type checking, etc.

                        This is preaching fire and brimstone - do it my way or else you will go to hell. I am not aware of any interaction of goto statements with OOP or type checking, that would be different to any other flow control statement.

                        NimitySSJ wrote:

                        However, I recently stopped using it for one reason: compilers can't optimize properly.

                        I doubt this as well - others have suggested reasons. However, the few times I have used goto statements (and it is nearly always the situation already mentioned - a function with complex flow control but needing common exit / cleanup code) I use it not for performance but to ensure the the accuracy of the logic and to minimize the chance of introducing bugs. In many applications these days, a few machine cycles is less important that reducing bugs. We need to teach the novices that all the features in the language are available and most have their place.


                        Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                        1 Reply Last reply
                        0
                        • R RomTibi

                          Is it wrong to use goto into a program? Why? When?:)

                          36. When you surround an army, leave an outlet free. ... Do not press a desperate foe too hard. SUN-TZU - Art of War

                          Moved by Admin on Saturday, December 08, 2007 6:11:58 PM. This is not a programming question. This is a programming discussion

                          P Offline
                          P Offline
                          Philip Laureano
                          wrote on last edited by
                          #17

                          RomTibi wrote:

                          Is it wrong to use goto into a program? Why?

                          Never use goto statements if the language of your choice has higher level conditional constructs, such as "if" statements. The only time I would ever condone the use of a "goto" (or its equivalent) is if you're writing in IL. Even then, I still wouldn't recommend using the OpCodes.Br instruction :P

                          L 1 Reply Last reply
                          0
                          • P Philip Laureano

                            RomTibi wrote:

                            Is it wrong to use goto into a program? Why?

                            Never use goto statements if the language of your choice has higher level conditional constructs, such as "if" statements. The only time I would ever condone the use of a "goto" (or its equivalent) is if you're writing in IL. Even then, I still wouldn't recommend using the OpCodes.Br instruction :P

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #18

                            Philip Laureano wrote:

                            I still wouldn't recommend using the OpCodes.Br instruction

                            I haven't seen a Visual Studio generated method yet without a silly BR.S inside; even in a Release build they typically end on something like:

                            IL_0013: /* 0B | */ stloc.1
                            IL_0014: /* 2B | 00 */ br.s IL_0016
                            IL_0016: /* 07 | */ ldloc.1
                            IL_0017: /* 2A | */ ret

                            :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


                            D 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Philip Laureano wrote:

                              I still wouldn't recommend using the OpCodes.Br instruction

                              I haven't seen a Visual Studio generated method yet without a silly BR.S inside; even in a Release build they typically end on something like:

                              IL_0013: /* 0B | */ stloc.1
                              IL_0014: /* 2B | 00 */ br.s IL_0016
                              IL_0016: /* 07 | */ ldloc.1
                              IL_0017: /* 2A | */ ret

                              :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


                              D Offline
                              D Offline
                              Daniel Grunwald
                              wrote on last edited by
                              #19

                              I have written an experimental IL optimizer, your code would result it:

                              IL_0013: ret

                              :) It's main purpose is to inline LINQ methods and then optimize away the overhead of the delegate/anonymous method; but that's not finished yet. Unfortunately, I don't have the time to continue working on the optimizer, and the current version has some optimization bugs that can introduce subtle bugs in the optimized code.

                              L 1 Reply Last reply
                              0
                              • D Daniel Grunwald

                                I have written an experimental IL optimizer, your code would result it:

                                IL_0013: ret

                                :) It's main purpose is to inline LINQ methods and then optimize away the overhead of the delegate/anonymous method; but that's not finished yet. Unfortunately, I don't have the time to continue working on the optimizer, and the current version has some optimization bugs that can introduce subtle bugs in the optimized code.

                                L Offline
                                L Offline
                                Luc Pattyn
                                wrote on last edited by
                                #20

                                Hi Daniel, an optimizer would be great to have built-in to Visual Studio's compilers I guess. I just recently started looking at MSIL code and I was not much impressed so far. Would Microsoft keep back on optimizing it because they rely on the JIT to optimize at its level anyway? BTW: can you tell me an easy way to look at native code, as generated by the JIT, on a program, say running from inside Visual Studio? Regards,

                                Luc Pattyn [Forum Guidelines] [My Articles]


                                this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


                                1 Reply Last reply
                                0
                                • R RomTibi

                                  Is it wrong to use goto into a program? Why? When?:)

                                  36. When you surround an army, leave an outlet free. ... Do not press a desperate foe too hard. SUN-TZU - Art of War

                                  Moved by Admin on Saturday, December 08, 2007 6:11:58 PM. This is not a programming question. This is a programming discussion

                                  V Offline
                                  V Offline
                                  Vasudevan Deepak Kumar
                                  wrote on last edited by
                                  #21

                                  Goto is normally discouraged because it violates a good program (control) flow and makes debugging/troubleshooting a bit difficult and sometimes a nightmare.

                                  Vasudevan Deepak Kumar Personal Homepage
                                  Tech Gossips
                                  A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

                                  1 Reply Last reply
                                  0
                                  • C CPallini

                                    :-D:laugh::-D

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                                    U Offline
                                    U Offline
                                    User 4688356
                                    wrote on last edited by
                                    #22

                                    see using goto makes your program less structured and makes use of no logic...it must be avoided

                                    C 1 Reply Last reply
                                    0
                                    • U User 4688356

                                      see using goto makes your program less structured and makes use of no logic...it must be avoided

                                      C Offline
                                      C Offline
                                      CPallini
                                      wrote on last edited by
                                      #23

                                      Member 4691687 wrote:

                                      using goto makes your program less structured

                                      Maybe. But it really helps to rollback in particular circumstances.

                                      Member 4691687 wrote:

                                      nd makes use of no logic...

                                      Nope. Program logic heavily relies to developer's one. :-D

                                      Member 4691687 wrote:

                                      it must be avoided

                                      I don't like design dogmas. :)

                                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                                      1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        Yes, but not as wrong as asking a programming question in a non-programming forum.

                                        D Offline
                                        D Offline
                                        David St Hilaire
                                        wrote on last edited by
                                        #24

                                        How about if I try to help him out with this code sample I came across from someone who was apparently trying to avoid using a goto? I don't have access to the actual function anymore, but the basic idea went: do { //some code here if ([condition]) break; //some more code here if ([condition]) break; //this goes on for awhile.... if ([condition]) break; // ... } while (false); // clean up code

                                        1 Reply Last reply
                                        0
                                        • R RomTibi

                                          Is it wrong to use goto into a program? Why? When?:)

                                          36. When you surround an army, leave an outlet free. ... Do not press a desperate foe too hard. SUN-TZU - Art of War

                                          Moved by Admin on Saturday, December 08, 2007 6:11:58 PM. This is not a programming question. This is a programming discussion

                                          D Offline
                                          D Offline
                                          Delphi4ever
                                          wrote on last edited by
                                          #25

                                          In the first programming course I had, the textbook described GOTO as "evil"! EEEEeeeEEEviiiil! :mad: If you used GOTO, GOSUB or global variables, you flunked. That was made clear from the beginning. If you used the three "G"'s, you had not understod functions and you failed the course. I have never used the three "G" ever since. I think it makes the code hard to read. I so hate spagetti code. A function has some input and generate some output. Very simple. No globals comming down from heaven above. Put them in the function parameters. If there are many parameters, just organize them in a struct. If there are lots of data (like a picture) make a pointer and call-by-reference. The only exeption is const Pi=3.14. That may be global, but only in the math module.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups