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

goto statement

Scheduled Pinned Locked Moved The Lounge
csharp
136 Posts 36 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.
  • T Tarek Elqusi

    Why many hate this statement and do not advise using it! I used it when I started programming with BASIC and GWBASIC. It is also found in the C#. Troubles are based on the programmer who is misusing it.

    G Offline
    G Offline
    GuyThiebaut
    wrote on last edited by
    #3

    I didn't even realise that C# had a goto statement! Now that I do know this I will now use it in a profligate manner - mwahahahahah!

    “That which can be asserted without evidence, can be dismissed without evidence.”

    ― Christopher Hitchens

    OriginalGriffO T L 3 Replies Last reply
    0
    • G GuyThiebaut

      I didn't even realise that C# had a goto statement! Now that I do know this I will now use it in a profligate manner - mwahahahahah!

      “That which can be asserted without evidence, can be dismissed without evidence.”

      ― Christopher Hitchens

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #4

      Noooooooooo! :laugh:

      The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • G GuyThiebaut

        I didn't even realise that C# had a goto statement! Now that I do know this I will now use it in a profligate manner - mwahahahahah!

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        T Offline
        T Offline
        Tarek Elqusi
        wrote on last edited by
        #5

        Nice, you can use it to replace all loop statements, but be careful! :)

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Because in nearly every case I have seen of it's use in C# or C++ it has been unnecessary, and only served to both confuse the code and show that the person who used it did not understand what he was doing. goto is not evil - but it is a "rule breaker" in that it violates all the principles of good code design and so using it should only be done with care. The problem is that it it taught on courses by lazy tutors as an easy way to get them started and then gets abused later because the students consider it "Normal" and don't learn to structure code well in the first place as a result. If you had grown up with GOTO as pretty much the only form of flow control (as I did) you would probably understand how easy it is to create impenetrable code with it, and why it should be discouraged until the coder is experienced enough to know when it is appropriate. About five years of "real" coding should be enough. But by then, he is probably experienced enough to know that there are probably better ways to achieve the same result...

          The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #6

          Very well put! The only goto-like statement I use is a break statement. However even at the beginning of coding in .Net I was uncomfortable using it, shaping my logic so that I did not need to use it. At university, back in 1988, I was taught never to use a goto statement - one very rare case necessitating its use in COBOL being the exception.

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          OriginalGriffO 1 Reply Last reply
          0
          • T Tarek Elqusi

            Why many hate this statement and do not advise using it! I used it when I started programming with BASIC and GWBASIC. It is also found in the C#. Troubles are based on the programmer who is misusing it.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            Under the hood almost every code block that is put into curly brackets is implemented with machine instructions for conditional or unconditional branches. GOTOs, if you want. They have just been hidden away. Unstructured spaghetti code is very hard to read and maintain. Readability and maintainability are even more important than correctness, at least in my book. Correctness will eventually follow as long as the code is readable. What I don't like is when people start religiously following rules and can even recite the reasons for them, often obviously without understanding the intentions behind them. 'Bad' code may have some other advantage than readability. If that advantage in a specific situation becomes more valuable than readability, then I would happily do what needs to be done. I also would heavily comment it to document my reasons for doing this. And the whole time I would enjoy the wailing of the code Nazis. :)

            Sent from my BatComputer via HAL 9000 and M5

            M 1 Reply Last reply
            0
            • T Tarek Elqusi

              Why many hate this statement and do not advise using it! I used it when I started programming with BASIC and GWBASIC. It is also found in the C#. Troubles are based on the programmer who is misusing it.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              In the olden day (60's and 70's mostly), goto was abused as much as (or more than) var is today. Some programmers resisted this, lead by E. Dijkstra. They gathered momentum, lost their rationality, became zealous, succeeded in converting all new and most old programmers to their Holy Cause. As a result, The Hivemind took the position that all use of goto is nothing less than Pure Evil(tm), and now prefers to use highly obfuscated but structured control flow where a simple goto would have solved the problem in a simpler way. The Hivemind, ever hypocritical, also suggests refucktoring "nested loops where you exit both of them from the inner loop" to a confusingly-named and nonsensical-on-its-own function with multiple returns, a practice that it itself also denounces.

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Because in nearly every case I have seen of it's use in C# or C++ it has been unnecessary, and only served to both confuse the code and show that the person who used it did not understand what he was doing. goto is not evil - but it is a "rule breaker" in that it violates all the principles of good code design and so using it should only be done with care. The problem is that it it taught on courses by lazy tutors as an easy way to get them started and then gets abused later because the students consider it "Normal" and don't learn to structure code well in the first place as a result. If you had grown up with GOTO as pretty much the only form of flow control (as I did) you would probably understand how easy it is to create impenetrable code with it, and why it should be discouraged until the coder is experienced enough to know when it is appropriate. About five years of "real" coding should be enough. But by then, he is probably experienced enough to know that there are probably better ways to achieve the same result...

                The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                That's the usual reasoning and perfectly ok in most cases. What if I need to squeeze out a better performance? Readability and maintainability now are secondary. I have done this often enough in the past and still do it today. Writing software is not a religion and mindlessly reciting and enforcing rules does not help at all. The rules may have good reasons, but you must always understand their goals. Break them if your goals happen to be different.

                Sent from my BatComputer via HAL 9000 and M5

                OriginalGriffO 1 Reply Last reply
                0
                • L Lost User

                  In the olden day (60's and 70's mostly), goto was abused as much as (or more than) var is today. Some programmers resisted this, lead by E. Dijkstra. They gathered momentum, lost their rationality, became zealous, succeeded in converting all new and most old programmers to their Holy Cause. As a result, The Hivemind took the position that all use of goto is nothing less than Pure Evil(tm), and now prefers to use highly obfuscated but structured control flow where a simple goto would have solved the problem in a simpler way. The Hivemind, ever hypocritical, also suggests refucktoring "nested loops where you exit both of them from the inner loop" to a confusingly-named and nonsensical-on-its-own function with multiple returns, a practice that it itself also denounces.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  harold aptroot wrote:

                  The Hivemind, ever hypocritical, also suggests refucktoring "nested loops where you exit both of them from the inner loop" to a confusingly-named and nonsensical-on-its-own function with multiple returns, a practice that it itself also denounces.

                  You mean the old story recursion vs. iteration? The stuff some eggheads traditionally use to torture students in their first semester? :)

                  Sent from my BatComputer via HAL 9000 and M5

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    That's the usual reasoning and perfectly ok in most cases. What if I need to squeeze out a better performance? Readability and maintainability now are secondary. I have done this often enough in the past and still do it today. Writing software is not a religion and mindlessly reciting and enforcing rules does not help at all. The rules may have good reasons, but you must always understand their goals. Break them if your goals happen to be different.

                    Sent from my BatComputer via HAL 9000 and M5

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #11

                    If you know what you are doing, then you have enough experience to know when it is appropriate to use. I completely agree with you: it's a useful tool. But like all tools, you have to know how and when to use it. Performance tuning (as you know well) needs more than just "quick code" - it needs a careful look at the whole of what is occurring and frequently a change of algorithm as well as hand-tuning of the code. And if you know what you are doing enough to do that, you understand the effects of your changes. Even Dijkstra said that it has it's place, but that use must be tempered with knowledge of the effects. Obligatory XKCD reference[^]

                    The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    G R T 3 Replies Last reply
                    0
                    • G GuyThiebaut

                      Very well put! The only goto-like statement I use is a break statement. However even at the beginning of coding in .Net I was uncomfortable using it, shaping my logic so that I did not need to use it. At university, back in 1988, I was taught never to use a goto statement - one very rare case necessitating its use in COBOL being the exception.

                      “That which can be asserted without evidence, can be dismissed without evidence.”

                      ― Christopher Hitchens

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #12

                      *shudder* First language I was taught, that. Then FORTRAN to follow, which was a breath of (what seemed like structured at the time) fresh air in comparison! :laugh:

                      The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      • G GuyThiebaut

                        I didn't even realise that C# had a goto statement! Now that I do know this I will now use it in a profligate manner - mwahahahahah!

                        “That which can be asserted without evidence, can be dismissed without evidence.”

                        ― Christopher Hitchens

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #13

                        You have just gained a level. Instead of blindly following some master wizard's rules, you now have begun to make your own. Before you know it you will zap your opponents with pointy pointers and even manage memory yourself. I actually prefer working with people who occasionally contemplate to break the rules instead of reciting them. :)

                        Sent from my BatComputer via HAL 9000 and M5

                        1 Reply Last reply
                        0
                        • L Lost User

                          harold aptroot wrote:

                          The Hivemind, ever hypocritical, also suggests refucktoring "nested loops where you exit both of them from the inner loop" to a confusingly-named and nonsensical-on-its-own function with multiple returns, a practice that it itself also denounces.

                          You mean the old story recursion vs. iteration? The stuff some eggheads traditionally use to torture students in their first semester? :)

                          Sent from my BatComputer via HAL 9000 and M5

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #14

                          That's fun too, but no - I mean the thing where they place the loops in a function and the goto turns into a return, and then they pretend that isn't really the same thing.

                          J 1 Reply Last reply
                          0
                          • T Tarek Elqusi

                            Why many hate this statement and do not advise using it! I used it when I started programming with BASIC and GWBASIC. It is also found in the C#. Troubles are based on the programmer who is misusing it.

                            G Offline
                            G Offline
                            Gary R Wheeler
                            wrote on last edited by
                            #15

                            I've made my living programming since 1980. I've written millions of lines of code in everything from C#, C++, C, to several assembly languages. I've been writing C# since 2008. I've never needed to use goto. I've been writing C++ since 2000. I've never needed to use goto. My C code, written from 1990 to around 2002, used goto solely for exception handling. The Microsoft and Watcom C compilers at the time didn't support exceptions in a robust fashion.

                            Tarek Elqusi wrote:

                            Troubles are based on the programmer who is misusing it

                            While that statement is true, the converse is not. I've never found a case in modern times where goto was necessary. There is always a structured alternative. If a programmer is good enough, they will not need goto.

                            Software Zen: delete this;

                            1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              Because in nearly every case I have seen of it's use in C# or C++ it has been unnecessary, and only served to both confuse the code and show that the person who used it did not understand what he was doing. goto is not evil - but it is a "rule breaker" in that it violates all the principles of good code design and so using it should only be done with care. The problem is that it it taught on courses by lazy tutors as an easy way to get them started and then gets abused later because the students consider it "Normal" and don't learn to structure code well in the first place as a result. If you had grown up with GOTO as pretty much the only form of flow control (as I did) you would probably understand how easy it is to create impenetrable code with it, and why it should be discouraged until the coder is experienced enough to know when it is appropriate. About five years of "real" coding should be enough. But by then, he is probably experienced enough to know that there are probably better ways to achieve the same result...

                              The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                              Kornfeld Eliyahu PeterK Offline
                              Kornfeld Eliyahu PeterK Offline
                              Kornfeld Eliyahu Peter
                              wrote on last edited by
                              #16

                              You mean you are 'grown up'! Can't believe that... :laugh: But as for the rest I agree with you - don't break rules just for breaking them...

                              I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is (V).

                              "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                              OriginalGriffO 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                If you know what you are doing, then you have enough experience to know when it is appropriate to use. I completely agree with you: it's a useful tool. But like all tools, you have to know how and when to use it. Performance tuning (as you know well) needs more than just "quick code" - it needs a careful look at the whole of what is occurring and frequently a change of algorithm as well as hand-tuning of the code. And if you know what you are doing enough to do that, you understand the effects of your changes. Even Dijkstra said that it has it's place, but that use must be tempered with knowledge of the effects. Obligatory XKCD reference[^]

                                The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                                G Offline
                                G Offline
                                Gary R Wheeler
                                wrote on last edited by
                                #17

                                OriginalGriff wrote:

                                it needs a careful look at the whole of what is occurring and frequently a change of algorithm as well as hand-tuning of the code

                                I've done some minor bits of optimization on occasion. I've never needed goto as part of any hand-tuning. Algorithm improvements and refactoring are generally the way to go for me.

                                Software Zen: delete this;

                                S 1 Reply Last reply
                                0
                                • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                                  You mean you are 'grown up'! Can't believe that... :laugh: But as for the rest I agree with you - don't break rules just for breaking them...

                                  I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is (V).

                                  OriginalGriffO Offline
                                  OriginalGriffO Offline
                                  OriginalGriff
                                  wrote on last edited by
                                  #18

                                  Good grief no! I'm just old.

                                  The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                  1 Reply Last reply
                                  0
                                  • T Tarek Elqusi

                                    Why many hate this statement and do not advise using it! I used it when I started programming with BASIC and GWBASIC. It is also found in the C#. Troubles are based on the programmer who is misusing it.

                                    A Offline
                                    A Offline
                                    altomaltes
                                    wrote on last edited by
                                    #19

                                    Ahh... This is one of my favourite subjects. I think we went to the total unbalance of the overpopulation of gotos of BASIC to the total unbalance of goto starvation on structured languajes. A goto is perfect for some weird situations. Imagine your fiancee calls you at work, because you've won severals milions on LOTO. May be your exit from work will be no too structured, and so must be. In others words goto deals well with some fatal error situatios, a glance at linux kernel code shows this. What? I'm getting seriuous. goto minibar.

                                    P P 2 Replies Last reply
                                    0
                                    • A altomaltes

                                      Ahh... This is one of my favourite subjects. I think we went to the total unbalance of the overpopulation of gotos of BASIC to the total unbalance of goto starvation on structured languajes. A goto is perfect for some weird situations. Imagine your fiancee calls you at work, because you've won severals milions on LOTO. May be your exit from work will be no too structured, and so must be. In others words goto deals well with some fatal error situatios, a glance at linux kernel code shows this. What? I'm getting seriuous. goto minibar.

                                      P Offline
                                      P Offline
                                      Pablo Aliskevicius
                                      wrote on last edited by
                                      #20

                                      If you don't think that GOTO is an interesting topic of conversation, try the Duff device. http://www.lysator.liu.se/c/duffs-device.html[^]

                                      Pablo. "Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899). "You are to act in the light of experience as guided by intelligence" (Rex Stout, "In the Best Families", 1950).

                                      P T A 3 Replies Last reply
                                      0
                                      • P Pablo Aliskevicius

                                        If you don't think that GOTO is an interesting topic of conversation, try the Duff device. http://www.lysator.liu.se/c/duffs-device.html[^]

                                        Pablo. "Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899). "You are to act in the light of experience as guided by intelligence" (Rex Stout, "In the Best Families", 1950).

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

                                        :thumbsup: I'd never seen that before. Awesome.

                                        1 Reply Last reply
                                        0
                                        • A altomaltes

                                          Ahh... This is one of my favourite subjects. I think we went to the total unbalance of the overpopulation of gotos of BASIC to the total unbalance of goto starvation on structured languajes. A goto is perfect for some weird situations. Imagine your fiancee calls you at work, because you've won severals milions on LOTO. May be your exit from work will be no too structured, and so must be. In others words goto deals well with some fatal error situatios, a glance at linux kernel code shows this. What? I'm getting seriuous. goto minibar.

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

                                          altomaltes wrote:

                                          your exit from work will be no too structured

                                          That's an interrupt.

                                          A 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