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.
The classic gotcha in C/C++ is the construct
It is syntactically valid: it puts the value of <code>b</code> into <code>a</code> and then executes <code>code</code> if <code>a</code> is non-zero. Sometimes this is even intended. However most commonly it is a typo: the programmer probably meant
which executes <code>code</code> if <code>a</code> and <code>b</code> are equal. Modern compilers will usually generate a warning when encountering the former construct (conditional branch on assignment, not comparison), depending on compiler options (e.g., the <code>-Wall</code> option for gcc). To avoid this gotcha, some programming languages such include specific syntax for when this is desired behavior, such as Python's "walrus" operator (<code>:=</code>). In languages where this specific syntax does not exist, there is a recommendation to keep the constants in the left side of the comparison, e.g. <code>42 == x</code> rather than <code>x == 42</code>. This way, using <code>=</code> instead of <code>==</code> will cause a compiler error (see Yoda conditions). Many kinds of gotchas are not detected by compilers, however.