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!
Printable View
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.
O_o
Why shouldn't it be?
Soma
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?Quote:
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?Quote:
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.Quote:
Originally Posted by EVOEx
Am I the only person to give the "do" a little love?
Soma
Sorry, not married :pQuote:
Originally Posted by phantomotap
But no, not for (controlled) infinite loops.
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...
>_<Quote:
Sorry, not married
GM > You've dealt `Soma' +3(1d4) point(s) of damage.
Soma
My personal favorite:
:DCode:o:goto o;
Code:o_O
:
{
//...
goto o_O;
}
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.
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?"