Loading...
Hello, my name is Miss Powell and welcome to computing.
I'm so pleased that you decided to join me here today.
In today's lesson, we're going to be learning how to use selection during programming.
Let's get started.
Welcome to today's lesson from the unit using selection in programming to develop a quiz.
This lesson is called "Conditions in Selection Statements." And by the end of today's lesson, you'll be able to explain how selection is used in computer programmes.
We'll be using these keywords throughout today's lesson.
Let's take a look at them.
The first word is condition.
Condition: This is a statement that can be either true or false.
The second word is selection.
Selection: This is part of a programme where, if a condition is met, then a set of commands is run.
Lesson outline: Conditions and selection statements.
This lesson is split into two sections.
In the first section, we'll identify how conditions are used in selection.
In the second section, we'll modify a condition in a programme.
Let's get started with the first section.
Identify how conditions are used in selection.
On the next slide, there will be some statements.
Whether some of the statements are true or false will depend on the individual person.
Get ready to record if you think the statements are true or false.
Use a whiteboard, a scrap of paper or another way to count how many you answer as true or false.
Decide if these statements are true or false for you.
Today is Wednesday.
My birthday is in March.
I like carrots.
My first name includes the letter S.
I walked to school this morning.
It is raining.
Computing is my favourite lesson.
Count up how many statements you recorded as true.
Andeep says, "I marked only one statement as true." Sam says, "I marked four statements as true." The children used the statements as conditions.
A condition is a statement that can be either true or false.
Programmers can use conditions to trigger actions.
You can think of conditions as being like questions that you can answer with either yes or no.
Look at this table we can see that true or false statements can be made into yes or no questions.
A yes or no question can be answered with either yes or no.
It's not open-ended.
If I said to you, what would you like to eat? That would be an open-ended question.
It could have lots and lots of answers, such as pizza, pasta, sandwich.
But if I asked, "Are you hungry?" then I'd be expecting you to answer yes or no.
A condition is a statement that can be either true, false or maybe.
Pause the video and have a quick think.
Is that true or false? The answer is false.
And why is that? A condition is a statement that can be either true or false.
In our example question, "Are you hungry?" I'd expect you to answer yes or no.
If it was a yes, I might go and make you some food.
And if it was a no, then I probably wouldn't do anything.
However, in that situation, if you answered maybe, I'd be quite confused.
It's the same for our conditions.
They are statements that can be either true or false.
Identify how conditions are used in selection.
When writing algorithms and programmes, you may want to set up actions to be carried out if the condition is met.
This is called selection.
The structure if, then is used when giving these commands.
This is an example of selection programming.
If left arrow key is pressed, then point in direction of minus 90, move 10 steps.
What condition is used? Pause the video and have a look at a series of commands.
See if you can work out what condition is used.
Left arrow key is pressed yes or no.
What happens when the condition is true? If the left arrow key is pressed, then point in direction of minus 90 and move 10 steps.
What happens when the condition is false? Pause the video and have a quick think.
Continue to the next instruction.
Selection is used when programming.
If left arrow key is pressed, then point in direction of minus 90, move 10 steps.
If then is used to show selection is happening.
This has been written with indentation to make it clear that the commands are linked.
In this statement, if touching the colour red, then say, "You completed the maze for two seconds." Where is the condition? Pause the video and have a quick look, see if you can find the condition.
Well done for having a try.
The condition is touching the colour red, yes or no.
What action will happen if the condition is true? Pause the video and have a quick think.
Fantastic! Say "You completed the maze for two seconds." And what would happen if the conditions were false? Well, the programme would move on to the next instruction.
In Scratch programming, you can set conditions by using the condition block.
Can you see it here in blue? It appears like a question.
The condition block needs to be used with other blocks to control the flow of actions.
Condition blocks are all the same shape, a hexagon.
They're designed to fit into blocks that contain a hexagonal slot.
This code sequence includes an example of using conditions in a programme.
If you look here at the code in Scratch, I've got the forever loop, so this will run continuously until the programme is stopped.
I have my conditional here in blue.
Space key pressed.
Condition: If spacebar is pressed.
Then my action will be to be carried out when the condition is true.
In this case, it's change colour effect by 10.
Repetition: Using a loop to check the condition.
Using the infinite loop means that the condition is continuously checked.
Means in our programme, it'll be ready to go when the space key is pressed.
Changing colour is controlled by a number.
This code will change the sprite's colour.
If the spacebar is pressed then change colour effect by 10.
Open the Funky Fish programme on oak.
link/funky-fish.
Pause the video so you can open the programme.
Identify how conditions are used in selection three of the code sequences have conditions, the fourth will reset the project.
Annotations are notes that can help you understand the programme.
You might notice a little sticky note here in the programming area.
This is an annotation and it gives you notes so that you can understand the programme.
I have a task here for you doing so well.
I'd like you to identify how conditions are used in selection.
Find out how a condition is used in each code sequence.
For each code sequence, record the condition that is used, what happens when the condition is true.
Pause the video to finish the task.
Well done, I'd like to give you some feedback about that.
But the infinite loop, it says forever.
This means that whatever blocks are inside of the infinite loop will be continuously checked.
If spacebar key is pressed, then change colour effect by 10.
The condition is if spacebar pressed.
Is that a yes or a no? When condition is true, the action is change colour effect by 10.
Let's take a look at this one.
We've got the when green flag clicked.
This is the event block to trigger the action to start.
We have the forever loop and within that it says "if up arrow key press" then "move 10 steps," "if on edge, bounce" and this is being run continuously.
The condition is if up arrow key pressed.
When the condition is true, the action is move 10 steps and if on edge, bounce.
Let's take a look at this one.
We've got when green flag clicked.
That's the event block.
Then we've got the forever loop.
If touching colour red, then move 25 steps, change colour effect by 25.
The condition is if touching colour red, yes or no.
When the condition is true, the action is move 25 steps and change colour effect by 25.
Fantastic, you've done so well in the first section, identify how conditions are used in selection.
Let's move on to the second section, modify a condition in a programme.
Andeep says, "All you need to get a condition to work is an if then block and a conditional block." What is Andeep's code sequence missing? Can you see his code sequence here in Scratch Junior? You've got when green flag clicked.
If colour blue is touching colour orange, then go to position, x: -208, y: 155.
Something's missing in Andeep's code sequence.
Pause the video and see if you can work it out.
Fantastic, well done for having a go.
Jacob says, "You need a forever loop." This code will only check the condition once.
It is missing an infinite forever loop.
Without the forever loop, when the green flag is clicked, the programme will check the condition once and very quickly.
So if the blue is not touching the orange, nothing will happen.
Then the programme will be satisfied that it's run the condition.
It won't be looking for it again later on in the programme.
Using the forever loop will fix that.
It will mean that the programme will be continuously checking for this condition.
Andeep says, "Infinite loop, that looks better." Now, the condition will be checked continuously.
How can you tell that this code sequence includes selection? Pause the video and have a quick look at the code sequence.
See if you can tell that it includes selection.
We've got when green flag clicked, forever loop.
If colour blue is touching orange, then go to position, x: -208, y: 155.
Point in direction 180.
Change colour effect by 5.
Sam says you can tell it includes selection because an if/then block has been used.
You can see the selection here in Scratch Junior.
You can think of selection as the computer programme choosing to select an action or not.
What is the condition used? Pause the video and see if you can work out the condition.
The condition is yes or no, colour blue is touching colour orange.
Is any part of the sprite that is blue touching anything in the project that is orange? What actions will be carried out when the condition is true? Pause the video and have a think.
Sprite will go to coordinates x -208, y 155.
Sprite will point in direction 180, down.
Sprite will change colour effect by 5.
What is the same in these code sequences? Pause the video and have a look at the two sequences.
See if you can work it out.
On the left we have when green flag clicked.
Then we have the forever loop.
We have if space key press, then change colour effect by 25.
On the right, we have when green flag clicked, forever loop, if R key is pressed, then change colour effect by 25.
Have a little look and see if you can spot what's the same and what's different.
Aisha says, "Both start when the green flag is clicked." That's the same, they both have the same event block.
Jacob says they both have an infinite loop, forever loop.
So this means that it's running continuously.
Sam says, both use conditions and selection.
Our condition is, if space key pressed, yes or no, or if R key is pressed, yes or no.
Our selection is, change colour effect by 25.
Both will change a sprite's colour when the condition is true.
So in these examples of code, we can change the colour of the sprites in two different ways.
What is the difference between these coding sequences? Pause the video and have a look.
The only difference is that they use different conditions.
On the left if spacebar is pressed and on the right if R key is pressed.
It's two ways of achieving the same colour change.
Modifying the condition means changing the keyboard key that is used to trigger the action.
So in this example, we could use "space key pressed" or we could change that into "R key pressed" or any other letter on our keyboard.
To modify the condition, press on the arrow.
This will open the menu to change the keyboard input.
Think about what action you want to trigger.
Often the key matches the action.
An example of this would be the up arrow key moving a sprite up.
Choose a new key.
The condition has been changed if R key pressed.
When you change the condition in a programme your programme might behave differently.
Andeep says, "Now when I press the spacebar, nothing happens." That might be good for Andeep's programme, perhaps he wants to use the spacebar for something else.
Open the programme Bug Maze on oak.
link/bug-maze.
Pause the video to open the programme.
I have a task here for you.
You're doing so well.
I'd like you to modify a condition in the programme.
Number one, fix each of the code sequences to use a new condition.
Conditions that you code should make sense for the action, e.
g.
right arrow should move sprite right.
Number three, check that your programme works.
Pause the video to finish the task.
I'd like to give you some feedback.
Open the programme Bug Maze example on oak.
link/bug-maze-example.
This will give you some examples of the conditions that you could use.
Well done, I have a task here for you.
I'd like you to modify a condition in the programme.
Number four, write instructions down for how to play the game using your improved conditions.
Use these sentence starters to help you.
Press the blank to move the sprite up.
Press the blank to move the sprite down.
Press the blank to move the sprite left.
Press the blank to move the sprite right.
When you touch the wall, blank.
When you touch the finish sign, blank.
When you want to start again, press blank.
Pause the video to finish the task.
I'd like to give you some feedback.
Write instructions for how to play.
Press the up arrow key to move the sprite up.
Press the down arrow key to move the sprite down.
Press the left arrow key to move the sprite left.
Press the right arrow key to move the sprite right.
When you touch the wall, you will go back to the start.
When you touch the finish sign, it will say you have won the game.
When you want to start again, press the spacebar.
Well done, you've done so well in today's lesson.
Let's summarise.
Conditions in selection statements.
A condition is a statement that can be either true or false.
Selection is when a programme includes conditions to decide which actions should be carried out.
In Scratch programming, selection happens using an if/then block, which checks a condition block.
If the condition is true, the action inside the block is triggered.