1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

Thursday, April 2, 2009

What is the difference between if(0 == x) and if(x == 0)?

Nothing!. But, it's a good trick to prevent the common error of writing


if(x = 0)


The error above is the source of a lot of serious bugs and is very difficult to catch. If you cultivate the habit of writing the constant before the ==, the compiler will complain if you accidentally type


if(0 = x)


Of course, the trick only helps when comparing to a constant.

 
# #