Home Micropython Python Basics : While loop

Python Basics : While loop

by shedboy71

The condition is evaluated, and if the condition is true, the code within all of their following in the block is executed. This repeats until the condition becomes false.

Because the while loop checks the condition before the block is executed, the control structure is often also known as a pre-test loop.

Let us look at a basic Python example

i = 1
while i < 6:
  print(i)
  i += 1

This first checks whether i is less than 6, which it is, so then the {loop body} is entered, where the print function is run and i is incremented by 1.

After completing all the statements in the loop body, the condition, (i < 6), is checked again, and the loop is executed again, this process repeating until the variable i has the value 5.

If you run this example you will see something likeĀ  this

MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822
Type "help()" for more information.
>>>
>>> 1
2
3
4
5

Breaking out of a loop

Using the break statement we can stop the loop even if the while condition is true, here is an example showing this

When the variable i reaches the value of 2 the break statement will be run

i = 1
while i < 6:
  print(i)
  if i == 2:
    break
  i += 1

If you run this example you will see the following

MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822
Type "help()" for more information.
>>>
>>> 1
2

Using else with the while loop

Python allows us to use the else statement with the while loop as well. The else block is executed when the condition given in the while statement becomes false.

If the while loop is broken using break statement, then the else block will not be executed, and the statement present after else block will be executed. The else statement is optional.

Here is an example

i=1   
while(i<=5):    
    print(i)    
    i=i+1    
else:  
    print("The while loop is complete")

When you run this will you see the following

MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822
Type "help()" for more information.
>>>
>>> 1
2
3
4
5
The while loop exhausted

 

Infinite loop example

That it is possible for the condition to always evaluate to true, this creates what is known as an infinite loop.

When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop.

Let’s look at a practical example of how an infinite loop is useful

In this example, we have a PCF8574 and we use a while loop which will always run so we can continuously get readings from the 8-bit input/output (I/O) expander

import pcf8574
from machine import I2C, Pin
import time

i2c = I2C(scl=Pin(22), sda=Pin(21))
pcf = pcf8574.PCF8574(i2c, 0x20)

while True:
    pcf.port = 0xff
    time.sleep(0.5)
    print(pcf.port)
    pcf.port = 0x00
    time.sleep(0.5)
    print(pcf.port)
    pcf.port = 0xAA
    time.sleep(0.5)
    print(pcf.port)
    pcf.port = 0x55
    time.sleep(0.5)
    print(pcf.port)

If you want to see the circuit, parts, and more this example is located at ESP32 and PCF8574

 

You may also like