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

    E Offline
    E Offline
    Euhemerus
    wrote on last edited by
    #77

    Plenty of programming books I've read have always stated that it is best to avoid the GoTo statement because it increases the chances of producing 'spagetti' code. Having said that, I first cut my programming teeth on assembly language; and you just couldn't avoid the use of GoTo (Jmp) statement in assembly.

    I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image. Stephen Hawking

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

      E Offline
      E Offline
      ely_bob
      wrote on last edited by
      #78

      Agreed that goto's should be avoided, and I dislike that they are not navigable in C#. However contrary to popular belief there are situations in which you are required to start coding before you actually know what the system architecture should look like. AN EXAMPLE: in situations where you have VERY complicated logic, say 100 's of variable combinations where each combination requires a slightly different approach yet they all implement the same code, you could permute out all the variants, and make a block for each (Terror to debug) if you categorized something wrong, or you can work your categorization into the code and call each functional block only once... this latter method, works very well in pure research of physical simulations, where you want to have a better understanding of the underlying properties (you can extract the characteristics of the system from your code)... a very specific example.. but... This is how your encouraged to solve problems in many graduate physics Biology and chemistry courses that focus on devising rules for observable properties, or devising qualitative groupings, of things which don't behave well with normal statistics... (categorical Bayesian data regression in degenerate systems is not very fun!)

      I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...

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

        S Offline
        S Offline
        Snowman58
        wrote on last edited by
        #79

        Lucian-aSterX wrote:

        i just saw that a collegue uses

        How old is your colleague? When I first learned to program, a "GOTO/JUMP" was the only way to break a loop before completion or to branch (i.e. early Fortran, original BASIC & 6502 assembly, etc). And I still tend to think in DO/FOR loops and GOTO line number. But I try to avoid using line numbers these days. We didn't have these modern languages like C and still used punched cards and paper tape (I am must be seriously old!).

        Melting Away www.deals-house.com www.innovative--concepts.com

        L 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
          CDMTJX
          wrote on last edited by
          #80

          That collegue is probably used to an older programming where other language constructs don't exist. Decades ago I was taught that gotos are bad. I haven't used a goto in years where I have any other option...

          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
            Larry G Grimes
            wrote on last edited by
            #81

            It seems to me, the GOTO statement goes back to the original Basic Programming of the early seventies, where it had really valid uses. Because resources, especially memory, was so scarce, you used less precious bytes when you used a GOTO instead of a FOR loop. Nowadays, it's more used for getting out of some sticky situations such as multiple nested loops or to get around such things. It's much more elegant and sound to set a boolean flag and just check it at every step out of the structure. What do you guys think?

            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
              DQNOK
              wrote on last edited by
              #82

              I just got through reading all the comments (as of the time I'm posting). Here's my 2 cents. I skipped formal religious training (ie degree in computer science from university -- taught by monks who are constrained to teach the latest C.S. fad or risk losing their jobs) and instead "squandered" my time at university studying math and science (my parents were so ashamed :(( ). Anyway, because of my secular education, I tend to be mostly agnostic when it comes to religious wars on subjects like: - use of goto - use of macros - procedural versus Object Oriented, versus Aspect Oriented programming - which language (Python, Java, C#, etc.) the angels in heaven speak - which language (Cobol, Fortran, Algol, etc.) the demons in hell speak. I once heard a man say to his deeply religous mother, "But mom, even Jesus drank wine", to which she responded "Yes, but I'd have thought better of Him if He hadn't!" Perhaps there are just times when the angels, in order to be understood by us mere mortals, stoop to vile "hell-speak" and use goto. At which point, we would have thought better of them if they hadn't... ;)

              David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

              1 Reply Last reply
              0
              • S Snowman58

                Lucian-aSterX wrote:

                i just saw that a collegue uses

                How old is your colleague? When I first learned to program, a "GOTO/JUMP" was the only way to break a loop before completion or to branch (i.e. early Fortran, original BASIC & 6502 assembly, etc). And I still tend to think in DO/FOR loops and GOTO line number. But I try to avoid using line numbers these days. We didn't have these modern languages like C and still used punched cards and paper tape (I am must be seriously old!).

                Melting Away www.deals-house.com www.innovative--concepts.com

                L Offline
                L Offline
                LucianPopescu
                wrote on last edited by
                #83

                Snowman58 wrote:

                How old is your colleague?

                My collegue is about ~35 years old... but i don't think tha using goto has a lot to do with age, because i remember when i was in college (~2 years ago) the programming teacher (C#) told us about goto loops and teached us how to use this. I have to admit that he wasn't so good at programming, anyway... this may be the reason...

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

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #84

                  Lucian-aSterX wrote:

                  when i just saw that a collegue uses this very often

                  What does that actually mean? At least once in every non-trivial class when there are 100 or more classes? Or 3 times in 1000 classes? Or once each in 3 classes?

                  1 Reply Last reply
                  0
                  • L Lost User

                    I did say I can see better soloutions that the author chooses to ignore.

                    D Offline
                    D Offline
                    Damian Flynn
                    wrote on last edited by
                    #85

                    A break statement in C# is like a GoTo statement in VBA, except that is just takes the code out of the active loop. GoTo statements should be forward moving (in the method) only - else spaghetti code ensues. It's sometimes easier to use a GoTo statement in VBA (e.g., instead of a do while loop), within a standard iterator loop, to exit the loop. But VBA does have an exit do statement. There should be no need to use a GoTo statement in C#.

                    1 Reply Last reply
                    0
                    • R Rage

                      John Simmons / outlaw programmer wrote:

                      goto hell

                      Hell ?[^]

                      C Offline
                      C Offline
                      chrissb
                      wrote on last edited by
                      #86

                      Hell[^] :D Actually doesn't look[^] too bad, although possibly a mite cold.

                      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
                        chrissb
                        wrote on last edited by
                        #87

                        I taught myself programming in TI-BASIC on a TI-83+ at school. I'd already learnt area in year 7 (after skipping 3 years :P) so didn't feel the teacher was worth my time. Moved on to Assembly, then C. Then went to uni in an I.T. degree which of course included programming units. And I saw much, much worse than a simple GoTo by the very people that were telling me GoTo is evil and should die. From which the lesson I gathered was, if you know how to use a programming construct, than do so. If you don't, avoid it. That's the rule of coding. Don't just do or not do because you were told by someone that it's good/bad for you as the case may be, that makes you absolutely useless. And I mean that in the nicest possible way of course. ;P

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

                          W Offline
                          W Offline
                          Ware Work
                          wrote on last edited by
                          #88

                          Generally I do not use gotos in c# but there is one exception with regards to switch (cases) where you want to process multiple cases. For example, I have a data driven website where certain css classes are listed in data and applied to a user control in order. They are all optional but want to process each one specified in a specific way, so I created something like:

                          char sepChar = '|';
                          string[] array = dataString.Split(sepChar);

                          switch (array.length)
                          {
                          case 3:
                          dosomeassign(array[2], element5);
                          goto 2;
                          case 2:
                          dosomeotherassign(array[1], element4);
                          goto 1;
                          case 1:
                          dosomeassign(array[0], element2);
                          break;
                          }

                          WarePhreak Programmers are tools to convert caffiene to code.

                          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