while (true) and for (; ; ) [modified]
-
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
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
-
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
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
-
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
Agreed.
Software Zen:
delete this;
-
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 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
-
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
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
-
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
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;
-
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
All my applications run to infinity and beyond. ;)
-
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 avoid
while(true)
andwhile(TRUE)
. I usefor(;;)
often; in C it is the one construct that gets accepted by all compilers without generating warnings; testing a constant expression in anif
orwhile
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.
-
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
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".
-
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
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.
-
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
I agree. There must be a mechanism to exit such loops cleanly.
Best wishes, Hans
-
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
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
-
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
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]
-
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 use them although not often. I also often add an empty IDisposable to classes in C# because I like to wrap things in "using" :~
-
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
Real men use "goto" :p
-
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
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.
-
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
-
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 use them although not often. I also often add an empty IDisposable to classes in C# because I like to wrap things in "using" :~
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
-
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
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)