Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

It is less desirable in general, depending on what was intended, because it mixes the assignment in the condition, but it is clear that the programmer intended the assignment to occur.

Noncompliant Code Example

In this noncompliant example, the expression x = y is used as the controlling expression of the while statement.

Code Block
bgColor#FFcccc
langc
 do { /* ... */ } while ( foo(), x = y ) ;

Compliant Solution

When the assignment of y to x is not intended, this conditional block is now executed when x is equal to y.

Code Block
bgColor#ccccff
langc
do { /* ... */ } while ( foo(), x == y ) ; 

When the assignment is intended, the following is an alternative compliant solution:

Code Block
bgColor#ccccff
langc
do { /* ... */ } while ( foo(), (x = y) != 0) ;

It is less desirable in general, depending on what was intended, because it mixes the assignment in the condition, but it is clear that the programmer intended the assignment to occur.

Compliant Example

In this compliant example,the expression x = y is not used as the controlling expression of the while statement.

Code Block
bgColor#ccccff
langc
do { /* ... */ } while ( x = y, p == q ) ;  

Exceptions

  1. Assignment can be used where the result of the assignment is itself a parameter to a comparison expression or relational expression

    Code Block
    bgColor#ccccff
    langc
    if ( ( x = y )  != 0  ) { /* ... */ } 
  2. Assignment can be used where the expression consists of a single primary expression

Risk Assessment

Errors of omission can result in unintended program flow.

...

Bibliography

[Hatton 1995]Section 2.7.2, "Errors of Omission and Addition"

...