Skip to main content

SA0041: Detect possible loop invariant code

Detects assignments in loops that calculate the same value for each loop cycle. Such lines of code could be inserted outside of the loop.

Justification: This is a performance warning. Code which is executed in a loop, but does the same thing in each loop cycle, can be executed outside of the loop.

Importance: Medium

Example 50. Example
PROGRAM PLC_PRG
VAR
    iCounter, iVar1, iVar2 : INT;
END_VAR
FOR iCounter := 0 TO 10 DO
    iVar1 := 100;    // SA0041
    iVar2 := iVar2 + iVar1;
END_FOR

Output in the Messages view:

  • sa_icon_message.png SAN0041: Possible loop invariant code 'iVar1 := 100'