Shop OBEX P1 Docs P2 Docs Learn Events
FOR, NEXT question — Parallax Forums

FOR, NEXT question

Spiral_72Spiral_72 Posts: 791
edited 2010-12-24 22:28 in BASIC Stamp
I have used this before on the PC, however a PC have relatively unlimited resources. The BS2 does not:

The following code is silly, does work, but it's here for illustration. If I have a FOR...NEXT loop, and in the loop have a check that can exit the loop, are there any problems with this? Does exiting leave something on a stack? or anything else that may cause a problem?
FOR x=0 TO 100
     DEBUG DEC x
     IF X=77
                   GOTO DoSomethingElse
NEXT
END

DoSomethingElse:
DEBUG "We exited the FOR loop"
END
Thank you again!

Comments

  • davejamesdavejames Posts: 4,047
    edited 2010-12-24 08:05
    Hi Spiral - Merry Christmas!

    Take a look at the ON...GOTO and the BRANCH statements. They appear to leave nothing on the stack. Yes, they would make for a looonnnnggg command line, but no worries otherwise.


    Regards,

    DJ
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-24 15:51
    Great! That's what I hoped. I don't notice a problem yet, but this sort of thing potentially causes bugs I'd chase my tail for hours and days on.

    Thank you sir.
  • ercoerco Posts: 20,256
    edited 2010-12-24 18:45
    As long as you're only using PBasic, the only "residue" from your for/next example is that x=77. Internally, PBasic keeps track of how many nested for/next loops there are, but as long as you don't exceed the max number of nested loops, it shouldn't be a problem or cause any bugs.

    spiral: I see the forecast for Charleston says 32 degrees tonight. Ouch! We had sunny and fairly warm here in Los Angeles today (yes, our week of warm rain finally stopped). Makes me glad I'm not in SC right now!
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-24 21:21
    Erco, Ummmm there's a new angle to consider actually. If I remember correctly, the BS2 will handle 16 nested FOR/NEXT loops (as opposed to an earlier model's eight nested, BS1??). I'll right a program to test that.


    Erco, Man surely you can imagine what a little 2" snow does to our normal daily functions. No bread or milk in the stores, all the kerosene and heaters are sold out, cars littering the roadside, no power..... We are PRAYING for a blizzard..... cause we can't make it into work :) We stay home and all us rednecks get out the 4WD trucks and the ATVs YEEHAW!
  • davejamesdavejames Posts: 4,047
    edited 2010-12-24 22:28
    ...welcome!

    (smiles)

    DJ
Sign In or Register to comment.