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 loops

goto loops

Scheduled Pinned Locked Moved The Lounge
csharpquestion
88 Posts 45 Posters 1 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.
  • P Pete OHanlon

    10 Print "This Code is useless."
    20 Goto 10

    "WPF has many lovers. It's a veritable porn star!" - Josh Smith

    As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

    My blog | My articles | MoXAML PowerToys | Onyx

    J Offline
    J Offline
    Johnny J
    wrote on last edited by
    #36

    Very meaningful! I guess you got me there... ;P

    1 Reply Last reply
    0
    • J Johnny J

      Strange - when I post a comment along those lines, I get downvoted, but when you do it gets upvoted. Makes me think that some people here go more for the person than the message! Isn't the first time I have noticed that, though...

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #37

      Well, he has a .45 in reach. Makes everybody agree with you.

      A 1 Reply Last reply
      0
      • J Johnny J

        Strange - when I post a comment along those lines, I get downvoted, but when you do it gets upvoted. Makes me think that some people here go more for the person than the message! Isn't the first time I have noticed that, though...

        R Offline
        R Offline
        realJSOP
        wrote on last edited by
        #38

        I got 1-voted, and probably from the same person that 1-voted you. I edited it because I thought that's why I was 1-voted - I had neglected to mention the one valid goto everyone uses. Of course, the 1-voters can also goto hell for all I care.

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        J R 2 Replies Last reply
        0
        • R realJSOP

          I got 1-voted, and probably from the same person that 1-voted you. I edited it because I thought that's why I was 1-voted - I had neglected to mention the one valid goto everyone uses. Of course, the 1-voters can also goto hell for all I care.

          .45 ACP - because shooting twice is just silly
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

          J Offline
          J Offline
          Johnny J
          wrote on last edited by
          #39

          5 for that...

          1 Reply Last reply
          0
          • R realJSOP

            I got 1-voted, and probably from the same person that 1-voted you. I edited it because I thought that's why I was 1-voted - I had neglected to mention the one valid goto everyone uses. Of course, the 1-voters can also goto hell for all I care.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            R Offline
            R Offline
            Rage
            wrote on last edited by
            #40

            John Simmons / outlaw programmer wrote:

            goto hell

            Hell ?[^]

            C 1 Reply Last reply
            0
            • J Johnny J

              Wasn't me even though I don't agree with you. But since you are so dead set I'm wrong, I challenge you: Show me ONE piece of code where a GoTo has a meaningful use!

              M Offline
              M Offline
              moon_stick
              wrote on last edited by
              #41

              I think the nested 'for' loop example is meaningful enough: for (int i = 0; i < I_BOUND; i++) { for (int j = 0; j < J_BOUND; j++) { if (MyTestingFunction(i, j, k)) { goto LoopExit; } } } LoopExit: Steve McConnell gives a few other examples here[^]. To be clear, I'm not saying there aren't other ways of doing this (as there clearly are), and I don't believe there are any situations where using a goto provides funtionality above and beyond other native constructs (though am willing to be proved wrong!). I just don't agree that you can categorically state that use of goto is *always* wrong.

              Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

              J 1 Reply Last reply
              0
              • M moon_stick

                I think the nested 'for' loop example is meaningful enough: for (int i = 0; i < I_BOUND; i++) { for (int j = 0; j < J_BOUND; j++) { if (MyTestingFunction(i, j, k)) { goto LoopExit; } } } LoopExit: Steve McConnell gives a few other examples here[^]. To be clear, I'm not saying there aren't other ways of doing this (as there clearly are), and I don't believe there are any situations where using a goto provides funtionality above and beyond other native constructs (though am willing to be proved wrong!). I just don't agree that you can categorically state that use of goto is *always* wrong.

                Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

                J Offline
                J Offline
                Johnny J
                wrote on last edited by
                #42

                It's probably easier, but I still don't think it's a good use in this case. Sort of like exiting a room through the window. I'm not an expert on this, but can you be sure that jumping out of a nested loop like that doesn't leave something not cleaned up properly, stack e.g.?

                M L 2 Replies Last reply
                0
                • L LucianPopescu

                  Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

                  S Offline
                  S Offline
                  Smithers Jones
                  wrote on last edited by
                  #43

                  Never ever use goto!! You have been warned.[^] :)

                  "I love deadlines. I like the whooshing sound they make as they fly by." (DNA)

                  1 Reply Last reply
                  0
                  • J Johnny J

                    It's probably easier, but I still don't think it's a good use in this case. Sort of like exiting a room through the window. I'm not an expert on this, but can you be sure that jumping out of a nested loop like that doesn't leave something not cleaned up properly, stack e.g.?

                    M Offline
                    M Offline
                    moon_stick
                    wrote on last edited by
                    #44

                    It depends on the context - if MyTestingFunction() is expensive to run then an intelligent way to exit the loop early is a pretty good reason, especially if the bounds of the data structure being traversed are very large. The variables in the loops are scoped by the braces so by the time the exit label is hit, the variables are out of scope and the GC can do its thing. Assuming that MyTestingFunction() is stateless and isn't dealing with unmanaged resources, I don't believe there's a memory leak issue here.

                    Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.

                    1 Reply Last reply
                    0
                    • L LucianPopescu

                      Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

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

                      I have used goto in C# when breaking out of nested loops, and rarely in switch blocks to avoid copying one case to the end of some other case (copypasta is worse than goto IMO). And I will hear no "nested loops should be refactored anyway" - it was performance code and performance code is not meant to look nice. In assembly I use the equivalent of goto all the time. There is no alternative.

                      C 1 Reply Last reply
                      0
                      • L LloydA111

                        What do you not understand about "Please do not post programming questions here"?


                        ▬▬▬▬▬▬▬▬▬▬▬▬

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #46

                        It isn't a programming question. He's just asking for opinions about usage of a statement (warning: related to programming). Ya know? Other than important things like football, bullshit politics, stiupid dradunk bdahbble, cricket, etc., people should be let free to discuss something related to programming now and then.

                        It's time for a new sig. Seriously.

                        P 1 Reply Last reply
                        0
                        • R Rage

                          pompeyboy3 wrote:

                          to greater effect than other soloutions although I must confess I can see better ways to code some of the examples

                          to greater effect than the other solutions he provided, but not greater than real good solutions. As you started to point out, you can write his example in perfectly good code without requiring to the goto instruction. IMHO, this blog entry is rather poor.

                          S Offline
                          S Offline
                          Steve Wellens
                          wrote on last edited by
                          #47

                          There are always other solutions. The blog was pointing out the in rare circumstances goto is the best solution....if you define best as also the simplest solution. And yes, goto can be abused just like any other element in any language. I've seen multiple inheritance abused far more than goto's.

                          Steve Wellens

                          1 Reply Last reply
                          0
                          • L LucianPopescu

                            Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

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

                            Lucian-aSterX wrote:

                            Do you use goto in C# ?

                            What do you thihk a do...while(0) loop with breaks is?

                            Morality is indistinguishable from social proscription

                            1 Reply Last reply
                            0
                            • L Lost User

                              I have used goto in C# when breaking out of nested loops, and rarely in switch blocks to avoid copying one case to the end of some other case (copypasta is worse than goto IMO). And I will hear no "nested loops should be refactored anyway" - it was performance code and performance code is not meant to look nice. In assembly I use the equivalent of goto all the time. There is no alternative.

                              C Offline
                              C Offline
                              Chris C B
                              wrote on last edited by
                              #49

                              At the risk of being 1-voted to oblivion, I agree with you. If a GOTO will improve the performance OR readability of code, it should be used. I recently used a GOTO: 0. A 3D grid of objects, with coordinates expressed in 2 angular and 1 linear dimension. 1. Millions of these points in a DB, with random coordinates and local density. 2. Require to display one of these points in a 2D view, from a different origin, within the context of no less than, say, 50 surrounding points. 3. The DB contains only the raw data to compute the 3D coordinates. You can build the SQL to get a best bet of the points to analyze, but you have only a vague idea of how many of these will fit your parameters. The maths is complex, first to get the true coordinates, then to transpose to the observer position, and then to see if you have enough to give the context. If too many, fine, its dense. If too few, increase the radius of the search area by a percentage based on the shortfall. The kicker is, the original point must be in the centre of the search area, so any limit on the number of points in a loop may reach the limit in a small segment of the required circle, displacing the master point. I can assure anyone who questions this that a simple IF (not enough points) Calculate required increase to value GOTO startOfTheWholeShebangWithNewValue at the end of the code is both faster and more readable than any alternative. I am not going to post the code, and anyone familiar with coordinate transformations will know why. ;)

                              C 1 Reply Last reply
                              0
                              • J Johnny J

                                It's probably easier, but I still don't think it's a good use in this case. Sort of like exiting a room through the window. I'm not an expert on this, but can you be sure that jumping out of a nested loop like that doesn't leave something not cleaned up properly, stack e.g.?

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

                                Johnny J. wrote:

                                I'm not an expert on this, but can you be sure that jumping out of a nested loop like that doesn't leave something not cleaned up properly, stack e.g.?

                                Yes, you can be 100% sure, as long as you stay in the function, which every reasonable compiler enforces. The only way to mess this up is inline assembly to change the stack pointer, or inline assembly to force a cross-function jump to a function which expects a different amount of bytes for locals on the stack.

                                1 Reply Last reply
                                0
                                • L LucianPopescu

                                  Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

                                  M Offline
                                  M Offline
                                  Member 96
                                  wrote on last edited by
                                  #51

                                  Those who know when to use a goto do not tell, those who tell do not know. :) I've used them before, in probably 3 places out of millions of lines of code. In those 3 places it was entirely appropriate and called for in my opinion and I didn't even bother to find any other way to do it. It's a feature of the language and when it's right to use it you just know. Avoiding using it because some people still harbor a hatred for old style basic programming is about as silly as anything else in this world.


                                  Yesterday they said today was tomorrow but today they know better. - Poul Anderson

                                  M 1 Reply Last reply
                                  0
                                  • L LucianPopescu

                                    Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

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

                                    Yes, you can make spaghetti code with goto's. However, you can just as easily make spaghetti code with degenerate loops, loops with insane stop criteria and weird booleans which you are forced to use as replacement. Also, just because some theoreticians said "goto is bad because it messes with the provability of the code" doesn't mean goto is actually bad. Who in their right mind actually proves that their code is correct? That's an insane amount of work, where you could just do some tests and "be reasonably sure" that it's fine. Chances are low that you're writing Mission Control code for an expensive space program. In other news, Knuth does not think that goto is evil, and wrote the paper "Structured Programming with Goto Statements" (use google).

                                    M T 2 Replies Last reply
                                    0
                                    • R Rajesh R Subramanian

                                      It isn't a programming question. He's just asking for opinions about usage of a statement (warning: related to programming). Ya know? Other than important things like football, bullshit politics, stiupid dradunk bdahbble, cricket, etc., people should be let free to discuss something related to programming now and then.

                                      It's time for a new sig. Seriously.

                                      P Offline
                                      P Offline
                                      popchecker
                                      wrote on last edited by
                                      #53

                                      Why hate football posts? At least you are a sport man right?

                                      R 1 Reply Last reply
                                      0
                                      • C Chris C B

                                        At the risk of being 1-voted to oblivion, I agree with you. If a GOTO will improve the performance OR readability of code, it should be used. I recently used a GOTO: 0. A 3D grid of objects, with coordinates expressed in 2 angular and 1 linear dimension. 1. Millions of these points in a DB, with random coordinates and local density. 2. Require to display one of these points in a 2D view, from a different origin, within the context of no less than, say, 50 surrounding points. 3. The DB contains only the raw data to compute the 3D coordinates. You can build the SQL to get a best bet of the points to analyze, but you have only a vague idea of how many of these will fit your parameters. The maths is complex, first to get the true coordinates, then to transpose to the observer position, and then to see if you have enough to give the context. If too many, fine, its dense. If too few, increase the radius of the search area by a percentage based on the shortfall. The kicker is, the original point must be in the centre of the search area, so any limit on the number of points in a loop may reach the limit in a small segment of the required circle, displacing the master point. I can assure anyone who questions this that a simple IF (not enough points) Calculate required increase to value GOTO startOfTheWholeShebangWithNewValue at the end of the code is both faster and more readable than any alternative. I am not going to post the code, and anyone familiar with coordinate transformations will know why. ;)

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

                                        Chris C-B wrote:

                                        At the risk of being 1-voted to oblivion, I agree with you. If a GOTO will improve the performance OR readability of code, it should be used

                                        If the statement is still there, ergo there must be a reason. "Don't use goto" is a good, quick rule for the newbie. :)

                                        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.
                                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                        [My articles]

                                        1 Reply Last reply
                                        0
                                        • L LucianPopescu

                                          Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...

                                          N Offline
                                          N Offline
                                          Nemanja Trifunovic
                                          wrote on last edited by
                                          #55

                                          Lucian-aSterX wrote:

                                          Do you use goto in C# ?

                                          I don't use C# any more, but my attitude about goto is simple: I have never used it before in C-like languages (assembly and Fortran are a different story) but if I ever find a situation when it is useful I'll use it without any second thoughts.

                                          utf8-cpp

                                          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