There’s one error that C++ programmers, no matter how experienced, tend to make so frequently that we feel it requires a separate section. That error is accidentally swapping the operators
== (equality) and
= (assignment). What makes this so damaging is that it ordinarily does not cause syntax errors—statements with these errors tend to compile correctly and the programs run to completion, often generating incorrect results through runtime logic errors. Some compilers issue a warning when
= is used in a context where
== is expected.
Two aspects of C++ contribute to these problems. One is that any expression that produces a value can be used in the decision portion of any control statement ...