In a Pascal "for" loop the value of the index variable, in your case "i", is undefined when the loop ends.
Therefore, the lines 13 and 14 of your code are using a value which could be anything. That's the reason you are getting a violation.
If you need to depend on the value of "i" when the loop ends, change it from a "for" to a "while" where you update the index yourself.
HTH.