0

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

I been reading a programming book(java) there the author uses for(;;) instead of while(true). This thing realy starts to annoy me so I thought I should check which is the best to use and if there are any diffrences. Here are the possible infinite loops:

for(;;){

}

while(true){

}

do{

}while(true)
  • Which is preferble to use?
  • Is there any diffrences in preformance (like you have to test if true is true in the while case) or are they compiled to the same thing?
Community
  • 1
  • 1
nist
  • 1,706
  • 3
  • 16
  • 24
  • 1
    My guess would be that most programmers would recognise the intent of `while(true)` without having to glance twice; so for the sake of keeping to the principle of least confusion, I'd choose that one personally. Ultimately I think it's little more than a style choice really. – Ben Cottrell Apr 11 '12 at 21:21
  • 1
    You forgot one: start: int nop = 1+1; break start; – Mikhail Apr 11 '12 at 21:21
  • 6
    `#define ever ;;` `for(ever)` – Femaref Apr 11 '12 at 21:24
  • 3
    On some compilers while(true) raises a compilation warning (constant condition), while for(;;) doesn't. Therefore if you need a clean C++ build without any warning you may be forced to use for(;;) – Paolo Brandoli Apr 11 '12 at 21:24
  • @Paolo : +1, I'm glad someone finally mentioned that. – ildjarn Apr 11 '12 at 21:35

4 Answers4

6

They are compiled to the same thing, and are merely a matter of style. Older programmers may recognize for(;;), but I would probably just use while(true).

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • It has nothing to do with older; people have been divided between `for(;;)` and `while (1)` since the dawn of C. – Kaz Apr 11 '12 at 21:21
  • @Kaz: `while (1)` is not the same thing as `while (true)` - the first will not work for Java. – user unknown Apr 11 '12 at 21:24
  • 6
    That is correct, but I hope you can appreciate that I could not have written "people have been divided between `for(;;)` and `while (true)` since the dawn of C". – Kaz Apr 11 '12 at 21:28
1

Yeah this is just a matter of style and I prefer while(true) since the intent is clear straight away. for(;;) just doesn't say anything to me.

Hiro2k
  • 5,254
  • 4
  • 23
  • 28
  • 3
    `while(true)` is a kind of nonsense. It's testing a condition that can never change. A true infinite loop has no condition, and `for(;;)` expresses exactly that, by design. – Kaz Apr 11 '12 at 21:24
  • 1
    Holy bike shed batman! I like the idea of testing a condition that can never change because it's guaranteed to execute infinitely. – Hiro2k Apr 11 '12 at 21:32
  • You might like the MIPS instruction set. In MIPS, there is a register R0 which is read-only and permanently holds a zero. Furthermore, the unconditional branch instruction B is just a macro for a "BEQ R0, R0, ": branch if the zero register is equal to itself. – Kaz Apr 11 '12 at 22:12
1

When in doubt, measure.

If nothing else, code up all three versions and look at the generated assembly for each of them. Unless the compiler is very stupid, I will bet real money it will generate the exact same code for all three.

EDIT

FWIW, gcc generates the same code for all three (at least in the dead simple cases that I wrote). while(true) is computable at compile time; there's no need to perform a test, just hardcode a jump back to the beginning of the loop.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

They should be identical performance-wise, but even if they aren't, speed doesn't matter as nuch as readability. The best method is the one that will be soonest recognized as an infinite loop by anyone that looks at the code. I would tend toward while (true) since it the most basic case, but that depends on who is looking at the code.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148