Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Why do some people write if(0 == x) instead of if(x == 0)?
Why do some people write if(0 == x) instead of if(x == 0)?
✍: Guest
It's a trick to guard against the common error of writing
if(x = 0)
If you're in the habit of writing the constant before the ==, the compiler will complain if you accidentally type
if(0 = x)
Evidently it can be easier for some people to remember to reverse the test than to remember to type the doubled = sign. (To be sure, accidentally using = instead of == is a typo which even the most experienced C programmer can make.)
On the other hand, some people find these reversed tests ugly or distracting, and argue that a compiler should warn about if(x = 0). (In fact, many compilers do warn about assignments in conditionals, though you can always write if((x = expression)) or if((x = expression) != 0) if you really mean it.)
2015-05-15, 1275👍, 0💬
Popular Posts:
If client side validation is enabled in your Web page, does that mean server side code is not run? W...
How Do I Run JUnit Tests from Command Window? To run JUnit tests from a command window, you need to ...
Can you prevent a class from overriding ? If you define a class as “Sealed” in C# and “NotInheritab...
Can two catch blocks be executed? No, once the proper catch section is executed the control goes fin...
How comfortable are you with writing HTML documents entirely by hand? Everyone has a different prefe...