Loading...
Hi, everyone.
My name is Mrs. Jenkins.
Let's get started.
Welcome to today's lesson, which is called "Conditional Statements." It is taken from the unit, "Sensing Movement With Physical Computing." By the end of today's lesson, you will be able to use a conditional statement to compare a variable to a value.
In today's lesson, we have two key words.
These words will appear throughout the lesson, so it's really important that we understand what they mean so that we can make the most of it.
Those two words are 'condition' and 'operator.
' Condition is a statement that can either be true or false.
And operator is a symbol used to compare values.
So keep an eye out for those words throughout today's lesson.
They are 'condition' and 'operator.
' In today's lesson, conditional statements, we have two learning cycles.
Use comparison operators in conditions, explore the order of if/then/else conditions.
We're going to start with use comparison operators in conditions.
Sam asks, "What is a condition?" A condition is a rule that decides what happens next.
So if the score equals 10, show a badge.
If the score is greater than 10, show a trophy.
She asks us now, "What is a comparison operator?" That is a great question, Sam, and we use them a lot in our computing.
A comparison operator compares two values.
For example, we've got the symbol meaning less than, symbol meaning greater than, and the equals meaning equal to.
You may have come across these in maths.
We use them to make decisions in a programme.
Let's have a little check-in now.
Which of these operators means less than? Have a go.
How did you get on? I asked you which of these operators means less than.
It is the first one, A, 'cause then below it, B, we've got more than, and C, we've got equal to.
Great job, everyone.
Sam's asking now, "What is a compass?" Do you know? A compass is a tool that shows you which direction you are facing: north, south, east, or west.
When might you have used a compass? Sam says, "I have used one when I went hiking." And Izzy said, "I used one when we went camping.
It helped us to find our way." "How do compasses work?" That's a really good question, Sam.
A compass works by using the earth's magnetic field.
Inside the compass, there is a small magnet that always points towards the earth's magnetic north.
If you point a compass north, you can see all the other directions.
Which direction is the arrow pointing in? If the arrow is not pointing straight to the north, east, south, or west, choose the direction it is nearest to.
Have a go.
What did you think? What direction is the arrow pointing in? It's pointing north.
Great job.
How about now? What direction is the arrow pointing in? How did you get on? This time, the arrow is pointing east.
Great job.
How about this one? Remember, if the arrow is not pointing straight to the north, east, south, or west, choose the direction it is nearest to.
Have a go.
How did you get on? It was south, well done.
Sam asks us now, "What is a heading?" A compass heading is a number from naught to 360 that tells us which direction something is pointing.
So for north, we've got zero degrees or 360 degrees.
East, we have 90 degrees.
South, we have 180 degrees.
And west, we have 270 degrees.
We can set a condition and we say, if heading is greater than 45, show north.
If heading is greater than 135 and less than 225, show south.
And Sam explains, "This condition checks if I am facing south." Okay, let's have a little check-in now.
Which compass shows north? So we've got three pictures - A, B, and C.
Have a go.
How did you get on? I asked you which compass shows north.
It is C.
We can see that purple arrow is pointing to the N, which is north.
Great job, everyone.
The micro:bit has a compass sensor.
You can use the compass heading degrees in MakeCode to find the current direction the micro:bit is facing.
So you can see there's an arrow pointing to where the compass is on the micro:bit.
The compass can be spit into four main ranges to show directions.
So rather than it just being straight up and down, we can give it a slightly wider area.
So north would be 315 degrees to 360 degrees and naught degrees to 45 degrees.
East, we would say from 45 degrees to 135 degrees.
South, 135 degrees to 225 degrees.
And west, 225 degrees to 315 degrees.
Comparison operators can be used in code.
They're used to check if the compass heading is in a certain range.
So for example here, we've got forever.
If the compass heading is less than 45, then show the string north.
So when we say show the string, it will show the words "north" appear on the micro:bit.
Okay, so for task A, I would like you to fill in the blanks.
So if the heading is less than blank or greater than blank, the direction is north.
If the heading is more than 45 but less than blank, the direction is east.
If the heading is more than 135 but less than blank, the direction is south.
If the heading is between blank and blank, the direction is west.
Have a go.
How did you get on? I asked you to fill in the blanks.
Let's have a look at the answers.
Okay.
If the heading is less than 45 or greater than 315, the direction is north.
If the heading is more than 45 but less than 135, the direction is east.
If the heading is more than 135 but less than 225, the direction is south.
If the heading is between 225 and 315, the direction is west.
Now, we do have a second part to this task, and this time, I would like you to fill in the blanks.
For one, two, and three.
So if score 100, show "You win." If speed blank 50, show "Too fast." If temperature blank zero, display "Too cold." Have a go.
How did you get on? I asked you to fill in the blanks with our comparison operators.
So if score 100, show "You win." If speed blank 50, show "Too fast." And if temperature blank zero, display "Too cold." Let's have a look at the answers.
Okay, so if the score equals 100, show "You win." If the speed is greater than 50, show "Too fast." If the temperature is less than zero, display "Too cold." Great job, everyone.
We're going to move on now to learning cycle two.
Explore the order of if/then/else conditions.
Okay, what if two conditions are true? E.
g.
If score is greater than five, show star.
Else, if score is greater than 10, show trophy.
The score is 15.
Which one will show? Have a little think.
The programme checks the first condition so the score is more than five.
This is true.
So a star will show.
The next condition is also true, but it won't be checked, okay? So it always checks the first condition first.
And if that is true, it won't go down to the next condition.
Let's have a little check-in here.
True or false: In an if/else if statement, more than one outcome can happen at the same time if conditions are true.
What do you think? Have a go.
How did you get on? I asked you true or false.
In an if/else if statement, more than one outcome can happen at the same time if conditions are true.
It is false.
And the reason why, only the first condition that is true will run.
The rest are skipped.
Great job, everyone.
Izzy asks, "Does the order matter?" And Jen explains, "In an if/else if statement, only the first condition runs.
If both are true, the second one is skipped.
Thank you, Jen.
A programme sometimes needs to check more than one condition.
An if/else statement is a way to check several rules one after another.
The computer checks the first condition.
If it's true, it runs the code and stops.
If it's false, it moves to the next condition.
The computer continues until one condition is true or it reaches the final else block.
Okay, we have two programme flows here.
I want you to have a look, and which programme flow will show me the correct answer if the heading equals 30? So programme flow A says compass heading, less than 45.
If true, show north.
False, move on to less than 135.
If it's true, show east.
If it's false, move on to less than 225.
If it's true, show south.
If it's false, west.
Programme flow B shows compass heading.
If it's less than 225, and if true, show at south, if it's false, you move on to less than 135.
If it's true, show east.
If it's false, we move on to less than 45.
If it's true, show north.
If false, show west.
So which programme flow will show me the correct answer if our heading is 30 degrees? What do you think? Let's have a little look.
Programme flow A works because the first condition checks if the heading is less than 45.
It is true, so the micro:bit shows north.
The other conditions are ignored because the first one has already run.
Programme flow B does not work.
The first condition says, if heading is less than 225, shows south.
30 is less than 225.
So the micro:bit shows south even though it should show north.
The programme never checks if it is north, 'cause the south condition was first and already matched.
Okay, let's have a little check now.
What will be shown if the heading is 45 degrees? Okay, so we've got a programme flow, and it shows compass heading less than 90 and true, show east, if false and less than 180 true, show south, or otherwise, show west.
So what will be shown if the heading is 45? What do you think? Have a go.
How did you get on? I asked what will be shown if the heading is 45.
The answer is east.
It will show east because it goes compass heading, if it is less than 90, and we know 45 is less than 90, it'll be true.
So show east.
Great job.
Watch the video to see how to add if/else if blocks to the code.
Okay, so we have added our if/else block.
We have then added our comparison operator and we've put our numbers, we want to say less than 45.
And then we have put our compass heading.
We've then put our heading, set heading.
Here, we've put a set of quotation marks that allows you to add text into that set heading rather than it being a number.
Okay, programme flow A.
The same programme flow can be seen as MakeCode.
So we saw it being made on the video, and this programme code can be seen in MakeCode.
So we've got a forever block all the way round, and then we've got an if/then/else block, but we have expanded that so it's got if/then, else if, else if, else.
Okay, and then we've got if the compass heading is less than 45, set the heading to north and show the string heading.
So it will say what the heading is.
Else, if compass heading is less than 135, then set heading to east and show the string heading.
Else if, compass heading is less than 225, then set heading to south and show the string heading.
If it's not any of those, so it's else, set heading to west and show the string heading.
In MakeCode, a programme checks one condition at a time, starting from the top.
It begins with if, then checks else if conditions if the first condition was false and finishes with else, which only runs if none of the other conditions are true.
Okay, we're going to move on to task B now.
I would like you to copy and then expand the code using else if blocks to show east and west.
Make sure the conditions are in the correct order.
Have a go.
How did you get on? I asked you to copy and then expand the code using else if blocks to show east and west.
Make sure the conditions are in the correct order.
So it should look a little bit like this.
Forever, if compass heading is less than 45 or the compass heading is greater than 315, set heading to north.
Else, if the compass heading is less than 135, set heading to east.
Else if compass heading is less than 225, set heading to south, else, set heading to west.
Great job, everyone.
Great job, everyone, you've worked so hard today.
Let's summarise what we have learned.
Conditions in a programme can be checked with less than, greater than, and equal to operators.
These are called comparison operators.
The order of conditions in selection statements can affect the outcome.
In an if/then/else statement, only the first true condition runs.
If both are true, the second one is skipped.
Programmes can be modified to produce different outcomes, and a different order of conditions can produce a different outcome.
Great job, everyone.
You've worked really hard today.
I hope to learn with you again soon.