0:00
Hey everyone, welcome back to this
0:02
channel and today's episode we are
0:04
diving into something super interesting
0:06
which is nested if statements in Python.
0:08
So far we have looked at simple if else
0:11
and even if l if else structures but
0:15
what if you need to make a decision
0:19
inside another decision?
0:22
That's where nested if statements come
0:23
in. It's basically putting one if
0:26
statement inside another. Think of it
0:29
like Russian dolls. Like you open one
0:32
and there's another inside. Same logic,
0:34
one inside the other.
0:37
So let me show you the syntax real
0:40
quick. So here is the syntax. If
0:42
condition is true, then this block, then
0:45
if condition, if condition, and then
0:48
this else condition, and this else
0:51
condition. I hope you're getting it. It
0:54
looks pretty familiar, right? like it's
0:56
just if else blocks but one inside the
0:59
other. Now let's understand this with an
1:06
So here is a small Python program but I
1:08
would not suggest you to just look into
1:11
it. I want you to code with me so that
1:13
you can understand when you do it
1:17
So first let's just give a number equal
1:24
greater than or equal to zero.
1:31
Okay. Now you have to check whether the
1:33
number is zero or not. So if number
1:40
then you just have to print
2:08
like it is related to this. If print
2:23
Okay. So what's going on here? First the
2:27
outer if checks means this one. It
2:31
checks whether the number is greater
2:32
than or equal to zero or not. If this is
2:35
true then we move into the inner if
2:40
which checks number is exactly zero or
2:44
not. If it's not zero then it will just
2:47
say the number is positive.
2:51
But if this if this condition if this
2:55
entire condition is false then simply it
2:57
will go to the else block and this is
2:59
where it will print the number is
3:04
So we skip this entire inner part once
3:08
if this condition is false.
3:14
So then it will just print the number is
3:16
negative. So in our case the number is
3:19
five. So if five is greater than or
3:22
equal to 0 then it will check it's true.
3:25
Then it will check whether five is equal
3:28
to zero or not. No it is not zero. Then
3:31
it will go to the else part. Then it
3:34
will print the number is positive.
3:38
You can run this and you can check it
3:43
So the number is positive.
3:47
So nested if statements are super
3:50
helpful when you want to perform layer
3:53
decision making. Like just make sure
3:55
that you're following the indentation
3:58
because that's how Python knows which
4:00
code belongs where. See the indentation
4:03
of outer if else and the indentation of
4:10
That's pretty simple. Nested if is
4:13
nothing but the if statement one inside
4:16
the another. That's the only difference.
4:19
If you understood what is if, else and
4:22
what is if, l, if and else like if you
4:24
have completed those two videos then you
4:26
will understand the importance of nested
4:29
if as well. It's just a simple one but
4:32
very powerful once you get the hang of
4:34
it. In the next video we will dive into
4:37
some more cool exercises to practice
4:39
everything whatever we have learned
4:42
especially with these if else
4:43
statements. So, make sure to try out
4:46
this code on your own and let me know in
4:48
the comments if you want me to review
4:50
your solution or help debug it. And if
4:53
you found this video helpful, don't
4:55
forget hit like, subscribe, and turn on
4:57
the bell icon so you never miss an
4:59
episode. See you in the next one.