Random question of the day -
Why is "for( ;; )" an infinite loop?
The first and third parts don't matter, so does that mean an empty expression evaluates to true?
How come "if ()" and "while ()" aren't allowed then?
Thanks!
Random question of the day -
Why is "for( ;; )" an infinite loop?
The first and third parts don't matter, so does that mean an empty expression evaluates to true?
How come "if ()" and "while ()" aren't allowed then?
Thanks!
Yes. In for loops, at least.
Because the designers didn't like that, I guess ;-). Empty doesn't always evaluate to true, but the designers of C added an exception to the conditional statement in the for loop. Just because they considered that a lot more useful than "while()", I guess.
And I agree, they were right on this decision.
I've always usedwhen I want an infinite loop because it makes sense to me. It seems like most people preferCode:while (true)though.Code:for (;;)
Why shouldn't what be what?Why shouldn't it be?
I, personally, prefer while(true). But I guess it's just a matter of style.
Why shouldn't for (;;) denote an infinite loop?Originally Posted by cyberfish
I've read an ancient text on optimisation in C that recommended for (;;) over while (1) because compilers back then supposedly might not recognise while (1) as an infinite loop, and end up generating less efficient code.Originally Posted by EVOEx
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Am I the only person to give the "do" a little love?
Soma
Sorry, not marriedOriginally Posted by phantomotap
But no, not for (controlled) infinite loops.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, I read about that too. But I'm not going to write code I consider less clear because there exist some fairly bad compilers that aren't able to work with it? No doubt that some other compilers won't be able to do other things properly that are fairly common. I'm not going to bother to keep up with all of them.
Should it ever be required that I'd write code for such a compiler, I'd change those things in the end, if required...
>_<Sorry, not married
GM > You've dealt `Soma' +3(1d4) point(s) of damage.
Soma
My personal favorite:
Code:o:goto o;
Last edited by Sebastiani; 04-26-2010 at 09:52 AM.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Code:o_O : { //... goto o_O; }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Another more current reason is that in some compilers you can get a warning about a loop expression being constant in while (true) etc, which is not a problem with for(;
"do while" is mostly a good choice as it is logically the simplest form of loop, obviously resulting in just a single branch instruction. However it also falls prey to generating a warning on some compilers.
Last edited by iMalc; 04-26-2010 at 01:23 PM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
But the question remains... why is for(;; ) infinite loop .
Well let's consider the available options:
for(;; ) means loop forever
for(;; ) means don't loop
for(;; ) is a grammar mistake
Maybe you feel that all three parts should have expressions but that would be bad for loops without meaningful initialization or update steps, like, I don't know, event driven loops. And if you're not going to require all three, then the question becomes "why is for(; true; ) an infinite loop?"