@wummi said:
Hi Chip,
I found a strange behavior with REPEAT <count> WITH <variable> when count is zero.
PUB go () | i
repeat 0 with i
debug(sdec(i))
{ debug output
Cog0 i = 0
Cog0 i = -1
}
I expected debug(sdec(i)) is never executed, but it executes twice.
I believe this behavior is expected. The Spin document indicates that the count value is normally expected to be a number greater than 0.
The WITH form of the REPEAT statement is a smaller/faster version than the REPEAT FROM statement.
v40 2023-09-21 ● New smaller/faster REPEAT form added for iterating a variable from 0 to n-1, where n > 0.
REPEAT n WITH i 'best way to iterate a variable from 0 to n - 1
REPEAT i from 0 to n - 1 'general equivalent, though WITH needs n > 0
Example:
_clkfreq = 200_000_000
PUB go () | i,n
repeat 0 with i
debug("with loop: ",sdec(i))
n := 0
repeat i from 0 to n-1
debug("from loop: ",sdec(i))
Output:
Cog0 with loop: i = 0
Cog0 with loop: i = -1
Cog0 from loop: i = 0
Cog0 from loop: i = -1
Comments
I believe this behavior is expected. The Spin document indicates that the count value is normally expected to be a number greater than 0.
The WITH form of the REPEAT statement is a smaller/faster version than the REPEAT FROM statement.
OK, i do not now that n must be greater than 0
Undefined is not the same as expected.