barrym95838 wrote:
You can choose your own behavior for a step of zero ... I think I have seen different treatments.
I see what you mean. When the step value is zero, is it an up or a down counting loop when testing the end condition?
Arbitrarily declare that 0 is treated as up counting. If a programmer wants to play tricks like that, they should make their own loop with GOTO and IF statements; it will probably be easier to understand.
There is another complication: overflow of the loop counter. Unless something special is done, there is no way to stop a loop at 32767 ($7FFF) as adding the step overflows to -32768 ($8000). And it is not only the discontinuity at $7FFF/$8000 either since the step can be any value.
The algorithm for terminating an upward counting loop has to be something like:
Code:
if the loop variable before adding the step < 0 or the terminating value < 0:
do a signed comparison
else:
do an unsigned comparision
The opposite would be true for a downward counting loop.
Things could get very ugly since the terminating and step values may be in variables manipulated by the loop body.