In this lesson on control structures, you'll explore the building blocks that allow your programs to make decisions and repeat actions, essential for creating interactive and dynamic code on the Micro:bit. Control structures like conditionals and loops will enable you to respond to inputs, process data, and build more complex programs.
Here's what you'll be learning and doing:
Conditional statements are a fundamental part of programming that allow your code to make decisions. They let your program execute different blocks of code depending on whether a certain condition is true or false. This is like choosing different paths based on the situation – for example, if it's raining, you might decide to take an umbrella; otherwise, you leave it behind.
The basic building block is the if
statement. It checks a condition, and if that condition evaluates to true, the code inside the if block runs. If you want to handle the case when the condition is false, you can add an else
clause. For checking multiple conditions, you can use elif
(which stands for 'else if') to test additional scenarios.
Conditions are typically built using comparison operators. Here are some common ones:
==
(equal to)!=
(not equal to)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)You can also combine conditions using logical operators like and
(both must be true), or
(at least one must be true), and not
(reverses the condition).
For instance, imagine you're programming a Micro:bit to monitor temperature. You might write code to check if the temperature is above 30 degrees to display a 'Hot' warning, or between 20 and 30 for 'Warm', and below 20 for 'Cool'. This kind of decision-making makes your programs interactive and responsive.
This simple if
statement is like a gatekeeper – it only lets the code through if the condition is met. They are the foundation of making decisions in your code. In this step, we'll create a program that checks if a temperature variable is greater than 20 and, if it is, scrolls the message 'Warm' on the display. If the condition isn't met, nothing happens – that's how a basic if
works.
We'll start by importing the Micro:bit library, setting a variable, and then using the if
statement to check the condition. Remember, indentation is key in Python – the code inside the if must be indented (usually by 4 spaces or by pressing the tab key).
Add the following complete code to your editor:
from microbit import *
temperature = 25
if temperature > 20:
display.scroll("Warm")
Here's a quick breakdown:
from microbit import *
: This imports everything from the Micro:bit module so we can use functions like display.scroll.temperature = 25
: We create a variable called temperature and set it to 25.if temperature > 20:
: This checks if temperature is greater than 20. If true, the indented code runs.display.scroll("Warm")
: If the condition is true, this scrolls 'Warm' on the LED display.==
instead of >
in the condition? Test it out and observe.Building on the simple if statement, we can add an else
clause to handle what happens when the condition is false. This is like having a plan B – if the first condition isn't met, the code in the else block runs instead. It ensures that your program always does something, no matter the outcome of the condition.
In this step, we'll update our temperature checker to display 'Warm' if the temperature is greater than 20, and 'Cool' otherwise. This makes the program more complete, as it provides feedback in both cases.
Update your code to this complete version:
from microbit import *
temperature = 25
if temperature > 20:
display.scroll("Warm")
else:
display.scroll("Cool")
Here's a quick breakdown:
temperature = 25
: Sets the temperature variable.if temperature > 20:
: Checks if temperature is greater than 20.display.scroll("Warm")
: Runs if the condition is true.else:
: This block runs if the if condition is false.display.scroll("Cool")
: Scrolls 'Cool' when the condition isn't met.>=
instead of >
? Observe how it affects when 'Warm' or 'Cool' displays. This will help you see how if-else covers all possibilities.To handle more than two possibilities, we can chain multiple conditions using elif
(short for "else if"). This allows us to check additional conditions if the previous ones are false. You can have as many elif clauses as needed, and optionally end with an else
to cover all other cases. This structure is perfect for scenarios with several mutually exclusive options, ensuring only one block of code runs.
In this step, we'll enhance our temperature checker to have three categories: 'Hot' if above 30, 'Warm' if above 20 but not above 30, and 'Cool' otherwise. This builds a more nuanced decision-making process in our program.
Update your code to this complete version:
from microbit import *
temperature = 25
if temperature > 30:
display.scroll("Hot")
elif temperature > 20:
display.scroll("Warm")
else:
display.scroll("Cool")
Here's a quick breakdown:
temperature = 25
: Sets the temperature variable.if temperature > 30:
: Checks if temperature is greater than 30.display.scroll("Hot")
: Runs if the first condition is true.elif temperature > 20:
: If the if is false, this checks if greater than 20.display.scroll("Warm")
: Runs if the elif is true.else:
: This block runs if all previous conditions are false.display.scroll("Cool")
: Scrolls 'Cool' for all other cases.elif
before the else, like elif temperature <= 0: display.scroll("Freezing")
. Remember the order matters – conditions are checked sequentially, and only the first true one executes. Test with values like -5, 0, 15, 25, 32, or 20. What happens if you rearrange the conditions? Observe how it affects the output.