WEBVTT

00:00.000 --> 00:13.560
Hi everyone, in this video we are going to learn the if-else statement in Python.

00:13.560 --> 00:16.440
So this is the syntax for if-else statement.

00:16.440 --> 00:21.240
And when the given condition is true, the statements under if-execute, and otherwise

00:21.240 --> 00:24.000
the statements under else will be executed.

00:24.000 --> 00:27.680
And if you have two cases that you can have in your program, and if you want to take

00:27.680 --> 00:32.960
a particular action in either case, then you can use the if-else statement.

00:32.960 --> 00:35.080
Now see a real-time example here.

00:35.080 --> 00:39.680
We have the traffic signal, and the code is if-red-signal-stop.

00:39.680 --> 00:40.680
Else move.

00:40.680 --> 00:42.520
So we have the two cases.

00:42.520 --> 00:44.640
Case one is that the signal is red.

00:44.640 --> 00:46.800
Case two is that the signal is not red.

00:46.800 --> 00:49.840
So if the signal is red, you want to stop.

00:49.840 --> 00:53.360
Otherwise that is if it is not red, you can move.

00:53.360 --> 00:55.320
Now see an example program.

00:55.320 --> 00:58.600
This program checks if the input given is zero or non-zero.

00:58.600 --> 01:03.440
First, we ask the user to enter any number, and suppose the user enters 10 and it is

01:03.440 --> 01:04.840
stored to num.

01:04.840 --> 01:07.320
And now we have the if-else statement here.

01:07.320 --> 01:13.240
First, if num is equal to zero, so condition is that the entered number equal to zero,

01:13.240 --> 01:17.520
and here num equal to 10, which is not equal to zero.

01:17.520 --> 01:23.040
So the condition is failed, and the if part will not work, and the else part will work.

01:23.040 --> 01:24.840
And it will print non-zero.

01:24.840 --> 01:27.720
Now see another case where the user enters zero.

01:27.720 --> 01:32.680
Now the condition num-zero has become true, so the statements under if will be executed

01:32.680 --> 01:34.560
and zero will be printed.

01:34.560 --> 01:37.400
So that's all, and this is how if-else work.

01:37.400 --> 01:40.480
In the next video, we will discuss the nested if-else statement.

