Loading...
Hello.
My name is Mrs. Holborow, and welcome to computing.
I'm so pleased you've decided to join me for the lesson today.
In today's lesson, we're going to be looking at relational operators and how we can use relational operators in selection statements.
Welcome to today's lesson from the unit, programming selection.
This lesson is called Relational Operators, and by the end of today's lesson, you'll be able to use relational operators as part of selection statements.
Shall we make a start? We will be exploring these keywords throughout today's lesson, relational operator, relational operator, compare two values and produce the result of true or false.
Condition, condition, an expression that evaluates to true or false.
Selection, selection, used when there is more than one possible path for a programme to follow.
Look out for these key words throughout today's lesson.
Today's lesson is split into two sections.
We'll start by using relational operators to compare values.
We'll then move on to use relational operators in selection statements.
Let's make a start by using relational operators to compare values.
Relational operators can be used to compare values within an expression.
Andeep deep says, "Anyone older than 12 requires an adult ticket at the theme park." So here we have the variable, adult_ticket is equal to age greater than 12.
The greater than is a relational operator here.
Relational operators can be used to compare two values within an expression.
Here the value, age, or the value held in the variable, age, is being compared to the value, 12.
This relational operator is comparing age to the number 12.
Relational operators evaluate to either true or false.
This expression returns false because age is not greater than 12.
In this example, age is equal to nine.
This expression returns true because age is greater than 12.
In this example, age is equal to 15.
You can use these relational operators in conditions.
The double equals is a comparison operator, and it checks if one value is equal to another.
The exclamation mark followed by the equals is not equal to.
Less than, less than or equal to, greater than or greater than or equal to.
Let's have a look at some examples of these relational operators.
Total equal equal 1.
Does total equal 1? Total exclamation mark equals 1.
Does total not equal 1? Total less than 3.
Is total less than 3? Total less than equal to 3.
Is total at most 3? Total greater than 10.
Is total greater than 10? Total greater than equal 10.
Is total at least 10? Time to check your understanding.
True or false, this relational operator checks that age is equal to 12.
Have a look at the code carefully.
That's right.
It's false.
The single equals is used for assignment.
To compare age to 12, you would need to use the double equals operator, the comparison operator.
Andeep has a really good question, "But what if the theme park had the rule everyone aged 12 or over is classed as an adult ticket?" Ah, Jun has a great response.
"You would use the greater than or equal to operator." So greater than equal to.
Great solution, Jun.
Fill in the blanks to complete this sentence.
A relational operator evaluates to either, or.
Pause the video whilst you fill in the blanks.
That's right.
A relational operator evaluates to either true or false.
Well done.
Okay, we're now moving on to our first set of tasks for today's lesson.
For part one, state whether the following conditions would either evaluate to true or false.
Pause the video whilst you complete the activity.
How did you get on? Let's have a look at the answers together.
So you asked if the conditions would evaluate to true or false.
5 greater than 10 is false 'cause 5 is less than 10.
10 double equals 10 is true because 10 is equal to 10.
10 not equal 10 is false.
10 greater than or equal to 10 is true.
12 less than or equal to 13 is true.
For part two, write the expressions for the following statements.
Age is less than 18, age is greater than or equal to 16, total is not equal to 100, and total is equal to 10.
Pause the video whilst you write the expressions.
How did you get on? Let's have a look at the answers together.
So for the first one, you should have hopefully written age and then the less-than symbol and then 18.
The second one should have been age greater-than symbol equals 16.
The third one should be total exclamation equals 100, so not equal to 100.
And then the last one should be total double equals 10.
Okay, we're moving on to the second part of today's lesson, and you're doing a great job so far, so well done.
We're now going to use relational operators in selection statements.
Relational operators can be combined with selection to control programme flow.
So here we have a snippet of code which says, if number greater than 5, then number is equal to number plus 5, else number is equal to number minus 5.
So we're using a relational operator in the if selection statement.
This programme checks if number is greater than 5.
If true, it adds 5 to the number, if false, it subtracts 5 from number.
This programme checks a user's score and then uses multi-branch selection to print an appropriate message.
So you can see here we've got an if, an elif and an else.
You can add as many branches as you need for your programme.
Time to check your understanding.
What would be displayed if score was equal to 10? Would it be A, you lose, B, not bad, or C, you win? Pause the video whilst you have a think and look at the code.
That's right.
The correct answer is B, not bad.
That's because that's the first selection statement which would return true in the code.
If score is less than 0 is not true, elif score is less than or equal to 10 is true.
What would be displayed if score is equal to 11? Is it A, you lose, B, not bad, or C, you win? Pause the video whilst you have a think.
That's right.
C, you win, is the correct answer.
That's because this is the first selection statement in the programme which returns true.
Okay, we're moving on to our last set of tasks for today's lesson.
The following programme has been created to check a password.
All of the code is correct, but it is in an incorrect order.
Amend the code so that the programme works correctly.
And as a note, you'll need to add indentation to some lines of the code.
Pause the video here whilst you have a go at the activity.
How did you get on? Did you manage to put the code in the correct order? Well done.
Let's have a look at the solution together.
So on line one, we should have stored_password is equal to "Fish4321" in speech marks.
On line two, we should have the print statement which says, enter password.
And on line three, we should store the value the user enters as input under the variable called password.
On line four, we have our selection statement which says, if password is equal to stored_password:.
On line five, we have our print statement which says, access granted.
On line six, we have else.
And on line seven, we have print access denied.
Remember, you needed to add indentation to the two print statements inside the selection statements.
For part two, open the starter programme, oak.
link/sandwich-order.
The prices for the sandwiches are as follows.
So sandwiches are 2 pounds 80, wraps are 3 pounds.
If you want to have crisps with either, you need to add 30p.
Add selection statements to calculate the correct price for the order, and then display the total to the user.
Pause the video whilst you have a go at the activity.
How did you get on? Did you manage to add the selection statements? Great work.
Let's have a look at the code together.
So between lines one and nine, we have all of the print statements which tell the user the prices of the sandwiches.
On line 10, we have the variable, total, which is initiated to the value 0.
On line 11, we have the print statement which asks the user, would they like a sandwich or a wrap? And then on line 12, we're storing that input as choice.
On line 13, we have our first selection statement, if choice is equal to sandwich.
Then on line 14, total is going to be equal to total plus 2 pounds 80.
That's how much the sandwiches are.
On line 15, we have an else, so if it's not a sandwich, it's going to be a wrap, so, on line 16, we have total is equal to total plus 3 pounds.
On line 17, we have another print statement which asks the user if they want to add crisps.
On line 18, we're storing their input as the variable, crisps.
And then on line 19, we have another selection statement which says, if crisps is equal to yes or Y, then total is going to be equal to total plus 30 pence.
You'll notice, on line 21, we're also using the round function here, and that's because we want to round to an appropriate number of decimal places.
So we're doing round, open brackets, total, 2, closed brackets.
That'll round two decimal places.
Don't worry if you haven't added this line of code to your programme.
And then on line 22, we're then printing out the total value of the order to the user.
Note that the selection statement on line 19 doesn't have an else, and it doesn't need one because we're just checking if the user wants crisps.
If they do, we're going to do something.
If they don't, we can just continue with our programme as normal.
For part three, extend the sandwich programme to tell the user they've earned a sandwich stamp if they have spent more than 3 pounds on their order.
Pause the video whilst you have a go.
How did you get on? Great work.
If you want to see the full working solution, you can go to oak.
link/sandwich-order-solution.
For part three, you were asked to extend the sandwich programme to tell the user if they'd earned a sandwich stamp.
So I've just given you the lines of code here for this part.
So on line 23, I have if total is greater than or equal to 3 pounds.
If it is, then I'm printing out, on line 24, you've earned a sandwich stamp.
Remember, if you need to make any corrections to your code, you can always pause the video here or go back a few slides.
Okay, we've come to the end of today's lesson, and you've done a fantastic job, so well done.
Let's summarise what we've learned together in today's lesson.
Relational operators are used to compare values in expressions.
Expressions return as either true or false.
Relational operators can be used in selection statements.
I hope you've enjoyed today's lesson, and I hope you'll join me again soon.
Bye-bye.