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.
  • 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
                      • R realJSOP

                        No serious programmer uses a goto. EDIT (After the 1 vote)---------------- Okay - maybe we all do - when we goto the bathroom...

                        .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

                        modified on Monday, July 5, 2010 10:22 AM

                        M Offline
                        M Offline
                        Mladen Jankovic
                        wrote on last edited by
                        #56

                        Real Programmers aren't afraid to use GOTOs[^] Yet another proof that owning a gun doesn't make you a real man.

                        [Genetic Algorithm Library] [Wowd]

                        1 Reply Last reply
                        0
                        • P popchecker

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

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

                          I never said I hate football - I just don't care about it enough to hate it. And at the same time I don't mind people discussing about it. All I'm asking for is a little space for people to discuss about something that's completely valid and on topic. That's programming.

                          It's time for a new sig. Seriously.

                          1 Reply Last reply
                          0
                          • L Lost User

                            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 Offline
                            M Offline
                            Mladen Jankovic
                            wrote on last edited by
                            #58

                            harold aptroot wrote:

                            provability of the code

                            :laugh: There's a great Joel 's article on the subject. Talk at Yale: Part 1 of 3[^] And when all was said and done, she got to the end of the proof, and somehow was getting exactly the opposite result of the one that made sense, until that same graduate student pointed out where, 63 steps earlier, some bit had been accidentally flipped due to a little bit of dirt on the board, and all was well.

                            [Genetic Algorithm Library] [Wowd]

                            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...

                              D Offline
                              D Offline
                              DaveyM69
                              wrote on last edited by
                              #59

                              I used one a couple of weeks ago. I could have used some other mechanism but it was the perfect tool for that particular job, anything else would have added unecessary complexity and would probably have been optimised away into the same IL anyway. It's all part of being a programmer in my opinion - selecting the right tool [keyword]. Having said that, it's the first time I've used it in years!

                              Dave

                              If this helped, please vote & accept answer!

                              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
                              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                              1 Reply Last reply
                              0
                              • M Member 96

                                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 Offline
                                M Offline
                                Mladen Jankovic
                                wrote on last edited by
                                #60

                                John C wrote:

                                Those who know when to use a goto do not tell, those who tell do not know

                                A bit like teenage sex?

                                [Genetic Algorithm Library] [Wowd]

                                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...

                                  C Offline
                                  C Offline
                                  Christian Graus
                                  wrote on last edited by
                                  #61

                                  I am not religious about not using goto, I'd use it if it were ever truly useful, above and beyond the spagetti effect it creates. I've never once used it in C, C++, or C#.

                                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                                  1 Reply Last reply
                                  0
                                  • L leppie

                                    Funny how no-one even the comments could come up with a really good alternative (I agree for simplicity, the && is approach is best). How would I do it?

                                    bool Do(params Func<bool>[] args)
                                    {
                                    foreach (var a in args)
                                    if (!a())
                                    return false;
                                    return true;
                                    }
                                    ...

                                    if (Do(Step1, Step2, Step3, Step4, Step5, Step6, Step7))
                                    Console.WriteLine("Brillant!");

                                    xacc.ide
                                    IronScheme - 1.0 RC 1 - out now!
                                    ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition

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

                                    leppie wrote:

                                    if (Do(Step1, Step2, Step3, Step4, Step5, Step6, Step7)) Console.WriteLine("Brillant!");

                                    And how do you put a breakpoint on Step3? If you can't put a breakpoint on a line of code, you lose maintainability.

                                    Steve Wellens

                                    L 1 Reply Last reply
                                    0
                                    • S Steve Wellens

                                      leppie wrote:

                                      if (Do(Step1, Step2, Step3, Step4, Step5, Step6, Step7)) Console.WriteLine("Brillant!");

                                      And how do you put a breakpoint on Step3? If you can't put a breakpoint on a line of code, you lose maintainability.

                                      Steve Wellens

                                      L Offline
                                      L Offline
                                      leppie
                                      wrote on last edited by
                                      #63

                                      Steve Wellens wrote:

                                      And how do you put a breakpoint on Step3?

                                      Either in Step3, or in the Do method...

                                      Steve Wellens wrote:

                                      If you can't put a breakpoint on a line of code, you lose maintainability.

                                      BS!

                                      xacc.ide
                                      IronScheme - 1.0 RC 1 - out now!
                                      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition

                                      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
                                        #64

                                        Despite all preaching against them, gotos are nothing bad or evil. They just should be used sparingly and with consideration. When you have some deeply nested (spaghetti) code, a goto may be the only way to get out without making the whole thing even more complicated. Indeed you might also see a break as a goto with an implicit destination. Refactoring the code into separate methods may be a far better choice most of the time, but sometimes you need such a spaghetti monster and then gotos may be needed. But if somebody needs them regularily, that person should work a little on the coding style.

                                        A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                                        P 1 Reply Last reply
                                        0
                                        • P Pete OHanlon

                                          OK - to clarify. What you have asked here is more of a programming philosophy question than a programming question. Others ask these in the Lounge and get away with them so I don't see why you shouldn't. I have never encountered a situation in .NET where a goto makes my job easier. Generally, a well structured application with small, self-contained methods should have no need for a goto. When I see them, it's generally an indication that somebody has landed themselves in an architectural muddle and they can't see the way out of the morass. This is generally when they have loops nested within loops nested within loops. The way I tend to look at this, if you've got yourself this deep into loopfuggery, it's an indication that you are using the wrong loops.

                                          "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

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

                                          If you look at oldfashioned code you will see that it generally had a considerably smaller memory footprint and executed faster (in comparison). Good design obviously costs memory and some efficiency. With today's best practices one would not rally get far on a 8 bit CPU and 4k RAM. I don't rerally like the 'we have plenty of everything' attitude, however, only small and selected regions of the code need such a performance treatment and should then be well commented.

                                          A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                                          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