SA0081: Upper border is not a constant
Function | Determines FOR statements in which the upper limit is not defined with a constant value. |
Reason | If the upper limit of a loop is a variable value, it is no longer possible to see how often a loop is executed. This can lead to serious problems at runtime, in the worst case to an infinite loop. |
Importance | High |
Samples:
PROGRAM MAIN
VAR CONSTANT
cMax : INT := 10;
END_VAR
VAR
nIndex : INT;
nVar : INT;
nMax1 : INT := 10;
nMax2 : INT := 10;
END_VARFOR nIndex := 0 TO 10 DO // no error
nVar := nIndex;
END_FOR
FOR nIndex := 0 TO cMax DO // no error
nVar := nIndex;
END_FOR
FOR nIndex := 0 TO nMax1 DO // => SA0081
nVar := nIndex;
END_FOR
FOR nIndex := 0 TO nMax2 DO // => SA0081
nVar := nIndex;
IF nVar = 10 THEN
nMax2 := 50;
END_IF
END_FOR