Use one if … else block to choose between two things, so exactly one of them happens.
A bike light is on in the dark and off in daylight. Never both, never neither: one question decides which.
What you should already know:
Count with a variable· Use a variable to remember a number, and change it each time a button is pressed.
If· Make the micro:bit decide what to do, using a sensor and an if block.
Last lesson the night light needed two if blocks: one to turn the lights on, one to turn them off. An if … else block does both: if the question is true, run the first part; else (otherwise), run the second. Click the + at the bottom of an if block to add else.
How this lesson works: Predict, Run, Investigate, Modify, Make
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Students use if … else to choose between two things, and see that exactly one part runs each time: the else runs only when the question is false. About 40 minutes.
Click the + at the bottom of an if block to add else (otherwise). If the question is true, the first part runs; if false, the else part runs. Exactly one of the two runs each time, so if … else has no gap (like two ifs that miss a value) and no overlap. else if (more questions in a row) is the next lesson.
Running the lesson
Predict: some students think both parts run, one after the other. Ask: what does 'otherwise' mean? Ask the 'both' group to read the block aloud with 'otherwise' in place of else.
Run: dark then bright. The lights go off now, because the else part clears them.
Investigate: the trace pins down the edge: 50 is not less than 50. Kim's flicker is the belief that else always runs too.
Modify: rebuilding the night light shows what else replaces: the opposite question.
Mix it up: an if with no else, from last lesson, inside a counter.
Make: one if … else, one question.
The hard part (on purpose)
Believing both parts run. Ask them to say the block aloud with 'otherwise' in place of else.
When students get stuck
There is no else in the if block. Ask: Where is the + on the if block? Fix: Click the + at the bottom of the if. It adds an else part.
The lights stay on in the day. Ask: Which part clears the screen, and when does it run? Fix: Put clear screen (or the small square) in the else part.
Two ifs and an else, and the picture flickers. Ask: How many questions do you need for two choices? Fix: One question and an else. Delete the second if.
Stretch and support
Stretch: Make the bike light flash in the dark (show, pause, clear, pause) and glow steadily in the day.
Support: Give students the if … else already in forever with the question filled in, and ask them only to fill each part.
Where it leads
Next lesson: else if, for three or more choices, where the order of the questions matters.
What each step is for
Investigate: else runs exactly when the question is false, so one if … else covers both cases with no gap and no overlap.
Modify: else replaces the opposite question: one if … else does the job of two ifs.
Mix it up: An if with no else does nothing when its question is false.