10

I've recently come across a while statement that uses 1 == 1 instead of true.

Example:

while (1 == 1) 
{
   // Do something
}

Instead of:

while (true)
{
   // Do something
}

They both appear to be correct and generate the same result but I wanted to know (apart from why a developer would use 1 == 1 instead of true - style/habit aside) what impact this has from a compiler perspective, is there a greater overhead in using the comparison operator instead of true?

Steve
  • 144
  • 1
  • 8
  • 5
    i think, there is no difference, just habbit – Backs Sep 24 '15 at 15:12
  • wouldn't `1==1` require an extra comparison each time the condition is checked? Or will the compiler optimize this? – mmoment Sep 24 '15 at 15:13
  • I've never seen the reason for while(true) or while(1 == 1), it seems lazy to me, why not have an actual variable that can be set to false to exit the loop? – Callum Bradbury Sep 24 '15 at 15:14
  • 4
    Use `for(;;)` and save a few bytes ;) Good for [code golf](http://codegolf.stackexchange.com) – James Webster Sep 24 '15 at 15:14
  • I'm sure 1 and 1 use the compare to see that they are equal. Whereas true is true. – g williams Sep 24 '15 at 15:14
  • 1
    @CallumBradbury, I believe I have used `while (true)` for a thread that I want to run continually until the entire program ceases – James Webster Sep 24 '15 at 15:14
  • I did some reading on this after writing my comment, and found a good post by the Skeetster - although I'm still not entirely convinced I take back my 'never seen the reason'. – Callum Bradbury Sep 24 '15 at 15:19
  • 4
    Voted to reopen. There *is* a definite answer to the question, as @DavidL has proved with his answer. – Dmytro Shevchenko Sep 24 '15 at 15:21
  • @CallumBradbury: can you give a link to the _good post by the Skeetster_ – PaulF Sep 24 '15 at 15:23
  • 1
    @DmytroShevchenko thanks, I've edited the question anyway to meet with the question guide. This also makes the answer from DavidL's even more relevant. – Steve Sep 24 '15 at 15:37
  • @PaulF http://stackoverflow.com/questions/6850380/are-whiletrue-loops-so-bad – Callum Bradbury Sep 25 '15 at 08:27
  • @CallumBradbury: Thanks for that - interesting reading. Just reminds me of the major arguments over where opening/closing braces should be placed - at the end of the day, just a matter of personal preference. – PaulF Sep 25 '15 at 09:45
  • Very good edit. Now worthy of an upvote, not a close vote :) – James Webster Sep 30 '15 at 14:25

1 Answers1

15

There is no difference. The compiler will optimize them to the same IL.

1 == 1

IL_0000:  nop         
IL_0001:  br.s        IL_0005
IL_0003:  nop         
IL_0004:  nop         
IL_0005:  ldc.i4.1    
IL_0006:  stloc.0     // CS$4$0000
IL_0007:  br.s        IL_0003

true

IL_0000:  nop         
IL_0001:  br.s        IL_0005
IL_0003:  nop         
IL_0004:  nop         
IL_0005:  ldc.i4.1    
IL_0006:  stloc.0     // CS$4$0000
IL_0007:  br.s        IL_0003

Any choice of one or the other is purely stylistic preference on the part of the developer.

David L
  • 32,885
  • 8
  • 62
  • 93
  • 2
    * `1 == 1` screams, "mind your step; something funny is happening here"; * `while(true)` could be mistaken for a function call (c.f. `DoStuff(true)`) when skimming through the code. – Kasper van den Berg Sep 24 '15 at 15:22
  • 2
    @KaspervandenBerg That's certainly an excellent point on writing good self-documenting code. Although personally, i think either case would benefit from a LARGE block of documentation around it since you need a very good reason to do this. – David L Sep 24 '15 at 15:23
  • Thanks @DavidL, I completely forgot about using ildasm to compare the two statements at an IL level. – Steve Sep 24 '15 at 15:33
  • 1
    @Steve Happy to help. An additional, useful tool for this type of thing is LinqPad. It won't always include every instruction but it will capture the most important steps to give you a quick breakdown. – David L Sep 24 '15 at 15:33
  • Since the semantics are identical I think the developer's preference can only be stylistic or optical, or maybe they are lacking a finger. – Peter - Reinstate Monica Sep 24 '15 at 15:34
  • @PeterSchneider While the definition of semantic certainly works for how I've used it above, I've updated it to be clearer per your point. – David L Sep 24 '15 at 15:35
  • Thanks again @DavidL - I have LinqPad but often neglect to use it and it didn't even occur to me that I could view the IL from within it - I shall be using it more in future! – Steve Sep 24 '15 at 15:43
  • This is true for a compiled language. For interpreted languages there are often special cases for true that would save some instructions. – Alex Court Sep 24 '15 at 21:18