Shop OBEX P1 Docs P2 Docs Learn Events
Nested Do While loop — Parallax Forums

Nested Do While loop

RoHerman00RoHerman00 Posts: 22
edited 2011-01-27 12:23 in BASIC Stamp
If you have a Do While loop like the one I have written out will it execute the most nested Do While first (in other words the While loop which turns on led1) then it will pause for half a second then turn on led2 pause for another half a second then finally turn on led3 pause for half a second and then turn off all the leds at the end?

i = 1
Do
high led3
pause 500
i = i + 1
Do
high led2
pause 500
i = i + 1
Do
high led1
pause 500
i = i + 1
while i < 2
while i < 3
while i < 4

low led1
low led2
low led3

Thanks,
Robert

Comments

  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-24 08:26
    Oh and whoever is reading the post the indents did not show up??? so for some reason all of the do whiles are right on top of each other.
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-01-24 09:25
    Yessir, that's normal around here :)

    If you enclose your code in CODE directives when you paste it, the tab spaces will be kept.

    Like this:

    Press the "#" button on your message editor (you may have to "go advanced").... then paste your code between the two CODEs:

    [C0DE]

    paste your code here

    [/C0DE]
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-24 09:36
    No

    I strongly suggest that you re-read the section of the Stamp Manual on the DO / WHILE statement(s) and that you try pretending that you're a Stamp executing your program. Use a piece of paper. Start with the first line of your program and copy it to the paper, then "execute it" by writing any changes in any variables on the line that you copied. Then decide what line of your program will be executed next and write that down on the next line, repeat.

    Remember that
    DO
       ' stuff
    LOOP WHILE expression
    
    is equivalent to
    startOfLoop:
       'stuff
    IF expression THEN GOTO startOfLoop
    
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-01-24 09:41
    I think this may be a little easier to read for you:
    i = 1
    DO WHILE i < 4
        HIGH led3
        PAUSE 500
        i = i + 1
        DO WHILE i < 3
             HIGH led2
             PAUSE 500
             i = i + 1
             DO WHILE i < 2
                HIGH led1
                PAUSE 500
                i = i + 1
             LOOP
         LOOP
    LOOP
    
    low led1
    low led2
    low led3
    
    

    I'm not sure what you intended, but it WILL execute even though it is a little Rube Goldberg for turning on three LEDs. In other words, it could be simplified considerably.

    Remember your "led#" needs to be identified for the BS to understand what it means. I figure you already know that however.
  • RoHerman00RoHerman00 Posts: 22
    edited 2011-01-27 12:23
    Thanks a lot guys,

    In reply to spiral_72 it isn't going to be turning on leds I just wanted to know if it would execute that way. The program I wrote is much more than that but it works basically the same way. And thanks for showing me how to post code.
Sign In or Register to comment.