Shop OBEX P1 Docs P2 Docs Learn Events
FOR.....NEXT loop questions — Parallax Forums

FOR.....NEXT loop questions

Buck RogersBuck Rogers Posts: 2,187
edited 2014-03-19 10:34 in BASIC Stamp
Hello!
An interesting curiosity concerning the use of the FOR....NEXT loop and the way it works.
In the other BASIC dialects one can construct and run multiple loops that way by properly placing the right variables onto the NEXT statement.

From that method the user can write a program who manages a series of FOR....NEXT loops who will do something each time each variable each time they are incremented. However PBASIC does support that feature, thankfully, but the code block below does not work that way.

Consider this block of code from my latest problem, ah, program:
SEROUT io, baud, [DEFWIN, 1, 20, 2, 9, 18, USEWIN, 1, CHGCLR, 12]
SEROUT io, baud, [DEFCLR, 12, $AD, $0C]
FOR W0 = 1000 TO 1050
PULSOUT U1A, 64
PULSOUT U1B, 64
'PULSIN  U1C, 64, U
COUNT U1C, 64, U
SEROUT io, baud, [" ", DEC W0,CR]
PAUSE 1400
SEROUT io, baud, ["__>", DEC U,CR]
PAUSE 700
NEXT
With that one it will start with that number and add a one to it until it reaches the end and store the amount in the chosen variable. But however I want a second loop to run starting at 65 and run until it reaches 127, and it would then store the amount in the variable U. Chances are that when I put both the U, and the W0 ones on the NEXT statement and separate them by commas, or even enter a NEXT U and then a NEXT W0, there will be a problem of some sort.

It was my understanding that this BASIC dialect largely followed earlier ideas on the language as applied to integers.

And yes Phil that is from my latest accomplishment concerning the backpack.

The original program as written was supposed to do that. Earlier on after evaluating it and an approximate model, I came to the disturbing conclusion that the W0 register would be altered by the U variable.

Comments

  • ercoerco Posts: 20,257
    edited 2014-03-12 19:21
    Heck, just start typing and trying. Nothing to lose, you can't blow anything up. Much faster than posting and waiting.

    U VAR BYTE
    FOR W0 = 1000 TO 1050
    FOR U=65 TO 127
    NEXT
    NEXT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-03-13 08:21
    erco wrote: »
    Heck, just start typing and trying. Nothing to lose, you can't blow anything up. Much faster than posting and waiting.

    U VAR BYTE
    FOR W0 = 1000 TO 1050
    FOR U=65 TO 127
    NEXT
    NEXT

    Unfortunately doing this this way will cause an immediate issue with the variables. I see Buck has used the same variable references...unfortunately when you declare U as a byte it will use the lower byte of W0 by default. So using W0 directly as a variable reference means that every time W0 increments, it changes variable U. It's best to declare all your variables at the start of your code and not use the direct register access references since you can inadvertently corrupt another variable using that register space.
  • ercoerco Posts: 20,257
    edited 2014-03-13 21:00
    Aiiiiiight! Thanks Chris, I learned something new here. So........

    U VAR BYTE
    W VAR WORD
    FOR W = 1000 TO 1050
    FOR U=65 TO 127
    NEXT
    NEXT
  • Buck RogersBuck Rogers Posts: 2,187
    edited 2014-03-13 21:02
    erco wrote: »
    Aiiiiiight! Thanks Chris, I learned something new here. So........

    U VAR BYTE
    W VAR WORD
    FOR W = 1000 TO 1050
    FOR U=65 TO 127
    NEXT
    NEXT

    Hello!
    OY!
    I didn't make the connection either. Thank you Chris. I'm blaming an individual to be named later and a complaining robot. Problem fixed but the reason stands.....
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-03-14 09:16
    No problem. I just know how these kinds of things can perplex one into frustration. And we don't want that. :innocent:
  • Buck RogersBuck Rogers Posts: 2,187
    edited 2014-03-14 20:50
    No problem. I just know how these kinds of things can perplex one into frustration. And we don't want that. :innocent:

    Good enough.

    Congratulations erco, you just won the prize, you're both here.
  • Buck RogersBuck Rogers Posts: 2,187
    edited 2014-03-15 17:02
    No problem. I just know how these kinds of things can perplex one into frustration. And we don't want that. :innocent:

    And having made the changes, I discovered that it was a case of making no difference whatsoever. So the response still stands about the all important blame game.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-03-17 16:38
    Buck,

    I didn't test your initial problem (I will see if I have time tomorrow), I just identified an issue in the code presented, which is that changes to either variable would have corrupted the other since as far as the BASIC Stamp Interpreter is concerned, U occupies the lower 8 bits of W0.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-03-18 16:18
    Buck,

    I re-read your first post but am not sure exactly what you're trying to do. Is there any way you can re-word the explanation so that someone who doesn't have a clue what you're trying to do might be able to offer a program example or suggestion? :nerd:
  • Buck RogersBuck Rogers Posts: 2,187
    edited 2014-03-18 16:47
    Buck,

    I re-read your first post but am not sure exactly what you're trying to do. Is there any way you can re-word the explanation so that someone who doesn't have a clue what you're trying to do might be able to offer a program example or suggestion? :nerd:

    Please take a look at it, I just added more information regarding the whole thing and the inevitable conclusion as to what else can happen.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-03-19 10:03
    Buck,

    I did look at it and perhaps it is just me, but I still don't see exactly what you're trying to accomplish. If you could describe what the two FOR...NEXT loops are supposed to do I could try to come up with a code example. Or perhaps someone else can translate for me? :innocent:
  • GenetixGenetix Posts: 1,754
    edited 2014-03-19 10:34
    Buck,

    I see that in Visual Basic the value of the counter variable can be modified while inside the FOR-NEXT.
    "If you change the value of counter while inside a loop, your code might be more difficult to read and debug."
    "Tip - A While...End While Statement (Visual Basic) or Do...Loop Statement (Visual Basic) works well when you don't know in advance how many times to run the statements in the loop. However, when you expect to run the loop a specific number of times, a For...Next loop is a better choice. You determine the number of iterations when you first enter the loop."

    Normally you don't change the value of a FOR-NEXT so why not just use a DO-LOOP?
    DO-LOOPs can be nested also.
Sign In or Register to comment.