← micro:bit Foundations

If and else

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.

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.

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.

Predict

The program as blocks
Show as code
lesson.ts
basic.forever(function () {
    if (input.lightLevel() < 50) {
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    } else {
        basic.clearScreen()
    }
})

This is last lesson's night light, built with one if … else.

The light level is 20 (dark). Which parts of the if … else run, each time round forever?

RunRun it and compare the result with your prediction.

Run

Drag the light slider down to dark, then back up to bright. Do the lights go off now?

The program as blocks
Show as code
lesson.ts
basic.forever(function () {
    if (input.lightLevel() < 50) {
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    } else {
        basic.clearScreen()
    }
})
Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

The program as blocks
Show as code
lesson.ts
basic.forever(function () {
    if (input.lightLevel() < 50) {
        basic.showLeds(`
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
    } else {
        basic.clearScreen()
    }
})

Work out what happens at each light level. Watch the edge at 50.

For each light level, what does the night light do?

light leveldoes
20
200
50
49

Kim thinks that in the dark her night light turns the lights on and then the else part clears them straight away, so it flickers. Is she right?

When does the else part run?

ModifyChange the code on purpose and check the result.

Modify

This is the two-if night light. Rebuild it with one if … else.

Need a hint?

Click the + on the first if to add else.

Another hint

Move clear screen into the else part, then delete the second if.

Make the else part show a small dot or square, so the light glows a little in the day.

Need a hint?

Swap clear screen in the else part for show icon, and choose the small square.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.

Rebuild the two-if night light with one if … else.

This ticks by itself when your blocks do what is asked.

In the else part, show a small dot or square instead of clearing the screen.

This ticks by itself when your blocks do what is asked.

InvestigateTrace how each line of code connects to what you see.

Mix it up

A counter with a prize, from earlier lessons.

The program as blocks
Show as code
lesson.ts
let count = 0
input.onButtonPressed(Button.A, function () {
    count += 1
    basic.showNumber(count)
    if (count == 5) {
        basic.showIcon(IconNames.Diamond)
    }
})
Each press of A adds 1 to count, shows it, and shows a diamond (the prize) if count is 5. What shows straight after the 6th press?

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Make a bike light with one if … else:

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
Need a hint?

One if … else inside forever. The question is about the light level.

Another hint

If it is dark (light level < 50), light all the LEDs. Else, show a small square.

A bike light: all the LEDs on when it is dark, else a small square.

This ticks by itself when your blocks do what is asked.

ModifyChange the code on purpose and check the result.

Change a desk lamp

This desk lamp lights up when the room is dim. Make it switch at 120 instead, and show a moon you draw when it is on.

Try it yourselfYour edits are saved in this browser.
It takes a few seconds to load. Your work in this step is kept.
For teachers

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.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

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

  1. 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.
  2. Run: dark then bright. The lights go off now, because the else part clears them.
  3. Investigate: the trace pins down the edge: 50 is not less than 50. Kim's flicker is the belief that else always runs too.
  4. Modify: rebuilding the night light shows what else replaces: the opposite question.
  5. Mix it up: an if with no else, from last lesson, inside a counter.
  6. 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

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

Now you can
Next lesson8. The first true question winsAdd else if to ask more questions in a row, and see that the first true one wins, so their order sets the ranges.