Spin "Next" Behavior
jazzed
Posts: 11,803
Anyone seen something like this before? The docs suggest the 2nd fragment should work. Next seems to only work right in a "repeat variable from begin to end" :mad:
' this works ' repeat n from 0 to 100 result := read if (result & $ff) == $FF next if result & $C0 next if result & $f == $f next if result & $8 quit
' this fails ' repeat result := read if (result & $ff) == $FF next if result & $C0 next if result & $f == $f next until result & $8
Comments
"NEXT is typically used as an exception case, in a conditional statement, in REPEAT loops to move immediately to the next iteration of the loop."
This does not say stop and check the end exit condition statement.
It is a bug as far as I'm concerned. Caveat Emptor.
Define next iteration of the loop! It may not mean jump to the top but do the condition evalution (if any) for the loop. In you first example it would jump to the top part, in the second to the bottom line.
Jonathan
Maybe the next guy who expects anything different will learn from this thread.
"This is just another friggin' BASIC Propeller eccentric hurdle to overcome."
In this case I disagree. I think it is intuitive that "next" bails out of the current iteration, the indented stuff, and immediately tries the loop construct again including the loop condition. Is this not what would be expected from similar statements in other languages?
To have "next" move on to the next iteration, the indented stuff, without checking the condition would lead to a lot of unexpected chaos.
I do think that "next" would more rightly be "continue" as in C.
The documentation sees to be a little ambiguous.
Edit: further:
Because it is possible that if it were not so then "next" might cause the loop to move on to an iteration that is disallowed by the loop condition. For example "until n = 10" might get skipped and the loop continues as n is incremented through 11, 12, 13... until it wraps around again. I think most would consider this unexpected behaviour.
There's no other logical way to do it, other than the way Spin handles it. Otherwise, something like this would be totally nonsensical:
Would you have it go into an infinite loop instead?
The loop condition is not part of the loop.
-Phil
Honestly, I almost never use the do while form in C.
For some reason I feel compelled to do it in Spin - no idea why.
Pure conjecture, but maybe it's because the repeat...while/until form provides a visual "closing bracket" that you find helpful?
I might never have thought to use it.