Shop OBEX P1 Docs P2 Docs Learn Events
Repeat loop with negative step caught me offguard — Parallax Forums

Repeat loop with negative step caught me offguard

Agent420Agent420 Posts: 439
edited 2012-12-16 21:58 in Propeller 1
I was coding a simple repeat loop in Spin last night that used a negative step, and couldn't figure out why it wasn't working - it would execute the first value and then quit...

repeat x from 7 to 0 step -1
   blah
   blah
   blah

My Basic and C coding habits caused me to insert the 'step -1' value without thought.· Turns out Spin is smart enough to know when the loop is going backwards and automatically defaults to a step of -1, so I was counteracting it's behavior [noparse]:)[/noparse]

I guess that implementation makes sense, but it's·another one of those Spin things that caught me off guard.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Comments

  • KyeKye Posts: 2,200
    edited 2009-08-28 12:30
    I'd had that happen to me too. Also note that when repeat loops exit the variable they incrment is greater than or less than the ned by step.

    So in your case without the negative step -1 statment the variable x will be left at -1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • photomankcphotomankc Posts: 943
    edited 2009-08-28 13:29
    I still get hosed left and right by the whole => vs >= thing.

    I always like to setup the counter and loop increment myself. I hate trying to remember the details of the short-hand notations so I like to be able to specify the exact termination condition and exactly when the count is incremented.
  • RaymanRayman Posts: 14,650
    edited 2009-08-28 14:06
    Yeah, I posted something about that a while ago... I think it's a bug (but they'll never admit it...)!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
  • CassLanCassLan Posts: 586
    edited 2009-08-28 14:10
    That also got me

    Rick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    NYC Area Prop Club

    Prop Forum Search (Via Google)

    ·
  • Agent420Agent420 Posts: 439
    edited 2009-08-28 14:10
    I don't think it's a bug, but it did cause me to rtfm ;-)
    Propeller Manual said...

    when the "[font=Parallax,Parallax size=2][font=Parallax,Parallax size=2]STEP [/font][/font][font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]Delta[/font][/font]" component is omitted, it uses either +1 or -1 by default, depending on the range of [font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]Start [/font][/font]and [font=Times New Roman,Times New Roman][font=Times New Roman,Times New Roman]Finish

    [/font][/font]
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • CassLanCassLan Posts: 586
    edited 2009-08-28 14:21
    Yes Agent,
    but the bug of it is that if you do put the "step -1" in it should work [noparse];)[/noparse]

    Rick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    NYC Area Prop Club

    Prop Forum Search (Via Google)

    ·
  • jazzedjazzed Posts: 11,803
    edited 2009-08-28 16:28
    If it's a bug, you can fix it in the Spin interpreter and reload cog 0 with your fix [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • mr23mr23 Posts: 21
    edited 2012-12-16 21:26
    I agree with CassLan, this just cost me a half hour. That couldn't possibly be it, could it? I asked myself more than once. Please fix it, Parallax.
    jazzed wrote: »
    If it's a bug, you can fix it in the Spin interpreter and reload cog 0 with your fix [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-16 21:58
    It's not a bug. It's a feature that the step size is always positive and the direction of the step is determined by the starting and ending value of the REPEAT. You may not like it and it may have been a problem for you, but it's the way the language works and it can't be changed.
  • kwinnkwinn Posts: 8,697
    xbwsdu200 wrote: »
    it seems a little difference with other programming language.
    here we should abide the rule of the spin language.

    The law of the land is the law. Or in this case the law of the language. Doing it this way seems logical enough to me. Still, it would be nice if this kind of thing was the same across all languages.
Sign In or Register to comment.