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. for those of you purists that don't like break, continue and goto

for those of you purists that don't like break, continue and goto

Scheduled Pinned Locked Moved The Lounge
question
65 Posts 21 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.
  • H Offline
    H Offline
    honey the codewitch
    wrote on last edited by
    #1

    why do this?

    for(int i = 0;i
    instead of

    for(int i = 0;i

    hengh?? why you still use break?

    :laugh:

    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

    OriginalGriffO J G N Sander RosselS 18 Replies Last reply
    0
    • H honey the codewitch

      why do this?

      for(int i = 0;i
      instead of

      for(int i = 0;i

      hengh?? why you still use break?

      :laugh:

      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      Why does it matter?

      Wrong is evil and must be defeated. - Jeff Ello

      H 1 Reply Last reply
      0
      • H honey the codewitch

        why do this?

        for(int i = 0;i
        instead of

        for(int i = 0;i

        hengh?? why you still use break?

        :laugh:

        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

        Because break is a lot clearer - and less likely to cause problems later if the for loop end condition gets modified.

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "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

        H L 2 Replies Last reply
        0
        • J Jorgen Andersson

          Why does it matter?

          Wrong is evil and must be defeated. - Jeff Ello

          H Offline
          H Offline
          honey the codewitch
          wrote on last edited by
          #4

          if it matters enough to be a purist about it, then i think it matters. if it doesn't matter to you, you're not a purist about it. :laugh: bow before my tautology!

          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

          J 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Because break is a lot clearer - and less likely to cause problems later if the for loop end condition gets modified.

            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            H Offline
            H Offline
            honey the codewitch
            wrote on last edited by
            #5

            rather my point. sometimes you need a good break, continue, or even a goto (though the latter i typically reserve for generated state machines)

            When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

            N 1 Reply Last reply
            0
            • H honey the codewitch

              why do this?

              for(int i = 0;i
              instead of

              for(int i = 0;i

              hengh?? why you still use break?

              :laugh:

              When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

              My personal preference is to use the break, as when later on when someone adds more logic before that final brace you now have potentially undesirable side effects with having mutated i within the loop. I can see the possibility of arguing it either way.

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

              ― Christopher Hitchens

              H 1 Reply Last reply
              0
              • H honey the codewitch

                if it matters enough to be a purist about it, then i think it matters. if it doesn't matter to you, you're not a purist about it. :laugh: bow before my tautology!

                When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #7

                I bow before thee my Goodwife CodeWitch. I'm not a purist in this matter, but I still agree with Griff though.

                Wrong is evil and must be defeated. - Jeff Ello

                H 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Because break is a lot clearer - and less likely to cause problems later if the for loop end condition gets modified.

                  Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

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

                  throw would be even more clear, definite and failure proof. (well I do see many kids using exactly that to 'not use goto endlabel')

                  Message Signature (Click to edit ->)

                  H K 2 Replies Last reply
                  0
                  • G GuyThiebaut

                    My personal preference is to use the break, as when later on when someone adds more logic before that final brace you now have potentially undesirable side effects with having mutated i within the loop. I can see the possibility of arguing it either way.

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

                    ― Christopher Hitchens

                    H Offline
                    H Offline
                    honey the codewitch
                    wrote on last edited by
                    #9

                    GuyThiebaut wrote:

                    I can see the possibility of arguing it either way.

                    My guess is that would put you in the minority. As for me, I prefer the break, but then I have no qualms about continue, return, goto, or other "out of band" control flow - pretty much except "throw" which I never use as a control flow statement (though I've been forced to use catch for that occasionally due to Other People's Code - *sideeyes Microsoft's HttpWebRequest class*) i just try to keep the out of band stuff near the top of my scope where people can see it.

                    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                    L G 2 Replies Last reply
                    0
                    • J Jorgen Andersson

                      I bow before thee my Goodwife CodeWitch. I'm not a purist in this matter, but I still agree with Griff though.

                      Wrong is evil and must be defeated. - Jeff Ello

                      H Offline
                      H Offline
                      honey the codewitch
                      wrote on last edited by
                      #10

                      I do as well, but then i have no qualms about any out of band control flow statements except throw. It's all in how and where you use them, IMO.

                      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                      J 1 Reply Last reply
                      0
                      • L Lost User

                        throw would be even more clear, definite and failure proof. (well I do see many kids using exactly that to 'not use goto endlabel')

                        Message Signature (Click to edit ->)

                        H Offline
                        H Offline
                        honey the codewitch
                        wrote on last edited by
                        #11

                        throw is worse than goto, IMO for control flow.

                        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                        1 Reply Last reply
                        0
                        • H honey the codewitch

                          rather my point. sometimes you need a good break, continue, or even a goto (though the latter i typically reserve for generated state machines)

                          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                          N Offline
                          N Offline
                          Nand32
                          wrote on last edited by
                          #12

                          I love goto. Lets go-back-to VB. :-D I've made use of gotos to a great extent during my VB times. like, On Error: GoTo the magical origin of the universe. An equally 'powerful' instruction - JMP. But JMP is never ashamed of being JMP, cuz it's assembly. You can go roam anywhere you want, it's got the license. :laugh:

                          H 1 Reply Last reply
                          0
                          • H honey the codewitch

                            I do as well, but then i have no qualms about any out of band control flow statements except throw. It's all in how and where you use them, IMO.

                            When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                            J Offline
                            J Offline
                            Jorgen Andersson
                            wrote on last edited by
                            #13

                            Throw doesn't belong to the control flow. Oh, maybe I'm a bit purist after all.

                            Wrong is evil and must be defeated. - Jeff Ello

                            H 1 Reply Last reply
                            0
                            • N Nand32

                              I love goto. Lets go-back-to VB. :-D I've made use of gotos to a great extent during my VB times. like, On Error: GoTo the magical origin of the universe. An equally 'powerful' instruction - JMP. But JMP is never ashamed of being JMP, cuz it's assembly. You can go roam anywhere you want, it's got the license. :laugh:

                              H Offline
                              H Offline
                              honey the codewitch
                              wrote on last edited by
                              #14

                              ugh, VB. I use goto in some of my code. Perfectly acceptable place to use GOTO - generated state machine code:

                              public static bool AcceptsByte(Grimoire.ParseContext pc)
                              {
                              pc.EnsureStarted();
                              if (-1 == pc.Current) return false;
                              if ((48 == pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s1;
                              }
                              if ((49 == pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s2;
                              }
                              if ((50 == pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s4;
                              }
                              if ((51 <= pc.Current && 57 >= pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s3;
                              }
                              return false;
                              AcceptsByte_s1:
                              if (-1 == pc.Current) return true;
                              return -1 == pc.Advance();
                              AcceptsByte_s2:
                              if (-1 == pc.Current) return true;
                              if ((48 <= pc.Current && 57 >= pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s3;
                              }
                              return -1 == pc.Advance();
                              AcceptsByte_s3:
                              if (-1 == pc.Current) return true;
                              if ((48 <= pc.Current && 57 >= pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s1;
                              }
                              return -1 == pc.Advance();
                              AcceptsByte_s4:
                              if (-1 == pc.Current) return true;
                              if ((48 <= pc.Current && 52 >= pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s3;
                              }
                              if ((53 == pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s5;
                              }
                              if ((54 <= pc.Current && 57 >= pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s1;
                              }
                              return -1 == pc.Advance();
                              AcceptsByte_s5:
                              if (-1 == pc.Current) return true;
                              if ((48 <= pc.Current && 52 >= pc.Current))
                              {
                              pc.Advance();
                              goto AcceptsByte_s1;
                              }
                              return -1 == pc.Advance();
                              }

                              but then I wouldn't write that code by hand. Too error prone.

                              When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                              N J 2 Replies Last reply
                              0
                              • H honey the codewitch

                                GuyThiebaut wrote:

                                I can see the possibility of arguing it either way.

                                My guess is that would put you in the minority. As for me, I prefer the break, but then I have no qualms about continue, return, goto, or other "out of band" control flow - pretty much except "throw" which I never use as a control flow statement (though I've been forced to use catch for that occasionally due to Other People's Code - *sideeyes Microsoft's HttpWebRequest class*) i just try to keep the out of band stuff near the top of my scope where people can see it.

                                When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

                                If you enjoy pain try mixing throw with interop. (sure way to properly destroy documents and pst files.)

                                Message Signature (Click to edit ->)

                                H 1 Reply Last reply
                                0
                                • J Jorgen Andersson

                                  Throw doesn't belong to the control flow. Oh, maybe I'm a bit purist after all.

                                  Wrong is evil and must be defeated. - Jeff Ello

                                  H Offline
                                  H Offline
                                  honey the codewitch
                                  wrote on last edited by
                                  #16

                                  i mean, i agree that it shouldn't, but it causes control flow changes and can be used that way. but like i said, i agree. Just because you can do something, doesn't mean you should.

                                  When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                  J 1 Reply Last reply
                                  0
                                  • H honey the codewitch

                                    GuyThiebaut wrote:

                                    I can see the possibility of arguing it either way.

                                    My guess is that would put you in the minority. As for me, I prefer the break, but then I have no qualms about continue, return, goto, or other "out of band" control flow - pretty much except "throw" which I never use as a control flow statement (though I've been forced to use catch for that occasionally due to Other People's Code - *sideeyes Microsoft's HttpWebRequest class*) i just try to keep the out of band stuff near the top of my scope where people can see it.

                                    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

                                    honey the codewitch wrote:

                                    My guess is that would put you in the minority.

                                    I agree, my experience is that as a generalisation us developers have very strong opinions. At the age of 49 I have found that I no longer have the desire to get into arguments over this sort of thing - as in six months time the 'best practise' recommendation will in all likelihood have switched.

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

                                    ― Christopher Hitchens

                                    H 1 Reply Last reply
                                    0
                                    • L Lost User

                                      If you enjoy pain try mixing throw with interop. (sure way to properly destroy documents and pst files.)

                                      Message Signature (Click to edit ->)

                                      H Offline
                                      H Offline
                                      honey the codewitch
                                      wrote on last edited by
                                      #18

                                      oooh. Another place i like to throw is in static constructors, just to get absolutely everyone's BAC up. :laugh: I'm kidding. I'm not a sadist.

                                      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                      1 Reply Last reply
                                      0
                                      • H honey the codewitch

                                        why do this?

                                        for(int i = 0;i
                                        instead of

                                        for(int i = 0;i

                                        hengh?? why you still use break?

                                        :laugh:

                                        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                        N Offline
                                        N Offline
                                        Nand32
                                        wrote on last edited by
                                        #19

                                        At times, the purpose of the search might be just to find the index of the valueToFind. Break keeps the index safe?

                                        (JS)
                                        var arr =[0,1,2,3,4,5];
                                        var valueToFind = 3;

                                        for(i=0;i

                                        H 1 Reply Last reply
                                        0
                                        • H honey the codewitch

                                          ugh, VB. I use goto in some of my code. Perfectly acceptable place to use GOTO - generated state machine code:

                                          public static bool AcceptsByte(Grimoire.ParseContext pc)
                                          {
                                          pc.EnsureStarted();
                                          if (-1 == pc.Current) return false;
                                          if ((48 == pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s1;
                                          }
                                          if ((49 == pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s2;
                                          }
                                          if ((50 == pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s4;
                                          }
                                          if ((51 <= pc.Current && 57 >= pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s3;
                                          }
                                          return false;
                                          AcceptsByte_s1:
                                          if (-1 == pc.Current) return true;
                                          return -1 == pc.Advance();
                                          AcceptsByte_s2:
                                          if (-1 == pc.Current) return true;
                                          if ((48 <= pc.Current && 57 >= pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s3;
                                          }
                                          return -1 == pc.Advance();
                                          AcceptsByte_s3:
                                          if (-1 == pc.Current) return true;
                                          if ((48 <= pc.Current && 57 >= pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s1;
                                          }
                                          return -1 == pc.Advance();
                                          AcceptsByte_s4:
                                          if (-1 == pc.Current) return true;
                                          if ((48 <= pc.Current && 52 >= pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s3;
                                          }
                                          if ((53 == pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s5;
                                          }
                                          if ((54 <= pc.Current && 57 >= pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s1;
                                          }
                                          return -1 == pc.Advance();
                                          AcceptsByte_s5:
                                          if (-1 == pc.Current) return true;
                                          if ((48 <= pc.Current && 52 >= pc.Current))
                                          {
                                          pc.Advance();
                                          goto AcceptsByte_s1;
                                          }
                                          return -1 == pc.Advance();
                                          }

                                          but then I wouldn't write that code by hand. Too error prone.

                                          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                          N Offline
                                          N Offline
                                          Nand32
                                          wrote on last edited by
                                          #20

                                          Yep, goto on a lengthier code is definitely not meant for the human eyes. :)

                                          H 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