I'm not saying anyone is wrong? Or that you should do it any other way... I still find the syntax of "for(;; )" very visually awkward. I'm only expressing distaste in the style, despite the fact that you deem it preferred.
Do what you want, but if complaints never arise, then change won't occur. Maybe in future languages the aspect will be addressed because someone like me said "damn that's ugly lookin fosho."
:-)
I agree though, while(1) has a very... odd appearance, the 1 is just as unnecessary as the ';;' should be... why can't 'true' be assumed?
What I find as ironic, is that the two most popular looping methods provide a mechanism to break out, yet we ignore it and provide the condition to break free within... that seems funny to me.
Last edited by simpleid; 08-16-2007 at 12:48 PM.
It is assumed on for-loops, hence the "for(;" works. I've seen people us a macro (e.g. "forever" to do "for(;
".
I usually try to use a condition in my while/for-loops - and in the above case, I suppose I should have used a "do ... while(fwrite(...) > 0);" instead.What I find as ironic, is that the two most popular looping methods provide a mechanism to break out, yet we ignore it and provide it within.
--
Mats
I didn't say it was preferred -- I said it was typical. I've seen and used both.
Heck, if you want:
Code:#define forever for(;;) forever { ... }Sometimes the natural place to break the loop is not at the beginning or end. Contorting your code so that the break is triggered by the loop condition can sometimes result in a big mess that's hard to figure out. I'm not making any judgement either way about this PARTICULAR case, since I haven't bothered looking at it in detail.What I find as ironic, is that the two most popular looping methods provide a mechanism to break out, yet we ignore it and provide the condition to break free within... that seems funny to me.
oh i know, i just wanted to try and make a joke out of it. heh.
however, the 'forever' really does look clean... might have to start using that...
> however, the 'forever' really does look clean... might have to start using that...
I wouldn't do that. It's only slightly shorter, and evil, being a macro. If forever is ever made a built-in keyword, that would be different.
Edit: The exact same length, actually, assuming no white space in for(;;). But someone seeing the latter doesn't have to hunt around to verify that it means what they think it does.
Last edited by robatino; 08-16-2007 at 02:45 PM.
Without looking at it much in detail, I daresay the example in question:I'm not making any judgement either way about this PARTICULAR case, since I haven't bothered looking at it in detail.
Can be just as easily expressed as:Code:for(;;) { for(i = 0; i < 1000; i++) buf[i] = n+i; n += 1000; if (fwrite(f, buf, sizeof(buf), 1) <= 0) break; }
Thus sidestepping this "which deliberate infinite loop is better?" argument, but creating a "is a do while loop better than a deliberate infinite loop where both will suffice?" argumentCode:do { for(i = 0; i < 1000; i++) buf[i] = n+i; n += 1000; } while (fwrite(f, buf, sizeof(buf), 1) > 0);![]()
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)