Today you are coding in Python on the micro:bit. You will build control structures step by step at your device: conditionals that choose a path, and loops that repeat actions.
Before you run anything, think: if a condition in an if statement is false, what do you expect the program to do?
You will predict, build, run and fix as you go, then solve two short challenges that mix a loop with decisions.
Keep the start short. Devices on the Python micro:bit editor, students logged in. Optional predict: ask what happens when an if condition is false, and park answers to revisit in Make sense. Do not let discussion eat build time. Pair students if that is your usual setup; put pairing in your oral instructions, not on their screen.
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:
Frame the lesson as a PRIMM code-along on decisions and repetition. Glance at the overview together, then move straight into the first build step. Key question: what is the difference between code that always runs and code that only runs when a condition is true?
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.
Read the idea of a condition as true or false briefly. Link to everyday choices only in a sentence, then get to the code steps. Watch for students who think both branches run; stress that the condition chooses a path.
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.Model the simple if on the board: import, set temperature, if with a comparison, indented display line. Common bug: no indent under if, or using = instead of a comparison. Differentiation: support by dictating the condition aloud; extension by asking what happens if they change the threshold.
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.Show adding else as plan B so something always runs. Ask: which block runs when temperature is 15? Bug to watch: else not aligned with if, or a second if instead of else. Keep the run-investigate beat quick before the next structure.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.