In previous lessons, we learned about using the if and else statements to check whether if somebody
is over 120cm or not, and allow them to actually purchase a ticket if they are over a certain height.
Now, in addition to the height, there's another condition that we need to check for, namely their
age.
If somebody is over 18 years old, then they should be paying the adult price, which is let's say $12,
but if they are 18 or under, then they should only be paying $7.
So how can we represent this extra condition that we need to check for in our code?
Well, we could use something called a nested if else statement.
We've seen our if else statements look like this, where it's only got two choices,
if this condition is true, do this, otherwise do that.
But in a nested if statement, once the first condition has passed, we can check for another condition,
and then we can have another if else statement inside this if condition.
In order for this thing to happen, this has to be true,
and this also has to be true. In order for this to happen, this condition has to be true,
but this condition has to be false.
So essentially the computer first looks at the larger picture, which is this first condition, and
decides on whether if it should go into the else block here or if it should go into the nested block
inside the if statement.
So now this is what our flowchart looks like, in the first if statement,
we check whether if their height is over 120cm,
if no, then the if statements all end.
You can't write, you can't buy a ticket.
But if yes, we actually take them to yet another if statement where we check their age.
If their age is 18 or under, then we give them a $7 ticket.
If they're over 18, then they have to pay $12.
So the place where we're going to nest, our if statement is inside here.
Notice how it's indented.
So it's already inside this if block.
And this already has to be true.
Now here we're going to create another if and else statement.
And the condition checks for their age.
So we better ask them for an age.
Let's say age equals, convert the input to an int.
And, "What is your age?"
So now that we've gotten hold of their age, we can see if their age is less than or equal to 18.
Well in this case we're going to give them the $7 ticket.
But else namely if this is not true, if their age is over 18, well, in that case, we're going to
give them the $12 ticket.
So now we have a nested if statement because this if and else statement lives inside this if statement.
So this condition will only be checked if this is already deemed to be true.
Now let's say our situation got a little bit more complex.
The boss comes over, checks our code and says, wait, wait, wait, wait.
There's actually more price tiers than that.
In fact, if you're less than 12 years old, you pay $5.
If you're between 12 and 18, you pay $7.
And if you're over 18, then you pay the full adult price, which is $12.
Now there are three possibilities.
So how do we represent this in our if statement?
Well, we could use something called the elif.
Instead of having a simple if else statement where there's only one condition, if it's true, do this,
otherwise do that.
You can add as many elif conditions as you want. So we can check for condition1,
if that's true, then do A,
but if that's not true, then we can continue and check for condition2.
If condition2 is true, well then we can do B.
And finally, if none of those conditions were true, we can do this final thing.
Our flowchart now looks something like this,
and this is the logic that we're trying to program.
Once we're inside this nested if statement, we're going to check if their age is under 12, in which
case they should pay $5.
If they're between 12 and 18, then they should pay $7.
And finally, if they're over 18, then they should pay $12.
Now, the first thing I'm going to check is if the age is less than 12 under this condition, they should
pay $5.
So let's change that to 5.
Now the next condition should be created using an elif, which stands for else-if.
So, it means,
if this is not true, else if,
can you check if this is true?
Well, in that case then we should do this.
For example, if the age is not less than 12, so they're over 12, then are they under 18?
Well then this condition basically catches everybody who's between 12 and 18.
And finally, if they're not less than or equal to 18 and they're not less than 12, then that's everybody
else who is over 18.
Now remember that we can use as many elif conditions between the if and else as we like.
So I could add another elif that checks whether if the age is less than, say, 22.
Well, in this case, do something else, and then I can keep going with these elif's until I'm done
with all my conditions.
So have a play around with this code.
Write it yourself, see if it makes sense, and then mess around with the elif"s so that it does what
you expect it to do.
And then once you're ready, go ahead and head over to the next lesson where I've got a coding challenge
for you.