Loop invariants are expressions whose values do not change during the execution of a loop. Placing the calculation of an invariant inside a loop is inefficient because the resulting value will always be the same, yet it must be re-calculated each time through the loop. Therefore invariants should be calculated and stored before loop execution begins.

Noncompliant Code Example

For y = 0 to Height-1
   For x = 0 to Width-1
      i = y*Width + x   ' y*Width is invariant
      '...
   Next x
Next y

Compliant Solution

For y = 0 to Height-1
   Dim temp as Integer = y*Width
   For x = 0 to Width-1
      i = temp + x
      '...
   Next x
Next y