Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed typos; dropped trivial "Applications" section

Do not use the assignment operator in conditional expressions because it typically frequently indicates programmer error and can result in unexpected behavior.  This means that the assignment operator should not be used in the following contexts:

...

In this noncompliant code example, an assignment expression is in the controlling expression is used with , as an operand of the && operator. Since && is not a comparison operator, assignment is an illegal operand.

Code Block
bgColor#FFcccc
public void f(boolean a, boolean b, boolean flag) {
  while ( (a = b) && flag ) {
    /* ... */
  }
}

...

When the assignment is intended, one of the same solutions should be used as shown above.

Applicability

Errors of omission can result in unintended program flow. 

ISO/IEC TR 24772:2010

"Likely Incorrect Expression [KOA]"

MITRE CWE

CWE ID 480, "Use of Incorrect Operator"

...