In programming, a gotcha is a valid construct in a system, program or programming language that works as documented but is counter-intuitive and almost invites mistakes because it is both easy to invoke and unexpected or unreasonable in its outcome.
if (a=b) code;
It is syntactically valid: it puts the value of
a and then executes
a is non-zero. Sometimes this is even intended. However most commonly it is a typo: the programmer probably meant
if (a==b) code;
b are equal. Modern compilers will generate a warning when encountering this construct. To avoid this gotcha, there is a recommendation to keep the constants in the left side of the comparison, e.g.
42 == x rather than
x == 42. This way, using
= instead of
== will cause a compiler error.
|Look up gotcha in Wiktionary, the free dictionary.|