Shop OBEX P1 Docs P2 Docs Learn Events
Spin "Next" Behavior — Parallax Forums

Spin "Next" Behavior

jazzedjazzed Posts: 11,803
edited 2010-09-30 00:31 in Propeller 1
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

  • kuronekokuroneko Posts: 3,623
    edited 2010-09-29 17:00
    I don't see anything wrong with that (maybe you expect it to do something different). You call next and it continues with the next iteration (i.e. condition evaluation). Which happens to be at the bottom line and in most cases (unfortunately) includes the exit condition.
  • jazzedjazzed Posts: 11,803
    edited 2010-09-29 17:08
    Why should next behavior include checking the exit condition?
  • jazzedjazzed Posts: 11,803
    edited 2010-09-29 17:12
    Manual v1.1:

    "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.
  • jazzedjazzed Posts: 11,803
    edited 2010-09-29 17:17
    "NEXT is one of two commands (NEXT and QUIT) that affect REPEAT loops. NEXT causes any further statements in the REPEAT loop to be skipped and the next iteration of the loop to be started thereafter."
  • kuronekokuroneko Posts: 3,623
    edited 2010-09-29 17:17
    jazzed wrote: »
    Manual v1.1:

    "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."

    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.
  • localrogerlocalroger Posts: 3,452
    edited 2010-09-29 17:21
    Jazzed, having written a couple of bytecode interpreters myself over the years I'd say my own implementation of NEXT, as it's described in the Prop manual, would mean branching to the last statement of the loop, not back to the first. If there is no end condition then the last statement is an invisible jump back to the start, but if the last statement is an exit test, it would be performed. I would consider an implementation that skipped such an end test a major bug factory for a lot of other unrelated reasons.
  • jazzedjazzed Posts: 11,803
    edited 2010-09-29 17:22
    Manual v1.1:
    jazzed wrote: »
    "NEXT is one of two commands (NEXT and QUIT) that affect REPEAT loops. NEXT causes any further statements in the REPEAT loop to be skipped and the next iteration of the loop to be started thereafter."
  • lonesocklonesock Posts: 917
    edited 2010-09-29 17:27
    I interpret "further statements in the REPEAT loop" to mean anything indented, thus I would expect the exit condition to be evaluated as it's at the same level as the REPEAT command.

    Jonathan
  • jazzedjazzed Posts: 11,803
    edited 2010-09-29 17:57
    This is just another friggin' BASIC Propeller eccentric hurdle to overcome.

    Maybe the next guy who expects anything different will learn from this thread.
  • localrogerlocalroger Posts: 3,452
    edited 2010-09-29 18:07
    No sure what you mean Jazzed but I'm a long-time BASIC guy, and Spin owes a lot to PBasic. I think Lonesock said it better than me because he was focusing on Spin and Spin makes it even clearer than Basic what's going on here. Maybe the problem is that it's really hard to do this in C-like syntax, which is just another reason C-like syntax sucks.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-29 18:45
    This is clearly one of those unusual cases that needs to be spelled out more clearly in the documentation, but it's not a bug. The UNTIL or WHILE clause is part of the REPEAT statement and such a clause placed at the end of the loop code is properly executed by the NEXT statement.
  • Heater.Heater. Posts: 21,230
    edited 2010-09-29 21:15
    Jazzed,

    "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:
    Why should next behavior include checking the exit condition?

    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.
  • Andrey DemenevAndrey Demenev Posts: 377
    edited 2010-09-29 21:23
    That is how it is intended to work, exactly like in other languages. Compare:
    andrey@debian:/tmp$ gcc -o test test.c
    andrey@debian:/tmp$ cat test.c
    #include <stdio.h>
    
    int main(void)
    {
    	int i = 1;
    	do {
    		printf("%d\n", i++);
    		continue;
    	} while (i < 10);
    }
    andrey@debian:/tmp$ ./test 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    andrey@debian:/tmp$ cat test.php 
    <?php
    $i = 1;
    do {
    	printf("%d\n", $i++);
    	continue;
    } while ($i < 10);
    
    andrey@debian:/tmp$ php test.php 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    andrey@debian:/tmp$ 
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-09-29 21:34
    Jazzed,

    There's no other logical way to do it, other than the way Spin handles it. Otherwise, something like this would be totally nonsensical:
    i := 0
    repeat
      do stuff
      if (condition)
        next
      do more stuff
    until ++i == 10
    
    Would you have it go into an infinite loop instead?

    The loop condition is not part of the loop.

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2010-09-29 22:46
    Thanks Andrey. I needed that.

    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.
  • mparkmpark Posts: 1,305
    edited 2010-09-30 00:19
    jazzed wrote: »
    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?
  • Heater.Heater. Posts: 21,230
    edited 2010-09-30 00:31
    Odd but my version of the Prop manual here has no example of a "repeat bla bla until something" as posted by Jazzed. Although it does get a mention in the formal syntax description.

    I might never have thought to use it.
Sign In or Register to comment.