Do not use the assignment operator in the outermost expression of a selection statement (if
or switch
) or an iteration statement (while
, do
, or for
) because it typically indicates programmer error and can result in unexpected behavior.following cases because it typically indicates programmer error and can result in unexpected behavior:
- Controlling expression of
if, switch
(selection statement) - Controlling expression of
while
,do...while
(iteration statement) - S
econd operand of for
(iteration statement) - First operand of
?:
(selection statement) - Either operand of
|| or &&
(logical operators) - Second operand of comma operator when the comma expression is used in any of these contexts
- Second and third operands of ?: (selection statement) where the ternary expression is used in any of these contexts
Noncompliant Code Example
...
[Hatton 1995] | Section 2.7.2, "Errors of Omission and Addition" |
---|