# Electronics Projects: How to Use DO Loops in PBASIC Code

The DO loop in PBASIC can make programming of the BASIC Stamp in your electronics project a bit more compact. The DO loop is a special PBASIC statement that performs essentially the same function as a label and a GOTO statement. For example, consider the following:

```Main:
HIGH 0
PAUSE 500
LOW 0
PAUSE 500
GOTO Main```

The same function can be accomplished without the Main label or the GOTO statement by placing the lines that turn the LED on and off between DO and LOOP statements, like this:

```DO
HIGH 0
PAUSE 500
LOW 0
PAUSE 500
LOOP```

The lines between the DO and LOOP statements will be executed over and over again indefinitely.

```' LED Flasher Program
' Doug Lowe
' July 10, 2011
'
' This program flashes LEDs connected to pins 0, 2, 4, 6, 8, and 10
' in sequence.
'
' This version of the program uses a DO loop.
' {\$PBASIC 2.5}
' {\$STAMP BS2}
Speed VAR BYTE
Led VAR BYTE
Speed = 50
Led = 0
DO
HIGH Led
PAUSE Speed
LOW Led
PAUSE Speed
IF Led < 10 THEN
Led = Led + 2
ELSE
Led = 0
ENDIF
LOOP```

You can add a conditional test to the LOOP statement to make the loop conditional. For example:

```Led = 0
DO
HIGH Led
PAUSE 500
LOW Led
PAUSE 500
Led = Led + 2
LOOP UNTIL Led > 10```

This code will flash the LEDs on pins 0, 2, 4, 6, 8, and 10. After the LED on pin 10 is flashed, the next-to-last line sets the Led variable to 12. Then, the LOOP UNTIL statement sees that Led is greater than 10, so it stops looping.

Instead of the word UNTIL, you can use the word WHILE to mark the condition in a DO loop. There’s a substantial difference between UNTIL and WHILE, and the difference is just as the words suggest. When you use the word UNTIL, the loop will execute until the condition tests true. When you use the word WHILE, the loop will execute until the condition tests false.

Note that you can also include the condition test on the DO statement or on the LOOP statement. If you place the condition test on the DO statement, the condition is tested before each execution of the loop. If you place it on the LOOP statement, the condition is tested after the completion of each loop.

It’s common to place WHILE tests on the DO statement and UNTIL tests on the LOOP statement.

```Led = 0
DO WHILE Led < 11
HIGH Led
PAUSE 500
LOW Led
PAUSE 500
Led = Led + 2
LOOP```

Here, the value of Led is tested prior to each execution of the loop. The loop is executed as long as Led is less than 11.

DO loops can be nested, which means that one DO loop can contain another DO loop. When DO loops are nested, the inner loop must have a conditional test. Otherwise, it will loop forever, and the outer loop will never have a chance to complete.

In this program, the innermost DO loop flashes the six LEDs once. It uses an UNTIL condition to stop the loop after the last LED has flashed. The outermost DO loop continues endlessly, causing the flashing sequence to continue indefinitely.

```' LED Flasher Program
' Doug Lowe
' July 10, 2011
'
' This program flashes LEDs connected to pins 0, 2, 4, 6, 8, and 10
' in sequence.
'
' This version of the program uses nested DO loops.
' {\$PBASIC 2.5}
' {\$STAMP BS2}
Speed VAR BYTE
Led VAR BYTE
Speed = 50
DO
Led = 0
DO
HIGH Led
PAUSE Speed
LOW Led
PAUSE Speed
Led = Led + 2
LOOP UNTIL Led > 10
LOOP```