So in this video we're going to look at else if which basically allows us to run code whenever another
condition is met if the first one isn't met.
So how does that look like.
So we have this if H greater 18 or greater equal 18.
Now let's say we have another condition in here that we want to be considered and executed, depending
on whether this is true or not, let's say the age is greater than six.
At least that's the age in Germany where kids usually go to school.
So if the age is greater than six, then we say go to school.
And then in the else block we say go party in kindergarten and kindergarten, there we are.
So now, depending on the age, we're going to do the right thing.
So let's run it again.
And we say it says go party in school as it did before.
But now we even have another variant.
So let's say the age is three.
Then we're going to say go party in kindergarten.
So why is that?
Well, the thing is we now have an if else if and else block.
So it always goes from top to bottom checking whether one of them is true.
And once one of them is true, all of the others will not be executed.
So this means that if this block is executed, this else if and this else will never be executed.
So the code will directly jump to this point right here, okay.
And we can test that.
We can test that by adding a beautiful little breakpoint.
And in order to test that let's also set the age a little higher because otherwise it won't work properly.
So there we are.
It's checking whether the age is greater than 18.
Let's go and step over.
We go into the next line okay.
You see, this code is executed because age is in fact greater 18 greater equal 18.
Then we go to the curly brackets and in the next moment we will jump directly down here to line 1617.
So there you are.
We directly jump to line 17.
Because if one of the statements is true, all of the following statements inside the same if else if
else block will be disregarded, will be passed and basically not executed.
So that's really important to understand.
In order to demonstrate this even further, let's say we say age greater equal six okay.
So if you look at the age now 22, you could say, yeah, the age is greater than 18 and is also greater
than six.
So this should also be executed.
Right.
But no it won't.
Let's run it and see if we go to the club and to the school.
We don't.
So we only go to the club.
And that is because this part here will not be executed, because this part here has been executed.
Which is why this statement here is useless.
It doesn't do anything because there is no way the code will jump into this code here.
Because if this here is true, this also is true.
And because then this will be executed.
This will never be executed as well as this.
Neither.
Okay, good.
So now that we have gone over this for quite a bit, let's try this again.
But this time we modify it a little bit.
So we're going to have a bool here is with parents.
And we're going to set that to false for example.
And what we're going to check is if the age is above let's say 13 and we are with our parents so is
with parents.
Then we can go into the club and party with the parents.
Okay.
So it's like, uh, I don't know, like a teenager club or something like that, but they need to come
with the parents, otherwise they can't get in there.
So then otherwise let's say the age is above 18, then they don't need to be with parents.
So we don't need to add this end block here as well.
So then we can also say go party in the club and else we're going to say go party in the kindergarten.
Okay.
So now because the age is 22 we don't need to be with the parents or not.
So we can just say, okay, go party, have fun, whatever.
But then if the age is 16, then it will matter whether we are with the parents or not.
So let me run this and it says go party in kindergarten, even though you see the age is above this
level.
But it's not that we are with parents, if we are 22, we can still party in the club.
So it doesn't matter whether we are with our parents or not.
It says go party in the club, but then if we say we are with our parents, then we can also be 16.
So let me say we go, well, we're 16 and it says go party in the club.
And maybe we should add go party in the club with your parents in order to see a difference in the output.
So.
So now let's run it again.
And now it's going to say go party in the club with your parents.
Okay.
So this was an example that I would like to extend in the next video where we're going to get user input
and check whether people are with parents or not.
And this is actually going to be a little exercise that I would highly recommend you try and do yourself,
where you basically ask the user two things.
First of all, their age and second of all, whether they are with parents or not.
Okay.
And we're going to use if and else if statements for that.
So see you in the next video.