Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: c/equivalence/equality operator/a

...

Anchor
nce_inequality_multistep
nce_inequality_multistep

Noncompliant Code Example (

...

Equality Operators)

This noncompliant code example may appear to have 5 iterations, but in fact, the loop never terminates.

...

Anchor
cs_relational_multistep
cs_relational_multistep

Compliant Solution (

...

Relational Operator)

Using the relational operator <= instead of an equivalence equality operator guarantees loop termination.

...

Anchor
nce_inequality
nce_inequality

Noncompliant Code Example (

...

Equality Operators)

It is also important to ensure termination of loops where the start and end values are variables that might not be properly ordered. The following function assumes that begin < end; if this is not the case, the loop will never terminate.

...

Anchor
cs_relational
cs_relational

Compliant Solution (

...

Relational Operator)

Again, using a relational operator instead of equivalence guarantees loop termination. If begin >= end the loop never executes its body.

...

Anchor
nce_boundary
nce_boundary

Noncompliant Code Example (

...

Boundary Conditions)

Numerical comparison operators do not always ensure loop termination when comparing against the minimum or maximum representable value of a type, such as INT_MIN or INT_MAX:

...

Anchor
cs_boundary
cs_boundary

Compliant Solution (

...

Boundary Conditions)

A compliant solution is to compare against the difference between the maximum representable value of a type and the increment.

...