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.
  • 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
    Dave Parker
    wrote on last edited by
    #17

    Real men use "goto" :p

    C 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
      Luc Pattyn
      wrote on last edited by
      #18

      break, return, yield, goto, throw, Application.Exit(), longjmp(), ... can all be justified given the right circumstances. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      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

        B Offline
        B Offline
        bob16972
        wrote on last edited by
        #19

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

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

          wizardzz wrote:

          What are your views on these?

          Infinite tail-recursion is much nicer.

          utf8-cpp

          1 Reply Last reply
          0
          • R RugbyLeague

            I use them although not often. I also often add an empty IDisposable to classes in C# because I like to wrap things in "using" :~

            T Offline
            T Offline
            TheGreatAndPowerfulOz
            wrote on last edited by
            #21

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

              I Offline
              I Offline
              Ian Shlasko
              wrote on last edited by
              #22

              Checking my production system, it looks like I've used while (true) in six places... #1: Background thread that continuously monitors a Named Pipe #2: Background thread that periodically checks for application updates #3: Background thread that uploads trades as they're queued #4: Background thread that watches for incoming messages from the server #5: Background thread that sends heartbeats to the server #6: Background thread that handles message routing ON the server Basically, all of them are intended to run throughout the life of the application... They all have a sleep or a wait handle, but otherwise just loop continuously... It's a useful construct. Yes, I suppose I could add a global "Shutting down" condition, but it just hasn't been necessary. They catch the thread abort, clean up in the 'finally', and are designed so as not to damage anything external if the shutdown interrupts them at any point.

              Proud to have finally moved to the A-Ark. Which one are you in?
              Author of the Guardians Saga (Sci-Fi/Fantasy novels)

              1 Reply Last reply
              0
              • D Dave Parker

                Real men use "goto" :p

                C Offline
                C Offline
                Chris Maunder
                wrote on last edited by
                #23

                Beat me to it!

                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                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

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #24

                  i like em just fine, in things like state machines, where the endpoint is unknown. do{...}while(ok) is ok, too.

                  image processing toolkits | batch image processing

                  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
                    AspDotNetDev
                    wrote on last edited by
                    #25

                    I like to consolidate:

                    for (; ; MessageBox.Show("Hello")) ;

                    :rolleyes: And here's the best I could do in VB.net:

                    For i As Integer = 0 To 0 Step 0
                    Next

                    Like somebody else mentioned, they may have their uses in background threads.

                    [WikiLeaks Cablegate Cables]

                    1 Reply Last reply
                    0
                    • N Nish Nishant

                      Most compilers (managed and native) will generate the same output code for either construct. So it's more a matter of style and preference. Personally, the while(true) seems more readable and it's more obvious what it's meant to do.

                      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

                      C Offline
                      C Offline
                      Christopher Duncan
                      wrote on last edited by
                      #26

                      Nishant Sivakumar wrote:

                      what it's meant to do.

                      Hang the app with an endless loop? :)

                      Christopher Duncan Author of The Career Programmer Watch Bad Programmer! - Premieres May, 2011

                      N 1 Reply Last reply
                      0
                      • C Christopher Duncan

                        Nishant Sivakumar wrote:

                        what it's meant to do.

                        Hang the app with an endless loop? :)

                        Christopher Duncan Author of The Career Programmer Watch Bad Programmer! - Premieres May, 2011

                        N Offline
                        N Offline
                        Nish Nishant
                        wrote on last edited by
                        #27

                        Christopher Duncan wrote:

                        Hang the app with an endless loop?

                        Only on a single core machine :-D

                        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

                        C P 2 Replies Last reply
                        0
                        • N Nish Nishant

                          Christopher Duncan wrote:

                          Hang the app with an endless loop?

                          Only on a single core machine :-D

                          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

                          C Offline
                          C Offline
                          Christopher Duncan
                          wrote on last edited by
                          #28

                          :laugh:

                          Christopher Duncan Author of The Career Programmer Watch Bad Programmer! - Premieres May, 2011

                          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

                            J Offline
                            J Offline
                            Jun Du
                            wrote on last edited by
                            #29

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

                              Mike HankeyM Offline
                              Mike HankeyM Offline
                              Mike Hankey
                              wrote on last edited by
                              #30

                              I use the while(true) consistently...in embedded apps. (AVR)

                              If you are cross-eyed and have dyslexia, can you read all right? http://www.hq4thmarinescomm.com[^] JaxCoder.com[^]WinHeist - Windows Electronic Inventory SysTem

                              1 Reply Last reply
                              0
                              • 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
                                          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