In this lesson you're going to talk about one of the most fundamental aspects of any programming language
or an algorithm and that is a conditional statement or what you call it if else statement which is some
sort of a control flow.
What it does for us is it helps us compare two or more objects of values or variables against each other
and depending on the result of the comparison we could do different things.
So let's go ahead and see this in practice so we get a better understanding of what it means as a concept
and then jump back into X code.
I'm going to close this project Stop this time and I say give me your command line tool.
And this one is called if else project.
So I might if else project I'm going to go into my main as usual when I get rid of all the comments
and then reformat everything nice and tidy.
And here is how I'm going to write my first if else statement.
Imagine a situation like this.
Imagine I have an integer called A which is 5 and I have another integer called B which is 6.
Now what they could do is I would say if A is larger than B then do this and this large say A is large
as simple as that.
Now we all know if I run this application now it's obviously nothing is going to happen simply because
in this case 5 is not bigger than 6 5 is actually smaller than 6.
So what if I wanted to have the are there a statement.
What if this wasn't true.
So I'm going to go ahead and say let's do this and this long b is larger and then rerun this code and
now it says B is larger B is bigger.
Now that we saw it once in practice let's better understand what it does essentially.
And if the situation depends on a condition which is here within this is that is either true or false.
So the condition that we feed into that if a statement is either true in which case the if part will
happen or it is false that the else or the L says will happen.
Talk about the Elsa's soon how we could have more than one else but let's see a different kind of practice.
Let's say I had to remove all of these.
So I had Boullion my Boullion was meant to walk are nuts for that or is fetched.
So that is true.
Obviously this is not something that I would make a static kill like this we would make this depend
on some other arised you.
And then based on that they would decide whether or network data is fetched or it is still we're still
fixing their ideas.
But based on that they could say if that is a fetch.
So their value is true or false if that's the case say and Aslaug we got the data and then if it is
else in the house you could for instance say and it's like still reading for instance or something to
that effect.
So again we have an if else a statement that works based on a boolean result of a conditional.
So the format of any false statement is essentially like this.
I have that.
So I have if condition and then if in case of true and then else in chase all from Faux now a little
bit earlier I said about the fact that there might be more than one else there might be Ellis says happening.
Let's see that once again and let's see how that could be the case.
And will writes My original quote in a five city is six and I'm going to say if a is larger than B then
obviously you say and this long a is bigger and this is what happens in the else in the else a is either
a smaller than B because that's not true or 8 is actually equal to be.
So let's see both of those in the else I'm going to say then if a is smaller than B then say and Aslaug
and this time say a is a smaller.
Simple as that.
And there isn't else.
If not neither of these two situations are correct.
A is not larger than.
So we ended up in the US in the ass 8 is another smaller than B then it means and it's like a is equal
to B and we can actually test.
Let me just get myself a little better for my thing so I can show everything that's wrong this could
of course right now that you say is a smaller but if they were both six for instance I read around this
chord is going to say a is equal to a B because it is flawed.
It didn't work.
It went to the else within the else.
It says Well is it a smaller.
That wasn't the case so it ended up in this one.
Let's rewrite this a little bit differently.
So I'm going to say if a is larger than B do that else if a is a smaller than B then I don't need to
have this mess.
That difference is statesman's.
I'm going to remove that get through the fall of it kind of makes sense now.
So I'm going to say if a is larger than B say is bigger.
That makes sense.
Else if a is a smaller than B say a is a smaller.
What is the tared situation is neither larger nor a smaller.
So else if neither of the two were correct say they are equal.
That's about down.
So you could either nest your false statements or you could use them in an else if situation.
Keep in mind that the else if situation is best used when your partners are the same like when you're
comparing a against B but you're not comparing a against B and C in that situation is much much better
if you actually miss your if statements just like what we did a few minutes ago.
Now in saying that let's say Now you want to expand your if statement into covering more than just the
sign of being larger or smaller.
What if you want to know if it is larger or equal.
So I'm going to rewrite this.
Say if a is larger or equal to B say and Aslaug larger or equal and obviously there is no to else there's
only one else in here and that is.
And this law is smaller so because it is neither larger nor equal it will be a smaller and larger or
each one.
What if I wanted to test if they're exactly equal.
And this is an important one.
So I'm going to say two equal signs and that is the sign of equality.
If you remember earlier on we talked about how a single equal sign is a sign of assigning values assign
values and two equal signs is a sign of comparing equality.
So if I say if a is equal to be two equal sides it means I'm asking are they equal against each other.
If that's true is going to print equal and then indeed as I could again write the same thing say well
if a is larger than B do something and say Steve and Alice the last one do something else.
So so far because these guys one larger is smaller than larger or smaller or larger or equal smaller
or equal.
There is another sign which is the not equal and that is how we use it.
Are you going to say going to get rid of all of these.
I'm going to say if a is not equal to be so if this is not happening.
So that sign you could use either alone which I would show you or I could use it with an equal sign.
So we could either say if a is not equal to B or we could say if this condition which is being true
is incorrect.
So you could use either of the two ways.
But for now we're going to use the easier one which is like this.
So I'm going to say if a is not equal to B then say not equal to B.
And that's another one.
Finally we should see how we can compare multiple different conditions against each other.
So I have another one and see and that is seven and I want to perceive a and b are equal but if B is
larger than c.
So what you're looking for A and B called for it to be larger than c.
So let's see how we could do that.
I could go ahead and say just like what we have done before if a is equal to B.
That's the condition I'm looking for in here B is larger than c then say and Aslaug.
Yea this is the one that we were looking for.
So that obviously works.
But again we are nesting if statements.
Maybe there is an easier way for it.
And it absolutely is.
And that is called.
And so I'm going to say if a is equal to B and to signs B is larger than c it means if both of these
conditions are true.
So I'm gonna copy that and I'm going to put it there.
So it's these two pieces of course they're exactly identical.
This one means if a is equal to B and also B is larger than c.
Now another thing that from time to time comes up is two conditions that we want.
One of them to be true and then I had this seen here so far we saw and as are now in this next example
I want to see this thing happen.
A and B are equal or B larger than c.
So I'm going to say if a equals B.
If this is the right thing or is B is larger than c so this is the sign of.
Or it means if this is true or this is true I'm going to be OK.
Either of these three if it is correct then this part is going to happen.
If none of them is correct then obviously the else part is going to happen.
Sometimes you care about the spot sometimes you don't care about the else you just want the if part
of your heart condition to be to happen.
So I'm going to add this one in here as well.
So that's a sign of or.
And one last thing before I close this lesson and that is some values they can be compared against each
other such as six again sevens against seven their boss or numeric models they are both integer values.
If too hot to floats if you hada the two billion nonuse you could compare them.
But for instance if you have two strings you can't just compare them against each other.
Let's see that in practice.
So if I have one and there's this thing called a string of one which says hello and then I had another
string that is called the string.
And this one is a random set of characters.
Let's say it's a or b or a unique ID for something.
Now I couldn't just going here and say You first two and one is equal to a string two simply because
there is no such operator between the strings.
The strings cannot be compared against each other like this.
There result of this will always fail.
So for situations like that there is a string or an array or a dictionary or things that you will see
down.
That's right.
You actually have to use the builtin equality features of these de-friend you know rivals for instance
in year I would say if a string of it doesn't matter which one comes first.
But if casting one is equal to a string string two.
So it's saying if this thing is equal to that the string and then obviously if it was true this part
will happen in the else.
The other part of it will happen obviously in this case.
The other part is happening now that we are here I should quickly mention something else as well.
Let me rewrite this.
That's not the.
The false statements or conditionals it's about the use of methods things such as this one.
We have a lesson about this coming up in this very section.
But I thought quickly you know I'll tell you about that.
So what's going on here say is string to 1 and you see what I'm typing and this menu pops which is the
auto completion menu.
It tells me if it is of the type and it's a string so string one is equal to a string and you see again
when the auto completion shows up and I'm typing that is equal to a string.
It tells me you have to give me another string.
So Carl I'm not on the string.
So you have two feet and not interesting to me that is not all.
It does have a certain value inside it.
And on the left side of it it says I will give you back a Boolean I will give you a blank either true
or false.
So is equal to a string.
Obviously here we have to feed the string get to the reason I brought this up is because whenever you're
using any of these methods you have to keep an eye on what you have to feed into them.
And also what you get back from them for instance if I had this mutable string this card it's my mute
that is just nothing for an hour.
And then I had a minute.
It says it is and not over the string.
Append the string.
It says Well if you want to use the append the string please feed me with this string and I will not
give you back anything because it doesn't return anything.
It just does something on my mutable thing.
I didn't see this much further in the future and that of text for instance we'll talk further about
formatting different methods and using methods.
But for now just so that you get some sort of familiarity whenever you're using any of these methods.
Make sure that you read these values lot when you're using this tell yourself oh I have to feed it it's
a string and tell yourself oh it's going to return a boolean.
It says returns a boolean value that indicates whether this thing is equal to the receiver using a literal
Unicode based comparison.
And I'm going to say what if this one is equal to a string to.
Obviously if you said and if statement is always based on a true or false and then based on that you
can decide what's going to happen.
So that's about that about using IF statements.
Once again really quick to reach out and if statements are conditionals based on true or false.
And they always are a part of the control flow and we will see more of the contraflow such as loops
on a solve in the future lessons.
So let's keep it up to here and we'll move on to our next.