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. while (true) and for (; ; ) [modified]

while (true) and for (; ; ) [modified]

Scheduled Pinned Locked Moved The Lounge
hostingcloudquestion
76 Posts 48 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.
  • J Jun Du

    I don't find many situations where I must use either. Something like while(!done) gives you a control over whether and when to exit, normal or abnormal.

    Best, Jun

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

    Not always. In C applications, for instance, you may know in the middle of the loop that you've to exit and while you may skip the following statements with an if and then use the condition, I prefer an immediate break.

    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]

    J J 2 Replies Last reply
    0
    • H Henry Minute

      I have only used them, or their equivalent in other languages, for testing something. Never in production.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus! When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.

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

      Same here, useful but risky in the wild.

      Join the cool kids - Come fold with us[^]

      1 Reply Last reply
      0
      • W wizardzz

        What are your views on these? How often do you use or see them and in what cases? Just curious, it's a little debate with my project's Architect. To clarify, I don't mean the preference between the 2, but the use of such loops in production.

        "Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" — Hunter S. Thompson

        modified on Thursday, March 10, 2011 12:10 PM

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #33

        One version I every now and then use is:

        while (6 * 9 != 42)

        This wouldn't cause an infinite loop since one (particular) day those will be equal :)

        The need to optimize rises from a bad design.My articles[^]

        1 Reply Last reply
        0
        • W wizardzz

          What are your views on these? How often do you use or see them and in what cases? Just curious, it's a little debate with my project's Architect. To clarify, I don't mean the preference between the 2, but the use of such loops in production.

          "Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" — Hunter S. Thompson

          modified on Thursday, March 10, 2011 12:10 PM

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

          I just used while(condition); in a unit test to test an async method for retrieving a business object. I can't imagine a use for them in production code though.


          There is no failure only feedback

          A 2 Replies Last reply
          0
          • M Member 96

            I just used while(condition); in a unit test to test an async method for retrieving a business object. I can't imagine a use for them in production code though.


            There is no failure only feedback

            A Offline
            A Offline
            Andy Brummer
            wrote on last edited by
            #35

            That's not even in the same category.

            Curvature of the Mind now with 3D

            1 Reply Last reply
            0
            • M Member 96

              I just used while(condition); in a unit test to test an async method for retrieving a business object. I can't imagine a use for them in production code though.


              There is no failure only feedback

              A Offline
              A Offline
              Andy Brummer
              wrote on last edited by
              #36

              any way, if you are targeting 4.0, check this: SpinUntil[^]

              Curvature of the Mind now with 3D

              M 1 Reply Last reply
              0
              • W wizardzz

                What are your views on these? How often do you use or see them and in what cases? Just curious, it's a little debate with my project's Architect. To clarify, I don't mean the preference between the 2, but the use of such loops in production.

                "Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" — Hunter S. Thompson

                modified on Thursday, March 10, 2011 12:10 PM

                D Offline
                D Offline
                Dan Neely
                wrote on last edited by
                #37

                X| and X|

                3x12=36 2x12=24 1x12=12 0x12=18

                1 Reply Last reply
                0
                • A Andy Brummer

                  I think I've used while true a few times, mostly in either complicated parser code, or early on when I was learning threading. It can make sense when side effects and the loop exit criteria are so intertwined that you can't separate them easily to reduce the exit to a single boolean. However, that's a huge flag to refactor/write your code so things aren't so intertwined. If you don't have the time/flexibility to do that, then I'd choose while (true) over creating a convoluted mess of a loop body just to have a boolean value to exit on.

                  Curvature of the Mind now with 3D

                  D Offline
                  D Offline
                  Dan Neely
                  wrote on last edited by
                  #38

                  By the time your code gets too noxious to write a good exit condition, it's also too noxious to easily debug without dismantling the mess first. Unless you can write bugfree codethulus on the first attempt, there's no excuse for letting it get that bad in the first place.

                  3x12=36 2x12=24 1x12=12 0x12=18

                  A 1 Reply Last reply
                  0
                  • A Andy Brummer

                    any way, if you are targeting 4.0, check this: SpinUntil[^]

                    Curvature of the Mind now with 3D

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

                    I don't understand your other reply but this one is gold! Thanks for that I'm going to use it immediately.


                    There is no failure only feedback

                    A 1 Reply Last reply
                    0
                    • D Dan Neely

                      By the time your code gets too noxious to write a good exit condition, it's also too noxious to easily debug without dismantling the mess first. Unless you can write bugfree codethulus on the first attempt, there's no excuse for letting it get that bad in the first place.

                      3x12=36 2x12=24 1x12=12 0x12=18

                      A Offline
                      A Offline
                      Andy Brummer
                      wrote on last edited by
                      #40

                      Yeah, I guess I'm too used to batting cleanup and having to pick my battles.

                      Curvature of the Mind now with 3D

                      D 1 Reply Last reply
                      0
                      • M Member 96

                        I don't understand your other reply but this one is gold! Thanks for that I'm going to use it immediately.


                        There is no failure only feedback

                        A Offline
                        A Offline
                        Andy Brummer
                        wrote on last edited by
                        #41

                        All I'm saying is that while(true) { do something repeatedly } sometimes has a place in real code. The other while(checkSharedVariable) {}; is just a good way to kick up your CPU power consumption.

                        Curvature of the Mind now with 3D

                        M 1 Reply Last reply
                        0
                        • A Andy Brummer

                          All I'm saying is that while(true) { do something repeatedly } sometimes has a place in real code. The other while(checkSharedVariable) {}; is just a good way to kick up your CPU power consumption.

                          Curvature of the Mind now with 3D

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

                          Ahhh! I see. Well it *is* cold in here. ;)


                          There is no failure only feedback

                          A 1 Reply Last reply
                          0
                          • M Member 96

                            Ahhh! I see. Well it *is* cold in here. ;)


                            There is no failure only feedback

                            A Offline
                            A Offline
                            Andy Brummer
                            wrote on last edited by
                            #43

                            :-D

                            Curvature of the Mind now with 3D

                            1 Reply Last reply
                            0
                            • W wizardzz

                              Ah, I'm going to modify my original post. I wasn't referring to the preference between the 2, but the use of infinite loops, especially if there is no breaks to exit the loop. I guess we have a crash-only design for some applications.

                              "Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" — Hunter S. Thompson

                              S Offline
                              S Offline
                              Steve Mayfield
                              wrote on last edited by
                              #44

                              Those constructs are usually seen in non-ineractive microprocessor embedded systems - I use them all of the time for the main "idle loop". I rely on a watchdog timer to restart the application if something hangs.

                              Steve _________________ I C(++) therefore I am

                              1 Reply Last reply
                              0
                              • A Andy Brummer

                                Yeah, I guess I'm too used to batting cleanup and having to pick my battles.

                                Curvature of the Mind now with 3D

                                D Offline
                                D Offline
                                Dan Neely
                                wrote on last edited by
                                #45

                                Unfubarring the mess and then fixing it typically takes me less time than trying to patch the fubar.

                                3x12=36 2x12=24 1x12=12 0x12=18

                                1 Reply Last reply
                                0
                                • W wizardzz

                                  What are your views on these? How often do you use or see them and in what cases? Just curious, it's a little debate with my project's Architect. To clarify, I don't mean the preference between the 2, but the use of such loops in production.

                                  "Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" — Hunter S. Thompson

                                  modified on Thursday, March 10, 2011 12:10 PM

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

                                  The only place I can remember using endless loops is in thread procedures. There often is no break, but of course I catch the ThreadAbortException to exit the loop. For example, a 3D rendering thread would be started at the beginning of the application and render as many frames as fast as it can until the thread is ended. Ending threads with an exception always appeared a little strange to me, because this definitely is changing the program flow with exceptions, but at least it has worked as expected up to now.

                                  "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                                  1 Reply Last reply
                                  0
                                  • N Nish Nishant

                                    Okay, I can't think of why anyone'd have a while-true (or otherwise infinite) loop that does not have any normal exit conditions. Although you could break out via an exception it just does not seem very clean to me.

                                    Regards, Nish


                                    New article available: Resetting a View Model in WPF MVVM applications without code-behind in the view My technology blog: voidnish.wordpress.com

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

                                    I wrote it below: A thread procedure without a clear number of iterations. Threads are terminated with an exception, but I also think that's not clean. But Bill wants it that way.

                                    "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                                    1 Reply Last reply
                                    0
                                    • T TheGreatAndPowerfulOz

                                      RugbyLeague wrote:

                                      IDisposable to classes in C#

                                      That's silly. It serves no purpose, except obfuscation and misrepresentation, if you don't implement the IDisposable!

                                      "If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams "Let me get this straight. You know her. She knows you. But she wants to eat him. And everybody's okay with this?" - Timon

                                      R Offline
                                      R Offline
                                      RugbyLeague
                                      wrote on last edited by
                                      #48

                                      I implement it - it just doesn't do anything. I like how a "using" easily and obviously defines a scope for a class. using(var foo = new foo()) { foo.DoStuff(); } feels better than: var foo = new foo(); foo.DoStuff(); because in the latter foo is still in scope after use - the former more explicitly defines the useful lifetime of foo - I know there are other probably more correct ways of doing this. I just like the "using" notation. I don't always use it of course.

                                      1 Reply Last reply
                                      0
                                      • B bob16972

                                        They are both handy from time to time (expression parsing and times when you don't know how many iterations will be needed). I personally prefer while(TRUE) so as not confuse any VB programmer that comes across my code.

                                        H Offline
                                        H Offline
                                        hairy_hats
                                        wrote on last edited by
                                        #49

                                        bob16972 wrote:

                                        I personally prefer while(TRUE) so as not confuse any VB programmer that comes across my code.

                                        :confused: Isn't that a reason to use for(;;) ?

                                        1 Reply Last reply
                                        0
                                        • W wizardzz

                                          What are your views on these? How often do you use or see them and in what cases? Just curious, it's a little debate with my project's Architect. To clarify, I don't mean the preference between the 2, but the use of such loops in production.

                                          "Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" — Hunter S. Thompson

                                          modified on Thursday, March 10, 2011 12:10 PM

                                          A Offline
                                          A Offline
                                          alanevans
                                          wrote on last edited by
                                          #50

                                          I'd prefer someone do this:

                                          while(true)
                                          {
                                          if(matched condition A)
                                          {
                                          do something A
                                          break;
                                          }

                                          if(matched condition B)
                                          {
                                          do something B
                                          break;
                                          }

                                          }

                                          to this:

                                          bool exit = false;
                                          while(!exit)
                                          {
                                          if(matched condition A)
                                          {
                                          do something A
                                          exit = true;
                                          continue;
                                          }

                                          if(matched condition B)
                                          {
                                          do something B
                                          exit = true;
                                          continue;
                                          }
                                          }

                                          It's messy, longer and more prone to bugs when being supported/extended, and all just to avoid the "infinite loop", even though really it's just as "infinite".

                                          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