Whats the difference

is there any difference between:

while(true);
and,
do(;;);

closed account (3hM2Nwbp)
Yes, while(true) is valid, do(;;) is not. Were you meaning for(;;)?

while(true) { } and for(;;) { } will accomplish the same thing.

If you meant
while(true) { }
versus
do { } while(true);

Then there is still a difference. The first is a top-driven loop while the second is a bottom-driven loop. The bottom-driven loop is guaranteed to execute the loop body at least one time.
Last edited on
yeah ops, my bad. thanks for correcting me
while(true) evaluates a runtime constant over and over, where as for(;;) doesn't, but I don't know if that makes a difference. In fact, even that possible minute difference will probably be optimized out by the compiler.
Last edited on
so it does not matter what one i use?
closed account (3hM2Nwbp)
It's a matter of style, while(true) { } looks a little cleaner than for(;;) { } in my humblest of opinions. Verbosity in code usually lends a hand to third parties trying to understand it.
Last edited on
thanks, i guess it's what your used to. Thanks for your opinion regarding my issue.
yeah you can also use
1
2
3
while(1)
{
}

it will have the same effect. just dont forget to use break so you dont get stuck in the loops :)
yeah cheers i've learnt loops the hard way lol :)
Mathhead200
...that possible minute difference will probably be optimized out by the compiler.
i.e. They should behave exactly the same.

I like to use while(true) { ... } also Luc Lieber.
Topic archived. No new replies allowed.