5

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

Community
  • 1
  • 1
abhinav
  • 151
  • 4
  • 11
  • 6
    How can you close a __C++ best-practice__ question pointing to __C# best-practice__ answers? Sorry, but this is absurd. Voted to re-open. – sbi Dec 04 '10 at 10:53
  • will there be any difference in the compilation speed!!generally programmers are said to use while rather than for – abhinav Dec 04 '10 at 11:16
  • @sbi: I think the confusion results from the answers being virtually equivalent between the two questions. But I agree, this *is* a distinctly different question. – Cody Gray - on strike Dec 04 '10 at 11:41
  • 1
    If anything, this is a more appropriate duplicate: http://stackoverflow.com/questions/2611246/is-for-faster-than-while-true-if-not-why-do-people-use-it – Cody Gray - on strike Dec 04 '10 at 11:42
  • @Cody: It is indeed. Is it an _exact_ duplicate? Shall we close this one pointing to it? (And: Even if the answers to best-practice syntax questions are virtually equivalent between languages, _best practice syntax_ IMO still warrants language-specific questions. And my answer doesn't even apply to C#, since it doesn't have as many compilers as C++ has.) – sbi Dec 04 '10 at 11:51

10 Answers10

9

no functional difference at all, just a matter of taste.

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
9

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.

sbi
  • 219,715
  • 46
  • 258
  • 445
8

With for, you can do this:

#define ever (; ;) // note the two happy faces? ;)

for ever { ... }   // endless loop

Which is not possible with while.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
6
  • Both will cause infinite loop unless break is called explicitly.
  • Personally I prefer while(1), it's more readable
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
4

No difference. I prefer the latter.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • will there be any speed difference during the time of compilation..another thing saurav did u see my other quwstion the automatic gear shift i,m looking forward to u ans that question!! – abhinav Dec 04 '10 at 08:52
  • @abhinav : No the machine code generate would most probably be same in both the cases. As far as I can see you have already accepted an answer to that question. – Prasoon Saurav Dec 04 '10 at 08:54
  • @saurav i accepted the answer but i'm still looking forward to a different approach.. – abhinav Dec 04 '10 at 09:03
  • @saurav can you plz tell me which book should i refer for c++ i'm doin engg 1st year and i don't have much idea bout the lang – abhinav Dec 04 '10 at 09:21
  • 1
    @abhinav : Read C++ Primer by Stan Lippman. – Prasoon Saurav Dec 04 '10 at 09:24
4

6 of one, 110 of the other.

The latter appears more concise.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
0

No difference, unless you want to make use some kind counter later as below.

for (int i =0; i < 100; i++) {
   // some code
} 
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
0

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.

ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
0

They both defines the exact same functional behavior and produce exactly the same IL code from C#.

VdesmedT
  • 9,037
  • 3
  • 34
  • 50
-1

I'm old-school, I still do the following:

#define TRUE 1  
#define FALSE 0  
while (TRUE) { /*--do something, mutley--* }
Joel
  • 4,732
  • 9
  • 39
  • 54
Simon Catlin
  • 2,141
  • 1
  • 13
  • 15