Have you checked if the dates you are using for comparison can't be obtained based on the current row you are reading for your table? Because loop structures in your script can increase the script execution time, so is best to avoid them...
Anyway, if you are right to choose this path you can use something like this:
FOR $(variable) = 0 to 1000
if(field > $(variable), ...)