video

Lesson video

In progress...

Loading...

Hello, my name is Mrs. Holburn and welcome to Computing.

I'm so pleased you've decided to join me for today's lesson.

Today we are going to be looking at how selection can be used in programming to control the flow of a programme.

Welcome to today's lesson from the unit Programming Selection.

This lesson is called Using Selection When Developing Programmes, and by the end of today's lesson, you'll be able to describe how selection statements are used in programmes to control programme flow.

Shall we make a start? We will be exploring these keywords throughout today's lesson.

Selection, selection used when there is more than one possible path for a programme to follow.

Condition, condition, an expression that evaluates to true or false.

Multi-path selection, multi-path selection, selection when there are multiple paths for a programme to follow.

Today's lesson is split into two sections.

We'll start by explaining how selection controls programme flow and then we'll move on to use selection in a programme.

Let's start by explaining how selection controls programme flow.

Selection is used when there is more than one possible path for a programme to follow.

It allows a programme to run sequences of code depending on whether a condition evaluates to true or false.

A programme checks the condition and selects the path of action.

We make reasoned decisions daily to depending on whether something is true or false.

Let's have a look at some examples.

Izzy says, "If I have enough money, I will buy the video game that I want." So here is this represented in a flow chart.

We have a decision which says have enough money? If that's true, then Izzy can buy the game.

The decision symbol is used in a flow chart to control the flow of execution.

Inside the diamond is a condition.

If the condition is true, then during programme execution it will follow one path.

If it is false, it will follow the other path.

So you can see the diagram is labelled clearly with the true and false arrows.

Let's make a prediction about what will happen in this system.

If you input the number 10, what will be the output? Maybe pause the video whilst you have a look carefully at the flow chart.

How did you get on? Let's walk through this together.

So 10 is more than five, so the decision will be true.

So the output will be 15 because 10 plus five is equal to 15.

Did you have that as your answer? Well done.

Time to check your understanding.

Which flowchart symbol is used to control the flow of execution? Is it A, B, or C? Pause the video whilst you have a think.

Did you select C? Well done.

The diamond shape is used for a decision in a flow chart.

In programming, selection is carried out using if statements.

So you can see here in the top box of code we've got the format.

So we've got if condition with a colon and then we have an indented block of code underneath and then we have an else again with a colon, and then we have another indented block of code.

And then below that we have some actual code.

So here we have if number greater than five with the colon and then the indented block says number is equal to number plus five, and then we have our else with our colon and then another indented block which says number is equal to number minus five.

So in this programme, if number is greater than five, we are going to add five to it, else we are going to take five away.

Jun says you'll need an if and an else when you want to run code on either of the two possible paths.

Time to check your understanding.

What do you use to carry out selection in a programme? Is it A, if statements, B while statements or C print statements? Pause the video whilst you have a think.

That's right.

The correct answer is A, if statements.

If you want to carry out selection in a programme, you need to use if statements.

So far all the examples we've seen in this lesson have just used one decision.

So we have a condition and we have two possible options of true or false.

What happens if there's more than one decision or multiple paths to follow in a programme? Maybe pause the video and have a quick think.

Multi-path selection allows your programme to handle situations where there are three or more different options to choose from.

So you can see here I haven't just gone if else block this time.

This time I have an if, an elif and an else block.

Note the format is exactly the same, so we have the condition with the colon and then we have the indented block of code underneath each selection statement.

Multi-path selection checks conditions one by one and picks the first one that is true.

Then it follows the instructions for that path or the block of code contained within that path.

You can use multiple elif blocks.

So this example only uses one, but you can have as many as your programme needs.

The else and its block are optional, so you don't necessarily need to have an else block.

Okay, we have come to our first task of today's lesson and you're doing a great job so far, so well done.

For part one, explain how selection controls programme flow.

Use your own words and pause the video whilst you have a go.

How did you get on? Did you manage to come up with an answer? Well done.

Let's have a look at this sample answer together.

Selection is a fundamental programming concept that allows a programme to make decisions based on conditions.

It controls programme flow by determining which set of instructions should be executed based on true or false conditions.

Selection uses conditional statements such as if, if else and else statements to evaluate conditions and decide which code block to run.

For part two, complete the table to state what will be the output based on the input.

Pause the video whilst you have a go.

How did you get on? Let's have a look at the answers together.

So if the input was six, the output is going to be incorrect.

That's because the selection statement on line three says if number is double equals to seven, remember the double equals is a comparison operator.

So it's checking if number which was inputted by the user is equal to seven.

If it does, it's going to print, "You got it!" If it doesn't, it's going to print "Incorrect." So six is incorrect, 10 is also going to be incorrect, but seven is going to output you got it.

For part three, complete the table to state what will be the output based on the input.

Note, the programme has changed slightly.

So look carefully at the code.

Pause the video whilst you have a go.

How did you get on? Let's have a look at the answers together.

So same bit of code, but this time we've added an elif.

So for seven it is going to output you got it, because it's looking for a comparison to seven the double equals.

For nine though this time it's going to print lower and then for three is going to be printing higher.

And that's because we have that statement which says elif number less than seven, print higher.

So three is less than seven, so it's going to print higher.

Okay, we are moving on to the second part of today's lesson and you're doing a great job so far, so well done.

We are now going to use selection in a programme.

Selection statements control the flow of execution because a block of code will only run if the condition is true.

So here line three will only run if score is greater than 30.

Here's a walkthrough of a selection statement.

On line one score is assigned to 20.

Score is not greater than 30, so the condition is false and line three is not going to run.

The flow of control moves to line four and outputs the end.

Let's go back through, but change the value of score this time.

This time score is assigned the value of 35.

Score is now over 30, so the condition is true.

The flow of control moves to line three and outputs You Won! The flow of execution continues to line four and outputs the end.

Time to check your understanding.

Which lines of code would execute in this programme? Is it A one, two, and four? B one, two and three? Or C, one, two, three and four? Pause the video whilst you have a think.

That's right.

The correct answer is 1, 2, 3, and 4.

Score is greater than 30.

So it'll execute the code on line three inside the selection statement, but remember it will also continue and execute line four.

Which lines of code would execute in this programme? Is it A 1, 2 and 4? B 1, 2, and 3, or C, 1 and 2? Pause the video whilst you have a think.

That's right.

Lines 1, 2 and 4 will be executed.

That's because score is not greater than 30.

So line three will not execute in this programme.

Okay, we are moving on to the second task of today's lesson and this time we're gonna go and do some coding.

So I'd like you to open the starter programme at oak.

link/chatterbot.

Run the programme and investigate how it works.

For part three, I'd like you to add another question, which asks the user what their favourite colour is.

It then displays "Blue is my favourite too," if the user enters blue or displays, "That is not my favourite colour," if the user enters anything other than blue.

Pause the video here whilst you go and open the file and have a go.

How did you get on? Let's have a look at some sample code together.

So I've just given you line 17 to 22 because these are the new bits of code.

So on line 17 we've got print, ("What is your favourite colour?") surrounded by speech marks and brackets On line 18, we are storing that input as colour.

Notice I've used input.

lower here.

Now I've used that to force the user input into lowercase just so that I can only check one version.

On line 19, I've got if colour is equal to "blue" again in speech marks because we are checking for a string.

And then I have my, don't forget colons with selection statements.

And then on line 20 indented I have print ("Blue is my favourite colour too").

I then have my else statement.

So if the user enters anything other than blue, this is what's going to be printed again, indented another print statement which says, that is not my favourite colour.

If you didn't quite get this working correctly, you can always pause the video here and use this code to help you.

For part four, I'd like you to add another question to your chatter bot, which asks the user what the weather is like where they are today.

The programme should display, "Wear a coat" if the user responds rainy.

The programme should display "Wear sun cream" if the user responds sunny.

The programme should display, "I have no advice" for all other responses.

Pause the video whilst you have a go at the activity.

How did you get on? You're doing a great job so far, so well done.

Let's have a look at this solution together.

So again, I've only shown you the new lines of code here.

So you can see my code runs from lines 24 to 31.

On line 24, I have my print statement which asks what's the weather like.

On line 25, I'm storing the user's input as the variable weather.

And then on line 26, I start my selection statement.

So if weather is double equal to "rainy" in speech marks and then my colon, I'm going to print "Wear a Coat." So that's the indented block inside that if.

Now this time, because I've got multiple conditions, I'm using an elif, so on 28 elif weather is equal to sunny this time.

I'm going to print "Wear sun cream." And then lastly on line 30, I have my else with a colon.

So any other response that the user has entered, I'm going to print "I have no advice." If you want to, you can go to Oak.

link/chatterbot-solution and see the complete coded solution.

Okay, for part five, it's a bit over to you.

If you have some time, add some more questions to your chatter bot, making sure that you use selection statements to provide sensible responses and try and use some multiple path selection statements.

So you have more than one option for the user to enter.

Pause the video whilst you go and have a go.

Okay, we have come to the end of today's lesson and you've done a fantastic job.

I'm sure you've made some fantastic chatter bot programmes.

Let's summarise what we have learned together.

Selection is used when there is more than one possible path for a programme to follow.

Selection is carried out using if, elif, and else statements.

A selection statement will check if a condition returns as true or false.

I hope you've enjoyed today's lesson and I hope you'll join me again soon.

Bye.