Some people prefer using 'for (;;)', instead of 'while (1)', to write a loop that loops forever. Why?
Code:for (;;) { DOSTUFF; if (...) exit(0); }Why is for(;;) better?Code:while (1) { DOSTUFF; if (...) exit(0); }
This is a discussion on Writing forever loops - using for(;;) or while(1)? within the C Programming forums, part of the General Programming Boards category; Some people prefer using 'for (;;)', instead of 'while (1)', to write a loop that loops forever. Why? Code: for ...
Some people prefer using 'for (;;)', instead of 'while (1)', to write a loop that loops forever. Why?
Code:for (;;) { DOSTUFF; if (...) exit(0); }Why is for(;;) better?Code:while (1) { DOSTUFF; if (...) exit(0); }
Last edited by Hammer; 07-13-2005 at 04:22 PM. Reason: Disabled smilies to make it more readable [Hammer]
Here is a reply I had when this question had come up before.
Originally Posted by Dave Sinkula
Last edited by Dave_Sinkula; 07-13-2005 at 03:38 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
"while (1)" is not readable.
But "(;;)" is secret code for the word, "ever".
:-)
Also, fussy checkers like lint will complain that while(1) is an expression with a constant answer. More often than not, you want your conditions to be actually conditional on something.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
it is a matter of style.
BSD style recommends using for (;![]()
I have never come across a situation where I need an infinite loop. I believe that you always need some kind of 'natural' control over it instead of
Code:for (;;) { if () exit(0); }
I disagree. How would you recommend drawing a loop of this form:Originally Posted by sand_man
You could write it like:Code:top: /* preliminary operations that also get repeated */ if (/* blah */ ) goto end; /* Further operations */ goto top; end:
But much better is simply:Code:/* preliminary operations */ while (! /* blah */) { /* Further operations */ /* preliminary operations COPIED and PASTED! */ }
Code:for (;;) { /* preliminary operations */ if (/* blah */) break; /* Further operations */ }
What is wrong with using a flag to continue looping?
is the same as:Code:top: /* preliminary operations that also get repeated */ if (/* blah */ ) goto end; /* Further operations */ goto top; end:
I know my code does extra condition checks but...Code:int f = 1; while(f){ /*do stuff*/ if(/*you want to leave loop*/){ f = 0; continue; } /*do other stuff*/ }
Last edited by sand_man; 07-15-2005 at 08:12 PM.
There are state machines. And embedded systems which run "forever".
Last edited by Dave_Sinkula; 07-16-2005 at 03:51 PM. Reason: Removed misplaced reply ('continue statement' thread).
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*