Hello YouTubers, I'm Parvez and this is our fourth video in C++ programming language
In this video we are going to talk about using conditional statements in C++
Especially we are going to talk about the if-else conditional statement in C++
So using conditional statements we can execute some section of the code according to a condition
So particularly in this video we are going to talk about the if-else condition
Now let me just show you the practical example and you will be know about the concept of this
So let me just remove this code from my previous video. Now for example in here I have a boolean variable so let me just call it condition
So first let me just write the syntax of the if-else if condition
For example now so in here this is the syntax of if condition
So for example in here I am checking that if my this condition is true I am going to
execute this statement. For example whatever you want, you want to display something or whatever you want
According to this condition I am going to execute this code in here
If the condition become false this statement will become false and we are not receiving anything
So now let me just show you practical. For example in here I have a boolean condition so let me just copy this and paste it in here
So by default it is true. Let me just make it true
Now if you see in here I am saying that if my condition is true so you can see that my
condition is true. If the condition is true I am going to just see out and I am going to just say that condition
is true and let me just make it like this condition is true and end line
Okay now if the condition become false right now we are not going to talk about the condition
become false but in the later part of the video we will talk about that
Now in here I am going to check that if the condition is true I am going to execute this code
Now if you see my condition is true and if I run this program
now you can see based on my condition I am receiving this statement condition is true
because in here my condition is true and if the condition is true this code will be executed
Now if I change this to false now it is the place that my condition become false and it
will not execute this code. So now if I run this now you can see that I am receiving nothing because this condition
is right now not true and it is false and it will not execute this
So now in here we are going to talk about the else condition
Now in here I am going to just add else okay like this okay now in here I am going to say
that if the condition become false do this statement in here and I am going to just say
condition is false. Okay now in this code first I am going to check this condition okay so if this condition
is true I am going to execute this code. Now if this condition become false I am going to say that execute this statement like this
Now if I run this so let me just run this okay now you can see that first it will check
this condition now this condition is false after that it will check this condition
Now this condition become true and it is telling me that condition is false
First check this condition and after if it is true it will execute this code
Now if it become false it will execute this code. So like this you can use if else condition
Now let me just create a second example let me just remove this
Okay now in here I am going to just create a int number and let me just give it 12
Now I am going to check okay so in here I am going to say a concept that also you can
check multiple condition. So this example is basically for the multiple conditions
So in here I am going to check that if my number is for example okay so if my number
is I am going to check to or let me just okay if my number is less than 10 I am going
to just say cout and I am going to say that for example number is less than 10
So number is less than 12
And let me just this is cout not count. Okay let me change this to 12
We have 12 in here. Okay so in here first I am going to check that if my number is less than 12 I am going
to say that number is less than 12. Now after this I am going to just check another condition using else if
So in else if I am going to say that if my number is equal to 12 I am going to cout that
let me just copy this I am going to say that number is equal to 12
And the else condition I am going to just say cout that number is greater than 12
Greater than 12. So in here first I have created a variable let me just describe the code
So first I have created a int number variable after that I am going to check this that if
my number is less than 12 I am going to just do this statement and say that number is less
than 12. And if I if else if the number is 12 I am going to say that the number is 12
And at the else condition I am going to just say that number is greater than 12
Now if I run this code. Okay so right now you can see it is telling me that the number is equal to 12
Because if you check here we have the number to 12. Now if I change this to 10
Because let me just describe because first I checked this condition now this condition
becomes false after that it comes to this condition and now this condition is true and
it will just return this statement this cout statement. Now if I change this to 10 I should receive the first one because it will check the first
one and because the condition became true and it will print this statement
So let me just run this. Okay now you can see that number is less than 12 because we have 10 in here and if I change
this to 13 and if I run this I should receive this last else statement
You can see that number is greater than 12 because we have 13 in here
It first checked this condition so now in here the condition became false after that
checked this condition this also became false and at the end we have else and it checked
this condition and now this condition became true. So like this you can use if else and also else if condition
So now let me just show you another example now we are going to just make a little complex
example and we are going to just do our example based on the user input
So now let me just remove these codes from here
Okay now this example is based on the user input so I am taking the input from the user
two numbers and I will check the condition of that two numbers
So in here first I am going to just create int a and also int b like this okay
Now I am going to just cout and I am going to just say please enter a number and I am
going to just inline and after that I am going to get the number from the user get the input
from the user and store that in my int a variable. Now let me just do this copy this because I want the second number also so please let
me just say this first number and just say this second number and I want to store that
in my b variable. Now I am going to just check the condition my
Okay now in here I am going to say that if a is greater than b I am going to just say
cout and I want to say that a value is greater than b, than b value or just say than b and
end line okay. So now in the else if so else if I am going to just say if my a is equal to b so if this
was I am going to just say let me just copy this cout and I am going to just say that
a and b values are equal like this okay and in the else condition
Now in this condition I am going to just say that a value is so let me just copy this a
a value is smaller than b smaller than b. So just we have a simple program so I have created just two variables after that I am
going to just get the input from the user and the first number I am going to just store
that in the a variable and the second number I am going to store that in the b variable
After that I am going to check so if my a is greater than b I am going to just say that
a value is greater than b and if it is equal I am going to just say a and b values are
equal and in the else condition I am going to just say that a value is smaller than b
So now let me just run this. Okay now I am receiving please enter number so first let me just enter 10 please enter
second number for example 5. Now you can see that a value is greater than b because in a we have 10 and in the b we
have 5. Now let me just run it again. Okay now let me just enter 5 in here and 10 in here so 10 and you can see that a value
is smaller than b because in a we have 5 and in b we have 10
Now let me just run it again and if I say 5 and also 5 you can see a and b values equal
So like this you can use if else condition in C++. If you have any question please let me know and for the further videos please subscribe
to my channel, like the video and also share the video