What is the difference between for(;;) and while(1)? [duplicate]
Possible Duplicate:
Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it?
I was wondering what is the difference between for(;;)
and while(1)
when both perform same function? Will there be any difference of speed of compilation
The difference with these is that many compilers will warn about while(true)
("constant expression used as loop expression"), while none I know of warn about for(;;)
.
They should generate the same code, though.
no functional difference at all, just a matter of taste.
With for
, you can do this:
#define ever (; ;) // note the two happy faces? ;)
for ever { ... } // endless loop
Which is not possible with while
.
- Both will cause infinite loop unless
break
is called explicitly. - Personally I prefer while(1), it's more readable
No difference. I prefer the latter.
6 of one, 110 of the other.
The latter appears more concise.
No difference, unless you want to make use some kind counter later as below.
for (int i =0; i < 100; i++) {
// some code
}
Both are the same in C++. However, your post is tagged with c#
and c++
.
If you're using C#, you'll need to use while (true) {...}
. Personally, I prefer the same in C++: numbers are used only when dealing with... well, numbers! When dealing with boolean values, true
and false
are used instead.
They both defines the exact same functional behavior and produce exactly the same IL code from C#.
I'm old-school, I still do the following:
#define TRUE 1
#define FALSE 0
while (TRUE) { /*--do something, mutley--* }
精彩评论