← Teacher guide

The first true question wins

Students add else if to ask more questions in a row, and see that the first true question wins, so the order of the questions sets the ranges.

micro:bit Foundations · Decide · about 40 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 8 min · Investigate 10 min · Modify 8 min · Mix it up 3 min · Make 8 min · Spirit level 3 min

Before class

Click the + at the bottom of an if block to add else if (another question). The micro:bit asks the questions from the top; the first true one runs and the rest are skipped. else runs only when every question above it is false. That makes ranges simple: temperature < 15, then else if temperature < 25 means 15 to 24, because anything under 15 was already caught. It also makes order matter: ask > 0 before > 500 and the second can never run. Tilt in the simulator: move the mouse over the micro:bit.

Running the lesson

  1. Predict: -100 is the trap. It is negative, so many students pick the left arrow. Read the question aloud: is -100 less than -300?
  2. Run: small tilts show the square. That middle zone is the else.
  3. Investigate: the trace pins down the edges (300 is not more than 300). Leo's tilt is the order mistake: the bigger question must be asked first.
  4. Modify: students fix Leo's program in the editor, then add a fourth range.
  5. Mix it up: last lesson's bike light, read at two light levels.
  6. Make: two questions make three ranges. Ask students to explain why the middle needs no 'and'.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Seeing that only the first true question counts. Ask them to read the questions from the top with one value and stop at the first yes.

When students get stuck

What you will seeA question to askThe fix
One picture never appears.Is there a value that makes its question true, and no question above it true?Put the narrower question first (> 500 before > 0).
The comfort meter shows hot for 20 degrees.Read your questions from the top with 20. Which is true first?Check the < and >, and the order: < 15, then < 25, then else.
There is no else if.Where is the + on the if block?Click the + twice: the first adds else, the next adds else if.

Answers to the checks

  1. Predict: You tilt the micro:bit a little to the left, just enough that acceleration x is -100. What shows?
    The small square. -100 is not less than -300 and not more than 300, so neither question is true and the else part runs.
  2. Trace the tilt: For each value of acceleration x, what shows?
    left arrow, small square, right arrow, small square
    -500 is less than -300, so the first part runs. 0 and 300 fit neither question (300 is not more than 300), so else runs. 400 is more than 300.
  3. Leo's tilt: Leo wants a small tilt right to show a diamond and a big tilt right to show an arrow. The arrow never appears. Why?
    A big tilt is also more than 0, so the first question is true and the rest are skipped
    The questions are asked from the top, and the first true one wins. Any tilt over 500 is also over 0, so the diamond shows. Ask the bigger question first: > 500, then > 0.
  4. Mix it up: This is last lesson's bike light. What does it show at each light level?
    all the LEDs, small square
    30 is less than 50, so the first part runs: all the LEDs. 200 is not, so the else part runs: the small square.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Fix Leo's tilt

The program as blocks
Show as code
basic.forever(function () {
    if (input.acceleration(Dimension.X) > 500) {
        basic.showArrow(ArrowNames.East)
    } else if (input.acceleration(Dimension.X) > 0) {
        basic.showIcon(IconNames.SmallDiamond)
    } else {
        basic.showIcon(IconNames.SmallSquare)
    }
})

Modify › Left as well

The program as blocks
Show as code
basic.forever(function () {
    if (input.acceleration(Dimension.X) > 500) {
        basic.showArrow(ArrowNames.East)
    } else if (input.acceleration(Dimension.X) > 0) {
        basic.showIcon(IconNames.SmallDiamond)
    } else if (input.acceleration(Dimension.X) < -500) {
        basic.showArrow(ArrowNames.West)
    } else {
        basic.showIcon(IconNames.SmallSquare)
    }
})

Make

The program as blocks
Show as code
basic.forever(function () {
    if (input.temperature() < 15) {
        basic.showLeds(`
            # . # . #
            . # # # .
            # # # # #
            . # # # .
            # . # . #
            `)
    } else if (input.temperature() < 25) {
        basic.showIcon(IconNames.Happy)
    } else {
        basic.showLeds(`
            # . # . #
            . # # # .
            # # . # #
            . # # # .
            # . # . #
            `)
    }
})

Change a spirit level

Open task: any program that does what the step asks.

Stretch and support

Stretch: Make the comfort meter show five ranges (freezing, cold, right, warm, hot) with four questions.

Support: Give students the if … else if … else already in forever with the pictures in, and ask them only to fill in the two questions.

Exit ticket

if temperature > 0 … else if temperature > 30 … What shows at 35 degrees, and why?

The first part. 35 is more than 0, so the first question is true and the rest are skipped. Ask > 30 first.

What each step is for

Where it leads

Next unit: lists, which keep many values under one name. Class ideas: a tilt game where players hold the micro:bit level for ten seconds; a comfort meter by each classroom window with real micro:bits.