Basics of Programming #9 – Selection

Basics of Programming #9 – Selection


OK, then, so, last time I asked you to have
a go at these tasks and I really hope that you did – especially this last “BMI Calculator”
task that I asked you to do, which I’ll do with you right now. And, if you haven’t already, give these
tasks a go now (link is in the description again). Now, if you don’t get how to do any of them,
there is an answer thing, which fully explains how to do each task in detail, so make sure
to look at those. Now, I’m not sure if you recall, but, back
two episodes ago we were talking about the “programming constructs”, which are the
three most fundamental parts of programming. Now, in the past two videos I went a little
bit off from that and started talking about variables and data types, but I did that just
because we really need to know what variables are and how they work in order to do the next
two programming constructs. So, in the last programming constructs video,
we looked at “Sequence”, which is the idea that code executes from top to bottom
– so, yeah, we know that idea very well already, and, as we go on, the other two will
slowly just become natural and you won’t have to really think about them. This video is about “Selection”. Selection lets you run specific code in certain
circumstances. So, if I have a variable called “name”,
and I can say do this when the name variable is set to “Alex”. This is an extremely important part of writing
programs, programs will have thousands of these things that are checking if a variable
is something, or whatever. So, let’s look at how we write these in
C#. Let’s create a project and call it “SelectionTutorial”. Now, here’s what we this program to do,
we want it to ask the user to input their name. If their name is “Alex”, then it will
say “Sounds like a very good person”. So, first, we’ll ask the user to input their
name, so, we’ll create a variable called “name”, and that will be a string, of
course, because their name is text. Then, we’ll do a “WriteLine” to literally
say to the user “What is your name?” Then, we’ll use a ReadLine to actually get
the user to input their name. So, this is all standard stuff really, we’re
just asking them to input their name and putting it into a variable called “name”, right? Well, now we’re going to use “Selection”
to make it say that they’re a good person if the name is “Alex”. So, in C#, and almost every other programming
language in the world, to do selection you write the word “if”. So, that’s the first part, but, then we
need to put in the “condition”, the thing that has to be true in order for our code
to run, which, in this case is whether name is equal to “Alex” or not. And, we put this condition in brackets, like
this. OK, now, we need to write the condition. So, we’re saying if name equals the string
Alex. So, this is kind of just like English. If the name equals Alex. Now, one thing you may have noticed is I wrote
two equals signs here, not one. And that’s because in C# you use two equals’
signs to compare. If I wrote it with one equals then it’s
saying to change name to “Alex”, and then somehow convert that into a Boolean – because
the condition is either true or false. Which is completely different to using two
equals’ signs, which gives us a Boolean, which is true if the two are equal and which
is false if the two aren’t equal. So, basically, use two equals’ signs in
“if” statements. Right, now, we need to put down “curly”
braces, you know, these things we see here, here and here – and I’ll explain why we
need these in a minute. So, I’ll put the opening one and hit ENTER
and Visual Studio will automatically create the closing one and put them into new lines
like this which looks very neat. Then, we will make our program say to the
user “You seem like a good person.” But, now, we’ll put this inside the curly
braces – the code we put in curly braces is the code that is run when this condition
is true. Now, you’ll notice that as I type it’s
been pushed over to the right a bit – this is called indentation. Now C# doesn’t technically need indentation,
because it knows it’s after this opening curly brace and before this closing curly
brace, so it knows it is part of this if statement. But, indentation is very helpful for us when
reading the code because it makes it clearer what code is part of what. You’ll notice that our code is already indented
quite a bit already because it’s inside these three curly braces. So, anytime we put code inside two curly braces
we push it over a bit in order to make it clearer to read. Now, Visual Studio will automatically do this,
but if it didn’t you would use the TAB key, which looks like this on the keyboard, to
insert an indent. So, if I go to the start of this line here
and hit the TAB key that will indent the line. And to get rid of that indentation, you can
just press backspace until it’s in line with the rest again. OK! So, this is what we’ve written. We ask the user for a name. Then if the name equals “Alex” then run
this code in the curly braces. And, this code is “indented” because as
you build bigger and bigger projects it will make it will make what’s inside what clearer. So, let’s run it first and see what we get. So, we type in something that isn’t “Alex”,
we get nothing. But, then, we type in “Alex”, and it tells
us that, that sounds like a good person – which it is. OK, now what do you think will happen if I
put a WriteLine right here. What will happen when I type “Alex”, and
what will happen when I don’t type “Alex” – when will it print “—“? Right, let’s try it. So I type something that isn’t “Alex”
as the name, and it gives me these three dashes. Which is coming from this WriteLine. OK, now, let’s try it with the name of “Alex”. OK, now it’s telling me that I seem like
a good person and it gives me a “—“. But why? Well, let’s go back to our favourite thing
– debugging! Instead of starting at the top, let’s start
from the “if” statement. So, let’s first see what happens when I
input something that isn’t “Alex”. So, you can see that I entered the name “a”
here, and, if we actually put our mouse over the “==”, it will tell us whether this
condition is true or false, and it’s false, because “a” is not the same as “Alex”. So, that means the code inside the “if”
statement won’t run, right? So, if we press F11, it will skip the code
in the curly braces because the condition was false. So, it will just print “—“, and that’s
it. Now, let’s look at what happens when we
do enter the name “Alex”. So, if I look, the condition is now true,
so, if I press F11, it will now run the code inside the curly braces, see? So, it will say that Alex is a good person. So, now, it will continue running down in
sequence. So it will leave these curly braces, and write
the “—“, as well, right? It doesn’t just run this, it will continue
to run all of the code after the “if” as well. OK! Now, what if I wanted it to write “You don’t
sound like a good person” if I don’t type in “Alex”. Well, there are two ways of doing this – one
is to make another “if” statement, that says if the name is not “Alex”, then do
this. Or we can add an “otherwise” to this “if”
statement that’s already here. So, instead of if name is Alex then do this,
we can actually make it say if name is Alex then do this otherwise do this. Let’s do the first one. So, we write if, because we’re making an
“if” statement. And then do “name”, but, instead of a
double equals, we do an exclamation mark equals. And this means it’s NOT equal to, because
we’re saying “name is not equal to Alex”, right? So, we do curly braces again, and in this
situation, we will make it say “You don’t sound like a good person.” So, now, our code says: if the name equals
“Alex”, do this. And if the name does not equal “Alex”,
then say this. So, if we run it, and we type something that
isn’t “Alex”, it will say that they don’t sound like a good person. And if we do type “Alex”, it will say
that they are a good person! Cool! OK, now, I also said that there’s a second
way of doing this, but we can only do it because we already have an “if” statement that
is checking if it does equal “Alex”. Basically, it’s an “otherwise”, so we
have our “if” statement here, if we put an otherwise straight after the ending curly
brace of the “if” statement, and put some code after that, it will only run that code
when this condition is false. So, let’s do that, now in C#, and almost
every other programming language an “otherwise” is called “else”, e, l, s and e. So, just to clarify: This code says if name
is Alex, then do this, and otherwise do this. That’s it. So, that will do the same. Now, there’s one problem with this code. You’ll notice that if I type “Alex”
without a capital “a”, instead of running this code, it runs this code. So, if I type it without a capital it says
that they aren’t a good person. Now, that’s not correct. That’s happening because when we compare
two strings, it is checking if the capitals are the same. Because, if you think about it at a binary
level a capital “A”, has a different binary number to the lower-case “a”. So, how can we check it, ignoring the case? Well, what we can do is convert both the string
on the left and the string on the right into lower-case (non-capitals). So, let’s say name looks like this. And we’re comparing it to this. Now, as it is, this is of course going to
give us false, because the capitals are different. But, if we turn the one on the next into lowercase
and turn the one on the right into lowercase, now they’re both exactly the same, aren’t
they? So, in order to turn the “name” into lower-case
all I have to do is just add a “.ToLower” after it. And, as usual, when you want to get C# to
do something, like, actually calculate something, you have to put brackets after it – sometimes
you fill these brackets with details, and sometimes you just leave them empty because
it doesn’t need any more details than it already has. OK, so, that’s the one on the left. Now, for the one on the right, if you wanted
to be really extra, you can technically just put a “.ToLower” after it – but that
is completely unnecessary, and you’re making the computer do extra work for no reason. Because in order to make this one lowercase,
literally all you have to do… Is just change the capital “A” to a lower
case “a”… That’s all it takes. Now, just to expand on our knowledge here,
when I do .ToLower here, we are not actually changing the name variable. So, if I just wrote “name.ToLower” here
just randomly. And I run it, notice how after I’ve run
this line – name doesn’t change? It stays the same, that’s because “ToLower”
doesn’t actually change the name variable, it takes what was in the name variable, makes
it lowercase and gives it back to us – just like “ReadLine” – “ReadLine” isn’t
changing “Console” or anything is it? It gives us a string back and we can choose
what we want to do with it. If we want to put it in a variable, we can
put it into a variable. If we want to put it in a condition, we could
just put it in a condition, like this. The same applies to “ToLower”, when you
run “ToLower”, it doesn’t actually change the name variable – it just gives us back
a string, and we can choose what we do with that. So, in this case here, we’re throwing it
away – we’re not doing anything with the string it’s given us back, so there’s
actually no point in running it here. So, if I did want it to change the name variable,
I would put “name=” before it. So, now, this will turn “name” into lower-case
and gives us back the string, then we take that string and change “name” to equal
it. So, this will now make name lowercase. Anyway, let’s move the “ToLower” back
into the “if” statement. So, in this case, this works out what the
lower-case of “name” would be and gives it back to us. And then, whatever it gave us back then gets
used in this comparison. So, hopefully now that kind of makes sense,
this “ToLower” doesn’t actually change the variable to be lower-case, it takes what
was in the variable, makes it lower-case and gives us back the string, which we can then
do whatever we want with. OK! Cool, now, there’s also a thing called “else
if”, which is an “else”, mixed in with an “if”. So, if I add an else if, after this if statement,
like this, let’s do “Dave”, and just make it say “Hello” … What this means
is… If the name is “Alex”, then do this… But if it isn’t “Alex”, then check if
it’s “Dave”, if it is “Dave”, then do this… But, if it isn’t Dave or Alex, do this. So, you could, for example, make some kind
of multiple choice game with this kind of design – you could say if you entered “A”,
do this, if you entered “b” do this, if you didn’t enter any of these, then that’s
an invalid input, and so on. Right, one final point as well – let’s
mention integers, and any number type, including floats for that matter. He we have the integer “3” – I’m not
going to bother putting that through “ReadLine”. Now, just with strings we have is equal to
and is not equal to. So, if I write an “if” statement that
checks if it equals, and prints to the screen if so, then, you’ll see that works fine. The same goes for not equal to, which is an
exclamation mark and equals. So, if I check if it does not equal “5”,
which it doesn’t because it’s “3”, this will also run fine. But, the two things that it has that strings
don’t have is less than and greater than. Now, as less than symbol looks like this – and
a greater than symbol looks like this. Now, it can be a little bit confusing to remember
which is which, but here’s an easy way to remember it that I learnt when I was a little
kid in Maths. And I know it is kind of cringe worthy, but
it works. So, think of this as a crocodile, and this
is its mouth – and its mouth is wide open. Basically, the crocodile always wants to eat
the bigger number. So, if its mouth is pointing right, like this
– then that means the one on the right is the bigger one, and if it’s pointing to
the left, then that means the one on the left is the bigger one, because it will always
point – because the crocodile will always want to eat the bigger one. So, if I write an “if” statement, and
in the brackets put this down. Then, if you look, the crocodile is looking
right so that means this is checking if the right number is bigger. And, since our variable is “3”, which
is smaller than “5”, this will actually be true – “5” is the bigger number,
and the same applies the other way around. OK, so, I’ve gone ahead and added some more
tasks to the website, now, I want to go through this grade calculator with you, because you
need to think very logically to do it. In this grade calculator it will ask them
to enter a percentage and then tell them which grade that percentage is. Right, let’s write the basic stuff, so,
we need to make a variable to put the number they typed in into, let’s call it “percentage”,
then, we need to make a variable that we put the grade we’re going to output in – that
will make sense in a minute, so, we’ll just call this “grade”, and it will be a string. Right, next, we need to input their percentage,
no big deal there. Now, this is where the selection comes in. So, first, we want to check if the percentage
is greater than 90, and if it is, then we’ll set the “grade” to “A*”, right? Then, we’ll say if the percentage is greater
than “80”, we set it to “A”, now, there is actually a bug here, but we’ll
get to that in a minute. OK, so, now I’ve just filled the rest in
all the way up to “D”, so, if the percentage was bigger than 90, then we set the grade
to “A*”, and if it was bigger than 80, then we set it to “A” and so on. Now, there’s actually a problem with that,
but in order to see it, we’ll start debugging. So, let’s say I enter the percentage of
93, let’s see what happens. So, first it asks if “percentage” is bigger
than 90, which it is, so it sets “grade” to “A” fine, right? But, then, watch what happens, it then asks
if the percentage is bigger than 80, which it also is… So, it now replaces our “A*” with an “A”,
then, it does the same with “B”, “C” and “D”, yeah, that’s not right. But we can fix this using “else if” instead. So, if also of these are else if – then
that fixes the problem. Because, remember an “else if” is the
same as an “else” and an “if”, these if statements will only run if the one above
them was false. So, if I typed “87”, then because this
first one is false, it would pass down to this one and so on. Now, we also need to handle an “E” grade
being less than 50% – yes, I just made all of these up and there is usually an “F”,
but whatever. So, I will say if the percentage is less than
50, then set the grade to “E”. However, there is one problem with this – what
would happen if I entered exactly 50? Well, it wouldn’t be this one, it isn’t
bigger than 90. It wouldn’t be this one, it isn’t bigger
than 80, not this one, not this one, and not this one either because this is greater than
50, not equal to 50. And, not this one either because this is less
than 50. So, what we need to do is make it, so it checks
if it is less than or equal to 50, and to do that just put an equal after the less than. So, this means if it is less than or equal
to 50. Great! Now, we’ll just print what “grade” is
to the screen. Now… (This is actually quite annoying really),
but a little failsafe in C# has been triggered here. Because the C# compiler cannot guarantee that
grade will be changed. And C# doesn’t like it when you try to access
a variable that you haven’t set at least once. So, basically, just to get rid of this error,
at the top here, just set “grade” to nothing. So that way it starts off as nothing. As I said, this is… Quite an annoying little… Failsafe C# has. It’s just there to stop you from not setting
a variable, and then… You know, print or using that variable when
it hasn’t actually been set to anything. So, don’t worry about it, if you get that
error that it hasn’t been set, just set it when you declare it and it will be fine. And, it asks us to enter a percentage, let’s
do 73 – and, it is indeed a “B” grade, so that’s pretty good. Now, there’s an extension to this task which
is to add a “U” grade, now, we have to be careful about this one. Because, if we add it like this, so, “else
if percentage==0”, it will never run. Why? Because this statement here will always be
true, won’t it? If it’s “0”, then it is less than 50,
so, it will never get a chance to this run this. Luckily though, we can fix this just by getting
rid of the “else”. So, that means it will run regardless of whether
this one ran as well. So, we’ll set the grade to “U”, so,
now, if I type “0”, it will give me a “U” grade! Great! Well, no, the grade isn’t very good, but
the program is. So yeah, keep that in mind when you attempt
the rest of the tasks. They need more thinking than the last episode’s,
but, you can still do them, just remember to debug if it’s not working – debug it,
don’t give up and go straight to the answer, stick a breakpoint in there and watch it run
for yourself, see what it’s doing, what all the variables are set to and why it isn’t
working as you expected. Right, so, that’s selection, that’s the
second programming construct, you can download this image in the description as usual. OK! That’s it, that’s all there is to it. So, the link to the rest of the tasks are
in the description. Now, can I ask that you do each task in their
own separate project, because in Episode 11, we’re going to come back to all of the tasks
and change them a bit. Anyway, this video really has gotten very
long now, and we don’t want a repeat of Episode 2’s length so, that’s it! In the next video we’ll talk about iteration,
where our knowledge of conditions and selection will be extremely important. Bye!

1 thought on “Basics of Programming #9 – Selection”

Leave a Reply

Your email address will not be published. Required fields are marked *