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 Offline
    W Offline
    wizardzz
    wrote on last edited by
    #1

    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

    H N A G S 33 Replies 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

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      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 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
        Nish Nishant
        wrote on last edited by
        #3

        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

        W G C 3 Replies 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

          W Offline
          W Offline
          wizardzz
          wrote on last edited by
          #4

          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

          N S A G 4 Replies 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

            G Offline
            G Offline
            Gary Wheeler
            wrote on last edited by
            #5

            Agreed.

            Software Zen: delete this;

            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
              Andy Brummer
              wrote on last edited by
              #6

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

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

                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

                H L L A 4 Replies 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

                  G Offline
                  G Offline
                  Gary Wheeler
                  wrote on last edited by
                  #8

                  They are useful constructs, depending upon the algorithm. "Production" or not doesn't enter into it. Proscribing certain programming constructs in 'production' code is a naive method of quality enforcement.

                  Software Zen: delete this;

                  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

                    S Offline
                    S Offline
                    Slacker007
                    wrote on last edited by
                    #9

                    All my applications run to infinity and beyond. ;)

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

                      I avoid while(true) and while(TRUE). I use for(;;) often; in C it is the one construct that gets accepted by all compilers without generating warnings; testing a constant expression in an if or while tends to cause a warning on a lot of compilers. Such "infinite loops" (nothing is infinite AFAIK) are fine: - when they are intended to loop forever (a lot of embedded systems don't care about orderly shutdown); - or when the loop exit condition isn't the first or last thing in the loop. :)

                      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

                        R Offline
                        R Offline
                        Rama Krishna Vavilala
                        wrote on last edited by
                        #11

                        for(;;) is the more common one. I found the first mention of it being somewhat of a standard in the Stourstrup's book - "The C++ Programming language".

                        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
                          Joan M
                          wrote on last edited by
                          #12

                          Typical in PLC programs and machine programming in which you need to create an infinite loop that controls all the behavior of the machine itself. Most of them though have it implicitly working without the programmer knowledge... Also it was sometimes used in oooooold programs in which the sequential programming (that thing that existed before OOP)... It can also be used for testing... It can also be used to crash a computer ==> while(true){a=a+0.00000000001;} // patience... it will explode sometime... well, this can also be used to cook eggs direct on the computer's box... and after that (and even I know you did not asked for that...) I prefer "while(true)" it seems more clear to me...

                          [www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.

                          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

                            H Offline
                            H Offline
                            Hans Dietrich
                            wrote on last edited by
                            #13

                            I agree. There must be a mechanism to exit such loops cleanly.

                            Best wishes, Hans


                            [Hans Dietrich Software]

                            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

                              E Offline
                              E Offline
                              Ennis Ray Lynch Jr
                              wrote on last edited by
                              #14

                              LABEL: ..code GOTO LABEL Much nicer.

                              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                              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 Meech
                                wrote on last edited by
                                #15

                                Would you be talking about an Alderson Loop[^]. :) While the Wiki article is not a definitive reference, it seems that the practical use of infinite loops are minimal. I'd recommend to avoid them entirely. :)

                                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                                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

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

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