video

Lesson video

In progress...

Loading...

Hi, I'm Ben and welcome to this programming unit, which is all about sensing.

In this lesson.

We'll be exploring the construct of selection, which is first introduced in the year five Oak National Academy lessons.

If you need extra support on selection, you may wish to revisit those units first before coming into this lesson.

You'll need somewhere quiet with no distractions, a pen and a piece of paper.

So when you're ready, let's begin.

Let's look at the objectives for this lesson.

In this lesson, you will explain that selection can control the flow of a programme.

You will identify condition, examples of conditions in the real world.

You will use a variable in an if.

then.

else.

statement to control the flow of a programme.

And finally, you will determine the flow of a programme using selection.

Let's have a look at an example of selection in the real world.

So this is a decision tree.

You may have seen it before referred to as a branching database.

What it does is allow us to classify objects, according to whether certain conditions are met.

So conditions we've got on this decision tree or this branching database are, does it fly? Does it have wings? And does it have an engine? And according to whether those conditions are met or not, we can classify these four objects.

So let's have a look at a couple of examples and you can have a go at this yourself.

Looking at this decision tree, what can fly and has wings? So 'yes' to both, does it fly and has a wings? So it must be the aeroplane.

What can't fly, but has an engine? So this time, does it fly? No.

Does it have an engine? Yes.

So it must be the motorbike.

We are always making decisions in our everyday lives.

Here is an example.

So if it's sunny, then I'll go and play basketball.

Else, if it's raining or if it's not sunny, I'll go bowling.

Let's have a look at how that decision could be related in programming.

So in programming we use, if.

then.

else.

for decisions like this.

So the previous slide had two outcomes.

One to be carried out if the condition was true.

So the condition being that it's sunny, the other to be carried out if the condition is false or not sunny.

Now let's overlay the true or false conditions onto the diagram we saw in the previous slide.

So there's our condition.

If it's sunny.

If the condition is true, then I'll play basketball.

If the condition is false, I'll go bowling.

This is known as an if.

then.

else.

statement.

So this is what it looks like.

If we present it in a text format, if it is sunny, then I'll play basketball else I'll go bowling.

Can you think of your own examples of real world if.

then.

else.

Statements.

Pause the video and complete the handout.

Next, we're going to have a look at an example of an if.

then.

else.

statement, running in a programme.

After this slide, you'll see an emulation of a programme on a microbit, which uses an if.

then.

else.

statement.

That statement is represented below as text.

So the statement is, if button A is pressed, show a tick.

Else, show a cross.

To create your project, including an if.

then.

else.

statement, you will need to go to makecode.

microbit.

org and create a new project.

The aim of this activity is for you to create a programme, which includes the if.

then.

else.

statement, which is detailed on the worksheet.

So go to makecode.

microbit.

org now and complete that project.

Once you've finished resume the video.

Here's what your code should look like.

So I've used a forever block to check that the condition is continuously checked.

If you've placed it inside an OnStar block, it will only check right on the beginning of the programme.

So you may need to edit your programme if that's what you've done.

So inside a forever block, I've used an if.

then.

else.

statement.

The condition is, if button A is pressed, show icon tick.

Else, show icon cross.

So with this programme, a cross would be displayed unless button eight is pressed, in which case it would show a tick.

If you need to revisit your programme, pause the video and go back to your programme now.

We can also choose the flow of a programme with a variable.

Instead of using buttons to control the flow of a programme, we can use a variable.

Let's make a simple fortune teller programme to illustrate this.

So in the editor, which you've just been in for your previous project, create a new project.

You can do that straight from the project you've been working in by clicking on the home button and then new project.

Name your project 'fortune teller'.

This project will require you to pick a random number.

In MakeCode we can do this using the pick random block.

This is an example below which you can see.

pick a random number one to six.

This would be really good for simulating the role of a dice.

So just like a dice, it will generate a random number between one and six.

I'm just going to click through the slides to show you the dice changing.

So we start off with two, now we have five and then we have three.

And just like a dice our random number block will pick a random number.

So here is the design for you to make your fortune teller programme.

The algorithm is when shaken, choose a random number, one or two and save it in answer.

Check if the answer is one, say 'Yes' Check if the answer is two, say 'No' To create this, you're going to need a variable and the variable will be called 'answer'.

And in your programme, you will set the 'answer' to a random number one or two.

In MakeCode, making a variable is very similar to the way you do in scratch.

You'll need to click on the variables category of blocks and name your variable 'answer'.

You also have the programme flow for this programme.

So on shake, choose a random number and then depending on which random number is chosen, it will either go to the left or the right on this programme flow diagram.

So number one will prompt, 'Yes', number two will prompt 'No' You have this designed on your worksheet for you to follow.

Go to makecode.

microbit.

org and create a new project.

Use the design provided to create your programme.

This is what your code should look like.

So I've used the 'answer' variable and set the 'answer' to pick a random number between one and two.

Then if the answer equals one, a string of 'Yes' will be shown, else, a string of 'No' will be shown.

Have a look at the code you have created and compare it to this.

If you need to make changes, go back to your project now and do that now.

You can now predict your future with the microbit.

To do this flash your programme to the microbit and ask a question about your future which can be answered 'yes' or 'no'.

For example, will I be rich? Shake your microbit and it will now predict your future, displaying 'yes' or 'no' on the LEDs.

If you don't have a microbit, you can do this with the emulator.

The next challenge is to add another 'else' to the programme.

Can you adapt the programme so that it chooses from three possible outcomes - yes, no or maybe? What would you need to add or change? There's a clue on the if.

then.

else.

block.

Go back to your project and try it for yourself.

This is the solution for three outcomes.

So in this programme on shake, the answer variable is set to a random number between one and three.

If the random number one is chosen, answer equals one.

Then the string 'yes' is shown.

If the answer is two, then the string 'no' is shown.

Otherwise else, then 'maybe' is shown.

So the condition that would pick that would result in maybe being shown is for the answer to be three.

Is neither one nor two.

So it must be three.

We can also explain the flow of a programme using a flow diagram.

The programme on the left is an example of a fortune teller programme.

Picking three random outcomes, represented by random number one, two or three, and using answer as the variable.

The flow for that programme is shown in the diagram on the right.

On the next slide, we're going to trace through what happens if the answer is two.

So we're assuming that random number picked will be two.

So we'll follow through and see where that leads us in the programme.

So if the answer is one, that's false, if the answer is two, that's true.

So will say no.

So let's go through that a little more slowly.

If the answer is one, well, it's clearly not one because it's two, so it's not true.

It's false.

Is the answer two? Yes.

Because the answer two was generated, that is true.

So it's going to say no.

Your final task in this lesson is to trace the flow of a programme for yourself.

So on the handout, trace the flow of a programme, for when one and three are generated as the random numbers.

Show the flow by shading the root the programme would follow.

We'd love you to share your work with Oak National.

If you'd like to, please ask your parents or carer to share your work on Instagram, Facebook, or Twitter, tagging @OakNational and #LearnedwithOak.