In this lesson we will be looking at conditions statements in C sharp.
By now I'm sure we all have the hang of creating new projects.
So I've already created a new project and I'm calling this one condition statements and I already cleaned
out the main function to put in some comment and so you can pause the video here.
Go ahead and create your new project inside your solution.
And once again it is called condition statements.
No.
A little prelude before I get into what condition statements really mean.
I want us to examine something about programming in general.
Most if not all.
Well every programming language has three control structures.
They are sequence decisions or selection and repetition sequence basically refers to the fact that the
program is going to execute the commands in the exact order that you placed them.
So in previous examples are we printed a prompt and then we waited for input and then we prompted again
notice that it went in the exact sequence that we wrote.
So that's the sequence.
However when we get to our roadblock and we need to maybe make a decision to see if something meets
a certain criteria then take this action or take on other action then that is another control structure
called selection or decisions.
Knowing this lesson we'll be exploring two condition statements that help us to make selections.
They are if statements and switch statements and then we're going to look at ternary statements as a
bonus.
Let's get started looking at if statements are typical reason what we would need to use and if statement
is to determine something or some action based on some condition.
So let's just say that I wanted to bring something to the screen if something was the way that I think
it may or may not be right.
So let's just say I wanted to see if and when to keep it very simple and I'm going to say something
like If tool is less than 3.
So notice this is a new operator we did arithmetic operators earlier.
We'll have the comparison operators where we're comparing is to greater than or less than this is less
than three.
All right so tool is less than three if two is less than three.
Then I want to do something.
Please note the syntax behind this statement.
The key word is if then I open parenthesis then I place the condition inside of the open parenthesis
and then I close.
So inside of the parentheses I should have a condition is something maybe equivalent to something.
Is it less than is it greater than there are so many ways we can compare things.
But I'm just checking if 2 is less than 3.
Then I want to print something to the screen so I can see a console dot right.
Line and then went to say yes it is all right.
So then I can see if 2 is less than 3 then print.
Yes it is.
What if there was another condition or if I didn't if that wasn't the condition then I wanted to take
another action.
So if it is then print print.
Yes it is.
And then I can see else.
And then after this I need my current braces again.
And then I want to see what what would I do if this condition is not met.
Someone to take this console that line and duplicate it just to expedite this and I'm going to say no
it is not.
So once again I am seeing if two is less than three then print.
Yes it is.
Else print.
No it's not.
Now you notice that this line kind of looks a bit green dot are kind of dull and Visual Studio is basically
just telling me that based on the condition that I specified here I would never ever get to the OS which
is true because every time when winter on that unless I change these values to will always be less than
three.
So yes it is will always get printed.
The Ls would never get printed but I'm still going to leave the L's because then I'm going to show you
different conditions that can go in here and hold the code would be executed.
So let's execute this and see the end result.
So once again it's going to run in the sequence and it's going to see if 2 is less than 3.
What should I do.
So let's check that out.
So remember that you can change your starter project using this dropdown.
Let's just make sure that it's on condition statements and go ahead and click Start and the end result
is that it prints.
Yes it is.
And that's because the condition was true.
No.
If I change these values to something that I want to say no it's not then I can say is 10 less than
maybe three are.
I'll leave the three.
So if 10 is less than three then we want to print.
Yes it is.
Else print.
No it's not.
No.
Just looking at it we know that 10 is not less than 3 so we expect that this condition will not evaluate
to True those it won't do what is in the first block but it will do what's in the else block.
So else is basically the force for any condition that is outlined instead of the if.
So let's try that again.
I want to run the program and the expectation is that it will say no it's not because 10 is not less
than 3 and there we go.
No it's not all right.
So that's a very very simple example let's put up a bit more meat on it so I'm going to find some variables
I'm going to define number one and number two also int num 1.
And please note with variables I may not have mentioned this before but when you're using variables
or when you're declaring variables you want to avoid spaces in the names and you want to avoid special
characters almost the same rules as when I suggested how you name your projects.
You can chemically case the names if you have multiple words some people use underscores but ultimately
just avoid any space and any special character.
All right.
So no one is going to be equal to two and Noam two is equal to five and see in doing that I violated
my own rules and Visual Studio is telling me that I'm wrong.
So int num 2 is equal to 5.
And before I use these two variables in the example I just want to take this time to list all the other
comparison operators that you have to your advantage.
So you have.
And I'm just putting the brakes there so you can see them so you have the less than you have greater
than you have equivalents.
So notice 2 equals science or the equal sign by itself is an assignment operator where you're assigning
a value from the right to the variable on the left.
However if you want to compare if two things are equal to each other then you have the equal equal all
right.
So that's the distinction between a single equals and a W equals side.
So we have equal equal.
We have hybrid ones like greater than and equal or less than an equal.
And then you have one that say is not equal which is an exclamation sign an equal sign.
All right so those are operators that you can use inside of your if statements when you want to compare
the value on the left with the value on the right.
Okay.
So don't we have no one to name two.
We did this with static values but we can also write an if statement with variables.
All right.
So I could be comparing number one with the number three so I couldn't say if no one is less than three.
Then I want to take this action otherwise I want to take that action.
All right.
So in this situation I have no one declared to have a value of two.
So if I execute this then two is going to be less than three or verify I said No I'm five here based
on the value of num five then num five oh sorry num two sorry.
Number two has the value of five my butt.
So if I say no two is less than three as long as Num two retains the value of five then we we'll know
that it is not less than three.
And so it will go to the else block and print.
No it's not.
So you can use variables to compare with static values and you can also compare two variables.
Number one and number two.
All right.
So here's what I'm going to do.
We're going to write a full program where we're accepting data from a user and then we will run an if
statement to know what to print or based on the input from the user.
So I'm going to write a program that's going to ask our users to tell us how many apples they have and
how many oranges they have someone to console a lot.
Right.
The first problem to see how many apples do you have.
And then we'll do a console dot read line where we get the inputs but then of course we can just console
that read line this value needs to go even to a variable someone's use no one to store the number of
apples.
So no one is equal to the console that read line.
And then we know that no one is an integer.
So if we're putting something into the integer from the console inputs we need to convert it someone
to say convert dot to int 32 and then open for in the CIS and close parentheses around that and then
I'm just going to copy this line and settle for typing it from scratch and then I want to say how many
oranges do you have and we're storing oranges in Gnome 2.
So no I have user input for gnome 1 and user input for gnome which of course will override the values
that they were initialized with because I'm not going to trouble the initialization here or at least
I would always recommend that if you're initializing you always initialize to a safe enough value that
you know it doesn't tamper with your operation going forward.
So we're initializing gnome 1 to 0 and num 2 to 0 and then we're prompting for the number of apples
and starting that's in num 1 and then I'm prompting for the number of oranges and storing that in num
to know my if statement is seeing if number two is less than number one then I want to take a particular
action but then I feel probably more comfortable saying if gnome 2 is greater than no one it really
doesn't matter.
So based on your context and based on how you probably would want to if you can see it in English or
see it yourself how would I compare these two.
Then you can just translated into code using the appropriate operator.
All right so I'm going to say if no one is greater than num 2 then that means that I would have more
oranges.
So gnome 2 represents oranges so if num to is greater than the one that I have more oranges than I have
apples so then my console or a lion is going to be you have more oranges and then if I if that is fours
meaning num two is not greater than no one then I'm going to print you have more apples
not less.
The quick test and see the Oh come off that.
So how many apples do you have.
I'm going to say I have two apples and I have five oranges.
So the expectation is that it brings you have more oranges.
Alright let's do another test where I flip the script this time I have ten apples and I have five oranges.
And so when that evaluates in my if statement I should be getting you have more apples.
There we go.
So that's basically what a selection does it looks at a condition and then it makes a decision based
on the outcome whether it's true or false for that condition.
Now the if statement can be expanded a bit more and I'm just going to do this and then we leave if statements
for no because if statements are very useful and as we go along we will be using them and see more purposes
but I just want us to have an appreciation for how they work and why we will be meeting them going forward.
But the if statement can also have more than one or more than two blocks so right now we're just looking
at if condition and then else but then you do have the facility to have multiple conditions in one big
if statement so I can see if not two is greater than no one then print you have more oranges then I
can see else if I then state another condition so I'm going to see if no one is greater than gnome 2
no one being apples and no to being oranges.
So first I'm taking to have more oranges than apples that I can see otherwise if there are more apples
and oranges then I want to print you have more apples What if I wanted to add on another statement and
other condition I said else if and then state another condition and then for that part I would want
to see if they are equivalent.
So I can see if no one is equivalent to Gnome 2 then open Bruce console the right line and then I'm
going to see you have booths or let me say the same you have the same number of apples and oranges.
Now we have one if statement doing three different conditions.
All right.
So I've if num two is greater than num 1 then 4 in something else if no one is agreed to then to then
print something else if no one is equivalent to Gnome 2.
So there are plenty of ways you could have written this but then I mean there are so many ways or at
least I can negotiate every single scenario.
Once you appreciate the concept then you can be more flexible.
So in this situation once again I'm just checking if num two is greater than no one then I want to print
you have more oranges.
And then if that doesn't evaluate to True then moves on to the next statement to see console the red
line you have more apples right.
And if that evaluates the falls and he moves onto the next condition where it says okay if they're equivalent
then print this statement.
No there are situations where none of these would ever match and then that's why you have the L so the
S is that a catch.
All right.
So if we have an S.
You usually use that to say whether when every other condition fails then take some action.
And this is once again is a catch all.
So I went to see final action.
Ah well invalid parameters No.
Based on this example I don't know what I could possibly put in that none of these would get met.
And further to that the comparison far greater than an equal to unless that an equal to would not fit
into this scenario.
And well we always expect it if one of these would be met then clearly they're not equal right but then
the case study or the use cases for these rather would be like if you are comparing grades.
Right.
So if the grades start at maybe an A starts at 90 then you can see if you have agreed that is greater
than or equal to 90.
Then you have an E.
If they fail a failing grade is fifty nine then you can see if you have agreed that is less than or
equal to fifty nine then you are a failing student so you could have done the greater than an equal
to for that A.
Or I could have just said if your grade is greater than eighty nine because then we know that anything
greater than 89 is probably 90.
But then based on the scenario once again you may or may not need to use it or you can use multiple
to accomplish the same thing.
Anyhow let's run a test on this so I'm going to put in some scenarios where I'm going to put in some
values and then we should put in values that will invoke each statement as we go along.
Once again the L statement may never get called because one of these conditions will be met anyway so
let's try it out.
So firstly we want a bigger number for num 2 than for an on one.
So we want more oranges than apples.
So 5 apples and then I have 10 oranges.
So num 2 is greater than no one.
So you have more oranges all right.
Let me run this theme and this program again this time I have more apples than oranges of ten apples
and I only have five oranges then it is you have more apples.
So this condition for no one being greater than num num 2 being greater than on one field.
So it went on to the else if and then this condition was met.
So it printed that statement.
Now if I have the same number between the two fruit types.
So that means I have ten oranges and ten apples when I press enter.
Then it went to the else.
If they are equivalent then you have the same number of apples and oranges.
All right.
So there are some rules surrounding whole if statements should look.
Notice the special toolkits that I've been using along the way.
The key words if and then the parentheses and then the condition inside the parentheses and then notice
no semicolon at the end of this line.
And then I opened the curly brace and then the C sharp syntax as we've come to Lloyds with all the consular
rights consular read line whatever it is that we're doing that those can proceed but then only between
the braces so I can put any code in this line I can't be console the right line in between the if block
just like that.
Right.
That is illegal.
So when it whatever operation it is you need to carry out a notice of that red line.
So it's saying that it's expecting it's incorrect syntax for a bill until you details about the incorrect
syntax but just follow these rules and you will should feel comfortable in writing your statements.
So once again you have a condition you have an if statement sorry you create the condition inside of
the parentheses and then you open the curly brace and then you proceed to write all your code inside
of your curly braces whatever you're acting instead of the curly braces is what gets done when that
particular condition is met.
So if num two is greater than on one then I want to take this set of actions.
Else if this other condition is met meaning this one failed but then this one gets met then I want to
carry out these actions and then I have another s if if this condition is met then I have my specific
actions and then my eldest is the catch all meaning that none of these came to pass.
None of them matched my requirements so that else would be that a catch all.
So you know based on the situation then it may be good it may not be but so in this situation if it
will go to the else I know that that's bad because that's not what I expect.
However in our first example you saw that I only had if condition and then else.
So when it's a simple condition where it's either R it's it's either this condition is true or it's
not true then you can just use an if else if you have five different scenarios that you need to test
or in this case three different scenarios you need to test then you can write if elusive else if and
if you know that they're the only three then you really don't even need the else.
So it can work without the in this scenario like I said this is an introduction and so we're just scratching
the surface with statements but I'm going to move on to switch statements which help us to make selections
but just do it a bit differently so that your statement gives us the ability to make more complex decisions
where we can see if something is greater than we can compare to values or some variable to some other
value whatever it is or comparing and then make an action the switch statement over we have to look
at a variable and then make a decision based on just the value of the variable.
So I'm going to give you an example.
So the switch statements look something like switch and then it takes a variable inside of the parentheses.
So what I'm going to do is to find another variable and I'm going to call this one num three so into
num three and it's going to be equal to zero.
All right so I'm going to see a switch num three which means assess the value inside of num 3 then I'm
going to see keys and then I can give a value.
So case 1 then call on so please note the syntax because this is very important that you follow the
syntax properly so you see a switch open various or open parentheses put a variable in there and then
you open your current embrace and then you make a key CSA case then the value that you want to assess
this variable for.
So I'm seeing if the value of num three is one then I say call on what comes after the call on is the
action I intend to take so that I'm want to see console that right line and let's keep it simple and
just save.
Value is one.
All right.
And then after I say that then I need a keyword so I can list as many actions underneath this.
So what comes after the case would be just that what comes inside the curly braces for the if statement
but then I have to end the case with a keyword that says break.
All right.
So I can see Case 1 consular land value is 1 so I'm just want to copy this I can retype it then what
if I wanted to do something else if the value was to so I have case to call on then I state my action
someone to console the right line and I don't want to change it from values 1 and say something like
I have to know.
All right.
So I have to know if the value is to print I have to know to the screen and then once again I can print
as many things as I want or take as many other actions as I need to someone to print this three times
or three more times.
So and this is just for example I'm just showing you that you can have multiple lines coming.
Often these after the case and the value and the call on and then when you're done you just make sure
you put your key word that is break if you don't put the brake keyword and let's say I forgot the brake
keyword then what would happen is that it would just run on so he can have as many cases as you feel
the need for it.
So as many situations or cases that you think are values that you want to compare this variable again
you can have case now whatever that value is since we're using integer we just put the value as integer
if this was a string then I would be seeing keys and then putting in the string.
But then we have to keep it type sensitive so if it's an integer variable that we're assessing then
the key is value must be an integer if it's a double then well it has to be numeric if it's integer
double if it's a string then it must be a string but then otherwise we have similar to the Ls in the
if statement we have a default situation that says well if none of the cases match then I am going to
do something.
So I'm went to inside of the default this console right line and see invalid value.
All right so I'm only comparing 4 1 and 2 which means that if if GNOME 3 is never 1 and not 1 3 is never
2 then I want the default to come into play and see invalid value and then once again I break once I've
done that everything is inside of that curly brace.
So I'm just going to put in a prompt for gnome three at the same time.
So after we do with our apples and oranges I'm going to see how many eyes do you have.
That's a good one.
That's right.
It's either one eye or two eyes but then I I'm just going to say invalid value if you put any other
number that's not one or two.
Right.
So how many eyes do you have.
And then I'm going to console the right.
Right.
Red line sorry into num three.
So let's test that one out.
So I don't want off to do all of this again so I'm just going to comment oat my if statement.
So the shortcut to comment is control key and see.
Right.
So holding on after highlighting the lines I just held on control of press K press C and then all of
those lines that were highlighted got commented so no when I execute my program is going to ignore the
comments on lines and just go straight to asking me how many eyes do I have.
And then taking an action based on our values.
So I have one eye then I want to say value is one.
All right.
Maybe I can make it a bit more intuitive.
Some want to change it and see you have one.
I
write this is just for educational purposes it's not really necessary.
So if you don't feel like changing it then that's fine but you have one eye and then I want to try it
again and then I'm want to put in the value too.
So with two we expect to have four statements being printed and I have to know it's printed four times
so once again you can have as many lines as required underneath the key.
So in between the case definition and the keyword break everything in there will get executed.
So let's do another one and we will put in the value of three so three since there is no case defined
for a value of three we'll go to the default and tell us in valid values.
So that's basically how such statements work.
A good use case for us suits them.
It would be like in a menu setting where you're expecting certain values so if you say like Press 1
to enter grade press 2 to view grade press 3 to exit program then you could just see get the user's
input into the variable and then you were a switch against that value and then based on which case was
chosen or which value was selected you run a case for that.
You take the actions accordingly and then if they enter a menu option of 5 when you only have three
menu options then you have the default.
All right.
Now as a bonus I'm going to show you ternary statements and I left this one for last because this one
can be a bit difficult to grasp and they are always our own using it but it can be a bit difficult but
it's very very convenient.
Once you understand how it works.
So I'm going to find another variable and I'm going to call this one result.
So this one is going to be string and I'm calling this results all right.
No.
The purpose of the Turner operator is to run a quick comparison and it allows you to decide right there
on the spot what value to assign to a variable when that condition is met are not met.
So in other words with our if statement if I said no to greater than no one I could have done an assignment
to a variable in here but then I would have to say number tool is equal to the num num 1 groups that
I known to and then do another assignment here and then on this one after do another assignment.
Whereas in a ternary operator I can see the result is equal to and then what I want to compare is num
to being greater than no one.
All right so this is my condition.
And then I can use a call on and then I see and I think usually this needs to go inside parentheses.
So as this puts it in parentheses.
So then I say this is my condition.
And then question mark sorry.
Question Mark and then I'm going to see no one greater then no to greater than no one.
Then I'm going to say what what did we assign more oranges.
You have more oranges when that is so you have more oranges and then call on and then we're going to
say you have more apples all right.
So let me just start over.
I just happened.
So we have a variable and then I'm seeing what the contents of this variable depend on the outcome of
this comparison.
So my variable result is going to be assigned some value based on the result of this compiler.
So what you see is num to agree to that number one then I have the question mark to see if this is true.
So all of this is seeing if this comparison is true then I must say ending that value to the inside
of result and then the colon says otherwise it is this value.
So the quick thing the good thing about this is that in one line I just run a minute.
If else statement.
All right.
The downside is that that's all I can do I can't of a third scenario to say if if the greater than are
their equivalent sorry I can't of a third scenario.
So I just want on one condition and then I say based on the outcome what if it's true then I want the
first one to be done if it's false and I want the last one to be done.
Now once again this comes in handy.
A lot of the times but the if statements are very easy to do.
Such statements are very quick also.
But then this if you understand the concept you can use this at any point and expedite completing your
program that you are authoring.
So I'm going to leave this program went to on commend all of these lines so that if you get your hands
on the source code then you have all of these examples for you and so on comments that's control k you.
So let me just show you that.
So if I am on another line and I want to come into it I can just say control Casey and then it commands
it and then if I want to on comments I can just say control key you and there you go.
So you press K and you can see while holding don't control.
All right so that's it for condition statements.
That's a brush over.
I hope you understand the concepts.
If you don't feel free to reach out to me.
But as you go along we're going to be using them more and more so you will get to appreciate real scenarios
where you will need to make selections and decisions based on values that are coming into your system.