Loading...
Hi everyone.
Welcome to today's lesson.
My name is Mrs. Jenkins.
I can't wait to learn with you today.
Welcome to today's lesson taken from the unit "Sensing Movement in Physical Computing." Today's lesson is called "Selection to Control the Flow in a Programme." By the end of today's lesson, you'll be able to explain that selection can control the flow of a programme.
Before we get started, we have three keywords.
These keywords appear throughout the lesson, so it's really important we understand what they mean before we get started.
The three words are, flow, condition and selection.
Flow is the order in which commands run in a programme.
Condition is a statement that can either be true or false.
And selection is part of a programme where if a condition is checked, then a set of commands is run.
Okay, so let's keep an eye out for those words as we go through the lesson.
A quick reminder, they are flow, condition and selection.
Okay, let's get started.
In today's lesson, "Selection to Control the Flow in a Programme," we have two learning cycles.
The first is, identify examples of a real world conditions.
The second is, use if/then/else to control programme flow.
Let's start with identify examples of real world conditions.
When we make decisions, we follow a flow.
This can help to find out information, categorise objects or determine the flow of a programme.
This is an example of a decision making flow.
It is a branching database.
So we could ask, does it fly? And if they said yes, we could say, does it have wings or does it have an engine? So which object can fly without wings? So we choose, yes, does it fly? Does it have wings? No.
And we end up with a hot air balloon.
See the hot air balloon can fly without wings.
Which object cannot fly but has got an engine.
So we need to choose, no, it cannot fly.
Yes, it has an engine, and we have the motorbike.
Okay, a little check in here.
What kind of database is this? A, rooting.
B, leafing.
C, oaking.
Or D, branching.
Have a go.
Okay, I asked you what kind of database is this? A, rooting.
B, leafing.
C, oaking.
Or D, branching.
It is a branching database.
Great job.
We are always making decisions.
So here, we have our person saying, if it's sunny then I'll play basketball, else, I will go indoor bowling.
This decision has two outcomes.
One, to be carried out if this condition is true, so if it is sunny.
The other is to be carried out if the condition is false, not sunny.
So, if it's sunny, then I'll play basketball, else, I will go bowling.
So, if it's sunny is the condition.
If the condition is true, then I'll play basketball.
If the condition is false, I will go bowling.
"Can you think of any other examples of if/then/else statements?" Izzy asks.
And Jun says, "If it's Saturday, then I can sleep in, else I have to get up early." Great job, Jun.
True or false? In a condition, a statement has three possible outcomes.
Have a go.
How did you get on? I asked you, true or false, in a condition a statement has three possible outcomes.
The answer is false.
In a condition, a statement only has two outcomes, true or false.
When writing algorithms and programmes, you may want to write a set of actions to be carried out if the condition is met.
This is called selection.
The structure if then, or if then else, is used when giving these commands.
Okay, we're going to move on to Task A now, and I would like you to create your own example of a real world decision involving a condition.
Here is an example.
If it is sunny, then I'll play basketball, else, I'll go bowling you.
You should use the words if, then and else in your condition.
Create your own example of decision involving a condition.
So you've got if, then, else.
Have a go.
How did you get on? I asked you to create your own example of a real world decision involving a condition.
You should use the words if, then and else in your condition.
Here is my example.
If I'm hungry, then I'll get a snack, else I'll wait until dinner.
Okay, we are gonna move on to learning cycle two now, which is, use if/then/else to control programme flow.
Here is Jun's example of selection.
If it is after seven o'clock, then get outta bed and get ready for school, else, stay in bed for longer.
If is used to give the condition, which is, after seven o'clock.
So if it is after seven o'clock, that is our condition.
Then is used to give the actions to carry out when the condition is true.
So for us it says, then get out of bed and get ready for school.
So that is the action to be carried out if the condition is met.
Else is used to give the actions to carry out when the condition is false.
So else stay in bed for longer, that is the action to be carried out if the condition is not met.
This has been written with indentation to make it clear that the commands are linked.
If it starts at the top by the end of the line.
If it's after seven o'clock, then get out of bed and get ready for school, else, stay in bed for longer.
Okay, we've got a little check now.
I want you to look at this statement.
In this statement, if I am hungry, then I will get a snack, else I will wait for dinner, we're gonna answer the questions, where is the condition? What action will happen if the condition is true? And what action will happen if the condition is false? Have a go.
How did you get on? I asked you to look at this statement, if I am hungry, then I will get a snack, else I will wait for dinner.
And I wanted you to answer, where is the condition? What action will happen if the condition is true? And what action will happen if the condition is false? So if I am hungry, I am hungry is the condition.
Then I will get a snack.
I will get a snack is what the action that will happen if the condition is true.
Else I will wait for dinner, that is the action that will happen if the condition is false.
So if I'm hungry, I will get a snack, but otherwise I will wait for dinner.
When writing algorithms and programmes, you may want a set of actions to be carried out if the condition is met.
This is called selection.
The structure, if, then, else is used when giving these commands.
So here we have, if button A is pressed, then show a tick, else show a cross.
This means that it will show an X, unless A is pressed down, when a tick will be shown.
If A is let go of, the X will be displayed again, and that's because it is in a forever loop as well.
So if the button is pressed, a tick will appear, otherwise a cross will appear.
Instead of using buttons to control the flow of a programme, you can use a variable.
A variable is a value that can be set and changed throughout the running of a programme.
Instead of using buttons to control the flow of a programme, you can use a variable.
In micro:bit MakeCode there is an option to create a variable.
In this lesson we'll be using a variable of number picked.
In the MakeCode platform, you can generate random numbers using this block.
Pick random one to six.
Just like a dice, this will generate a random number between one and six.
You can set the range of numbers that is picked from.
So here we have our algorithm, and then we have our programme flow.
The algorithm says, when shaken, choose a random number one or two, and store it in number picked.
Number two, check if the number picked is one, then say yes.
And number three, check if the number picked is two, then say no.
Our programme flow says shake, choose random number between one and two, and then if we follow down to one, it will say yes, follow down to two, it will say no.
The programme flow can be used to create an algorithm on the micro:bit MakeCode platform.
True or false? The flow is the order in which the programme is run.
Have a go.
How did you get on? I asked you, true or false, the flow is the order in which the programme is run.
The answer is true, the flow is the order in which the programme is run.
Okay, so we've got our programme flow again where it says shake and then if it is shaking, it will choose a random number between one and two.
If it shows one, it will say yes, and if it shows two, it will say no.
We now have that as a MakeCode code.
So we've got in pink it says, on shake, and then we've got set number picked, that's our variable.
Two, pick random one to two.
And then it says, if the number picked equals one, then show string yes, else show string no.
Okay, so it's the same as our programme flow, but it is written in code.
The code could be expanded to add an else/if to give more than one condition that could be met.
So if number picked is one, then show yes.
Else, if number picked is two, then show no.
Else show maybe.
So that can be seen in our code and we've changed that pick random number now to say, pick random one to three.
On shake, set number picked to pick random one to three.
If number picked equals one, then show string yes.
Else if number picked equals two, then show string no.
Else show string maybe.
Okay, and we can see this is a programme flow as well.
Pick random number between one and three, if the number picked is one and it's true, say yes.
If the number picked as two and that's true, say no.
Or otherwise say maybe.
Okay, so for Task B, you are going to create your own code using the flow diagram.
So you can go to oak.
link/makecode to create a programme using the flow diagram.
This time the flow diagram says, pick random number between one and six.
If the number picked equals two, say yes.
If it doesn't and the number picked equals four, and that is true, say no.
And it doesn't say two or four, say maybe.
Have a go.
How did you get on? I asked you to create a programme using the flow diagram.
The flow diagram said, pick random number between one and six.
If the number picked equals two, say yes.
If the number picked equals four, say no.
Or otherwise say maybe.
So here we have, on shake, set number picked, to pick random one to six.
If the number picked equals two, then show yes.
If the number picked equals four, then show no.
Else show maybe.
Great job everyone.
You have worked so hard in today's lesson.
Well done.
Let's summarise our learning.
When we make decisions, we follow a flow.
This can help to find out information, categorise objects or determine the flow of a programme.
Conditions are events that can only be evaluated as true or false, or yes or no.
Selection in the form of if/then/else statements can be used to check conditions.
Selections uses conditions to determine the programme flow.
Great job everyone.
You have worked so hard today.
I can't wait to learn with you again soon.