Conditionals and Loops
In this tutorial, we will review conditionals and loops in MicroPython, as control structures that allow us to make decisions and repeat actions.
Both conditionals and loops are the main tools for controlling the flow of execution, and constitute the majority of the logic that we will put in our program.
In MicroPython, if, else, for, and while basically function identically to their equivalents in Python (and, by the way, are quite similar to the rest of the languages).
Conditionals
The conditional structures if and else allow us to execute a block of code only if a specific condition is met.
In MicroPython, conditionals are implemented using the keywords if, elif (optional), and else.
Basic Syntax of if
The basic structure of an if conditional is as follows:
if condition:
# Block of code to execute if the condition is true
Condition: A boolean expression that evaluates to True or False.
Block of code: Instructions executed if the condition is true.
For example:
temperature = 25
if temperature > 30:
print("It's hot")
In this example, the message “It’s hot” will only be printed if the temperature is greater than 30.
Using else
The keyword else is used to execute a block of code when the if condition is not met.
if condition:
# Block of code if the condition is true
else:
# Block of code if the condition is false
Example:
temperature = 25
if temperature > 30:
print("It's hot")
else:
print("The temperature is pleasant")
If the temperature is not greater than 30, “The temperature is pleasant” will be printed.
Using elif
The keyword elif (“else if”) is used to evaluate multiple conditions in sequence.
if condition1:
# Block of code if condition1 is true
elif condition2:
# Block of code if condition2 is true
else:
# Block of code if no condition is true
Example:
temperature = 25
if temperature > 30:
print("It's hot")
elif temperature > 20:
print("The temperature is pleasant")
else:
print("It's cold")
The program evaluates multiple conditions and executes the block corresponding to the first true condition.
Loops
The for and while loops allow us to repeat a piece of code multiple times. In MicroPython, the most common loops are for and while.
for Loop
The for loop is used to iterate over a sequence of elements, such as a list or a range of numbers.
for variable in sequence:
# Block of code to execute in each iteration
Variable: Takes the value of each element in the sequence.
Sequence: A collection of elements to iterate over.
Example:
for i in range(5):
print("Iteration:", i)
This loop prints:
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
while Loop
The while loop repeats a block of code while a condition is true.
while condition:
# Block of code to execute while the condition is true
Condition: A boolean expression evaluated before each iteration.
counter = 0
while counter < 5:
print("Counter:", counter)
counter += 1
This code prints:
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
The loop runs as long as counter is less than 5.
Loop Control: break and continue
MicroPython provides break and continue to control loop flow.
break: Terminates the loop immediately.
continue: Skips to the next iteration.
for i in range(10):
if i == 5:
break
print("Iteration:", i)
This prints:
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Example using continue:
for i in range(5):
if i == 2:
continue
print("Iteration:", i)
This prints:
Iteration: 0
Iteration: 1
Iteration: 3
Iteration: 4
