Welcome back.
In this video I want to show you nested if statements.
So there is this concept of nested if statements, which basically means that an if statement is inside
of an if statement.
Now you can go further and say a nested if else if else statement.
So you can have an if and inside of it you can have another if and an else if and then you can have
an else if of the bigger level as well.
So all of that is possible.
And now let's have a look at what this could look like inside of our little example where we asked for
the whole number.
So let's say the user guessed the number right.
And now we want to give him a price.
So or her a price maybe as well.
So here we could check whether they are eligible for the price.
Because maybe this price is only for people who are adults.
So only if you are an adult you could have one something for kids you couldn't have won anything, for
example.
So what you could do is you could have an if statement inside of our if statement here.
Okay.
So we can check if age.
So we're going to create a new variable here.
Let's say int age is equal to zero by default.
And then we can say if age greater equal 18 then ask for their address.
So Console.WriteLine.
Please enter your address so that we can send you the price okay.
So this would be a statement here that we have.
Let me also make sure we have a semicolon at the end.
And then we ask the user for their address.
So console dot read line and whatever they enter there we store inside of another string that we call
address.
Now we're going to cover this scope thing in a second.
Really important.
But before we do that let's look at this here.
So we don't know of the age of the user.
So let's ask the user for their age.
So here I'm going to say console dot read line.
And then we convert that.
Actually let's put it here.
We convert it into an integer.
So we parse it like so we've seen parsing before right.
So we're parsing whatever the user entered.
And we need to tell them please enter your age.
And now we're checking their age.
And if the age is 18 or above then we're asking them for their address.
And we're storing whatever they entered inside of this address variable.
Okay.
So this is just to show you that you can have an if statement inside of another if statement.
So let's run this.
So please enter a whole number.
And I'm going to say zero numbers are equal to.
Please enter your age 18.
Please enter your address so that we can send you the price okay.
Address 1000.
There we are.
So now this address will be stored inside of a variable.
And that's pretty much it.
So now the cool thing is we can also go ahead and have an else block inside of this.
If block inside of this greater block that will only be executed if we guessed the number right.
So if we didn't guess the number right, this will never be executed.
This entire code will never be executed and it will directly jump to numbers aren't equal.
So however, if we did guess the number right and we're above 18, then we will be asked about our address.
If we're not above 18, then we could say something like sorry, you can't get your price due to your
age.
Really sucks, but that's what it is.
Okay, so let's run this again.
Let's guess the number, right?
Well guess what?
We know the number at zero numbers are equal.
Please enter your age.
Let's say um 13.
Sorry, you can't get your price due to your age.
Oh, Sadge.
All right, so now you've seen nested if statements.
And this can go multiple layers.
So you could go further and say yeah I'm going to have another check here.
So if the address is exactly something then do whatever.
You can go as many layers as you want.
However it's not good practice to go super deep in it.
But then there are large language models which are two degree exactly that.
Okay, so that's it for this video.
See you in the next one.