8 Loops

while loops

The while statement is also a conditional statement, it is used to run a block of code multiple times in a loop.

The statement starts with while followed by a test condition and the line ends with a : character.

The while statement is similar to the if statement, they both use a  test condition, and if the test condition is true it will run the block of code that follows. The difference is that with a while loop after the block of code finishes the test condition will be tested again and if it is still true then the block of code will be rerun again, and this continues as long as the test condition is true. Whereas with the if statement the block of code is run only once.

If the test condition turns out not to be true then the block of code that follows is skipped and the program will continue after the block.

For example:

pindir(P1_6, OUT)
a = 0
while a < 10:
   out(P1_6, 1)
   wait(500)
   out(P1_6, 0)
   wait(500)
   a = a + 1

This example flashes the LED 10 times.

The line  a= 0 will initially set the variable a to 0 and the line while a < 10: will be true and the block of lines that follow will be run which causes the LED to flash once.  At the end of the block the line a = a + 1 will make a equal to 1  (a is increased by 1) .  The loop  will repeat and the test condition is tested again and this time the value of a is 1 and the test condition will still be true. The block of code will be run again and the variable a will increase to 2, in fact it will run the block of code and each time the value of a will increase by 1. However after 10 loops the value of a will equal 10 and on the next loop when the test condition is tested again the line while a < 10: will not be true and the loop will be skipped and the program will continue execution after the end of the loop. This means that the loop will run for a total of 10 times and the LED will flash 10 times. Using the while loop we can make the loop run any number of times we want by using the correct test condition.

Try programming it now. Did you count 10 flashes?  If you want to run it again you can press the right RESET switch on bottom of the Launchpad board on the right. This will reset the Launchpad board and it will restart your program and you can count the flashes again.

In the blinky.mpy example we used the statement while 1:  The 1 in the while 1: line tells the program to loop through the block code that follows forever. The test condition is just a simple 1 which is the same True (in fact you can write while True: if it make it easier to understand). This is an endless loop.

pindir(P1_6, OUT)
while 1:             # run forever
   out(P1_6,1)
   wait(500)
   out(P1_6,0)
   wait(500)

However if we were to use while 0:   like

pindir(P1_6, OUT)
while 0:             # never runs
   out(P1_6, 1)
   wait(500)
   out(P1_6, 0)
   wait(500)

This has the unfortunate effect of causing the block to never run. This is called dead code and should be removed.

for loops

Besides the while loop command there is the for loop which does a similar job to the while loop. The for loop uses the range function to create a series of values. The for loop looks like this:

pindir(P1_6, OUT)
for a in range( 10 ) :
   out(P1_6,1)
   wait(500)
   out(P1_6,0)
   wait(500)

This example will flash the GREENLED 10 times. The a in the for line is called the loop variable. The range(10) part means to increment loop variable a upto 10. This means it will start with a equal to 0 and increases a by 1 each time round the loop, until it gets to 9 and then it will exit the loop. a will never be equal to 10, it will exit before it gets to 10.

range

A second form of the range statement allows you more control over how the loop variable is incremented.

for a in range( start, stop, inc):

The loop variable a starts with the value start and increments by inc each loop until gets to the value stop. The increment value inc can be negative or positive, allowing the loop variable to count up or down.

for a in range( 10, 3, -2 ):
    print( 'loop variable a is' , a )

Writes this output to the mpyUart window:

loop variable a is 10
loop variable a is 8
loop variable a is 6
loop variable a is 4

Note: The range command can only be used in the for statement.

break and continue

Finally there are two special commands which can be used with the while and for statements to escape from a loop
break    will break out of the loop completely
continue  will continue to the next iteration of the loop