Everybody wants to next video.
So in this video we're gonna be going over a few different ways to control the logic flow of an application.
So this is commonly done with if and else and then there's also a few called else if that we're going
to go over in this video as well.
So the first thing we do is we're gonna set up a few variables so we'll do the age twenty six.
Name is Brandon and then male is true.
I'm a male.
Now I get it set up some variables.
So let's.
What if we want to do some different logic.
So what if I want to know is my name Brandon.
So the way an if statement works is you have if your brackets or your parentheses and then some sort
of condition.
Now the condition must equal true.
Otherwise you have to specifically specify that you want it to be false.
So by default if something if this condition is true.
So for example if I just put true I did that it would condition is true and we would run the code inside
of the statement.
Now just to kind of show you that.
So if we print here
we run that we could see here was executed.
However if we put this is false and we run it again we're not going to get anything perfect.
So the condition must be true.
However in this case we say not false then that condition is technically true and then we pass it is
here.
So that's a that's a keyword example I'm gonna go over that in a little bit.
However we're just gonna look back at this.
So now let's say if I want to check to see if a string let's say in this case Brandon is inside of a
variable.
So that's simple you give it then the D string or this could be another variable in so if we if we've
done that correctly we should see here.
Perfect.
That means Brandon is a name.
So if we get rid of the O we still see here.
Now what's important to understand is when you're looking at this it's not a full match it's just looking
for if this string is inside of here.
So that so we can keep removing all the way up to what C.P.R. is B are in Brandon.
It is.
However if we want to check to see if it's equal to we're going to use two equal signs.
Now it's important to understand here is that when you're assigning something which is US assigning
the name Brandon to the name variable we're using a single equal sine when you're doing a check or a
condition you're going to look for double equal signs.
So now we've got B are is equal to equal to name which would fail.
We see that here that it fails.
So if we do the rest of the name which now it has to match 100 percent we see that we get here.
Now that's an important distinction to be aware of.
Now the same thing goes let's say names.
Right.
So let's say we want to check to see our age.
So we do the same thing.
So if age is equal to 26 then we're gonna head and print
age
of age.
Right.
So we should be able to see both of age.
Perfect.
Now what happens if we want to see if something is greater then.
So there's a few options you can do.
You can do greater then.
So if age is greater than 26 if age is less than 26 or greater than or equal to 26 and less than or
equal to 26.
So if we look at is age is the age greater than 26.
No it's not.
So it's not going to print our statement there however is a greater than or equal to.
So does it equal 26 or more.
We now see we see print of age.
So let's see if somebody is 21 or older.
So as my age over 21 one Yes it is.
I am of age and if we do the opposite in age is less than or equal to 21.
They are not of age and however you will not see that printed because I am over 21.
So you can do that with integers and floats as well.
Now boolean is.
So for example here.
So if we do if now there's a few options here you can do it the traditional way where you see male is
equal to true then we print
male.
Right.
And that is completely valid it will work.
However if you're just looking to find if something is true or not or false you can just leave the variable
in there.
So like I said this is the equivalent to me just writing this.
So we go ahead and write it and run it we can see is male and that's an important distinction.
Because it's true.
So if we change this to
change that's a false.
We now see that it's not going to pop up because it is not true.
Turn this back to true.
Now we've gotten through single single type logic now each section of this statement is a logic flow.
So what we can do is what happens if we want to know if the person is.
If their name is Brandon and they're older than 21.
So you could do that with two different if statements and print stuff out separately.
But what if you want to do something when only that combination is together so you can you can link
logic inside of an if statement so you can do this with.
So we we need to know if let's say Brandon
is equal to name and this is the important part that keyword and means that both the first value the
first condition and the second condition which I'm about to write which is age is greater than or equal
to 1 one must be true to make it to this print statement.
We'll just put here for now.
So order.
So if Brandon is equal to the name and the age is greater than or equal to 21 we'll print here as we
can see we can print here.
Now you can even go and if you've even further.
This is a.
And male equal to true.
So we can go we can bring this as as much as we want.
However there's a few options here as well.
Now when you start when you're just doing ends that's completely fine you can do and and and and and
but when you start mixing in or so if I do or male is equal to true or males who are false so that means
if I changed my name to Todd now De Jong and I change my age to 18.
Right.
However male will do.
False.
Sorry.
So we're going to see a reaction.
We still get here.
However the name is not set and the age is less than twenty one.
It's because of that or statement.
So only one statement in this particular case.
So these two have to be true or this has to happen.
So if any of these occur it will print out here now will change us back.
6.
True.
Now if we run that again we're still going to see the same result because we're not because this condition
is what's meant now.
What I want to go over with you guys as well so let's say we wanted to do two different sections but
we wanted an oar.
We wanted a complex set of questions so we'll do the same thing.
So we did Brandon Siegel to name and the age is greater than or equal to 21.
So what we need to do before we can hit this or is we want to surround that condition and another set
of brackets so when this happens it's condensing all of that into one condition.
As far as the application or the code is concerned now each of them.
Without that that surrounding quotes from surrounding our brackets they are considered individual components
now in the language it's treating that as one and then or we put another set and we say or the age is
greater than or equal to say less or equal to twenty one and name is equal to.
Right and here we two so you see and hear and be two.
The reason this occurs is because the name is Brandon and the age is greater than twenty one.
That's what I said or this could happen now this is not the greatest example in the world.
However understand that you can combine and mix match this quite a bit and you can have some pretty
long if statements but depending on what you're needing to have done you can break them down into smaller
sections.
Now what I want to go over is there's also something called else.
So if we did if let's say age is equal to 26 for ages equal to 26 we will print age is twenty six.
Right.
And if we ran that we already know from previous runs that this would print out the age.
However if I changed just twenty five this won't run.
However we can do else at the end and then we can run some other function.
Age is not twenty six.
Now it's important to understand that if this condition is not evaluated to true or any of these conditions
are not evaluated shoe it will look for an else.
So what this means is as if this doesn't work do this instead.
If this is not true perform this operation.
So if we come in here we can see.
Age is not twenty six.
But if we change it to twenty six again it should.
You shouldn't see age is not you should see age is twenty six and you see there it is now.
What if my age is 18.
So we look here me my age is not equal to twenty six.
So that's fine.
But we can change this from an elf to an.
Else if elif which is E L I F then our kids again now we can do.
Age is less than or equal to I don't say for greater than or equal to 18
print adult.
Now what's important here is that we've done we've conjoined to if statements so the only time this
if statement we'll be evaluated is if this one fails so if this is true it just does this and then skips
over the rest of the if statement.
Now it's important understand that in some cases you're going to want an if statement followed by another
completely separate if statement and you don't want it to be an.
Else if the reason being is that maybe you want both to be evaluated in this particular case it'll evaluate
this one if this is true it will never and will never even come down here to look at this.
So you can even on top of that thrown else at the end and just print you know here the three right.
We should never make it to hear v three because we are greater than or equal to 18.
So if we run it again you see a adult which is exactly right here.
However we can see that if we did 17 we would get to hear a three
so that's how if statements work and some of the operations that go around each one of them play around
with this it's it's powerful and there's a lot of variations for this that I couldn't sit here and go
through every single one of them the types of operations you want will be done based on what your application
is wanting to do.