0

Possible Duplicate:
Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it?

I would like to know which infinite loop is more preferable:

for(;;) {
    // Do some stuff
}

or

#define TRUE 1

while(TRUE) {
    // Do some stuff
}

Is there any difference between them from the performance point of view?

What is more preferable from coding standards point of view?

Community
  • 1
  • 1
Alex
  • 9,891
  • 11
  • 53
  • 87
  • Or `while (1)` or `while (true)` if you're in C++ or you've included `` or ... How many angels can dance on the head of a pin? It is an unanswerable question; there is no functional difference, and it is only a matter of taste. – Jonathan Leffler Nov 01 '12 at 11:16
  • 3
    (I was typing this as answer): The only reason I prefer `for (;;)` is that the VC compilers emit the following warning when `while (true)` or `while (1)` construct is used and the warning is set to the highest level (`W4`): `warning C4127: conditional expression is constant` which is very annoying, particularly when warnings are being treated as errors (`WX`). – hmjd Nov 01 '12 at 11:20
  • 2
    I don’t understand all the downvotes, this looks like a totally valid question. – Konrad Rudolph Nov 01 '12 at 11:26
  • @hmjd, look at answers to the question that this one is duplicate of, you will find it there. – Griwes Nov 01 '12 at 11:29
  • 1
    @KonradRudolph It seems like downvoting because of duplicate. – Alex Nov 01 '12 at 11:30
  • @Griwes, yep I seen that. Cheers. – hmjd Nov 01 '12 at 11:30

5 Answers5

4

From a performance point of view it doesn't matter. The compiler will optimize both into the same assembly code. From a "coding standards" point of view, for(;;) is more terse, which is nice. Although while(TRUE) is a bit clearer, for(;;) is so common that clarity in this case is not much of an issue (in fact, the ubiquity of for(;;) may make it more clear than the while version).

Alex V
  • 3,416
  • 2
  • 33
  • 52
1

No difference. while (true) might look more obvious to some.

Kos
  • 70,399
  • 25
  • 169
  • 233
1

Is there any difference between them from the performance point of view?

No, none whatsoever.

I use for (; ;) but the reason is whimsical: it kind of looks like for (ever) … (and that’s kind of an established idiom in the C++ community).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
1

It's matter of taste, but especially matter of coding conventions used. Preferably one style should be used everywhere in same project, or same rules used in picking the style when they will be mixes. Many C++ frameworks also provide forever as macro, which in my opinion should then be preferred in code using the framework. There should be no difference in generated code.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • “many … frameworks” – can you name an example? I’ve never seen this in a real framework. I’d actually count this as a reason against using the framework (gratuitous macro use!) – Konrad Rudolph Nov 01 '12 at 11:21
  • At least Qt. Also I've used some internal frameworks with forever. In Qt you can disable it though to avoid namespace clutter. – hyde Nov 01 '12 at 11:25
-1

The difference between the two is not big. You usually use for when you know how many elements to traverse. You use while if instead a condition needs to be satisfied

ISTB
  • 1,799
  • 3
  • 22
  • 31