IVNU -- Iteration Variable is Not Used in Loop Body

From RAD Studio
Jump to: navigation, search

Go Up to C++ Audits

Description

It is a common programming error to incorrectly use an iteration variable from an exterior loop inside the body of an inner loop. This audit detects inner loops where the iteration variable is not used inside of the inner loop body, but the iteration variable of the exterior loop is used instead.

Incorrect:

 int sum = 0;
 for (int i = 0; i < lengthX; i++) { 
   for (int j = 0; j < lengthY; j++) {
     sum += arr[i][i]; 
   }

Correct:

 int sum = 0;
 for (int i = 0; i < lengthX; i++) { 
   for (int j = 0; j < lengthY; j++) {
     sum += arr[i][j]; 
   }
 }

See Also