Hello YouTubers, I am Parvez and this is our 5th video in C++ Programming Language
In the previous video we have learned about if-else statement or condition in C++ Programming Language
In this video we are going to talk about shorthand if-else or also we can call it ternary operators
in C++ Programming Language. Now if you see this example, we have an if-else in here
So we have just an int number and its value is 10
And in here we are going to check that if our number is greater than or equal to 10, we are going to execute this statement
In the else case, we are going to do this. So if this condition is true, we are going to just execute this code
And if this condition becomes false, we are going to execute the else condition
If you run this, obviously you will receive that the number is greater than 10 because
this condition is true. Now if I run this, you will see that number is greater than 10
You can see number is greater than 10. But now if I change this to 8, now in this case, this condition is false
It will not execute this code but in the case, it will execute the else code in here
Now if you run this, you will see that I am receiving number is less than 10
You can see that number is less than 10. Now the main point in this example is this, that in here we have 4 or 5 lines of codes
in this section for creating an else statement in C++. Now in another way that you can do this in C++, you can use ternary operator or also
we can call it shorthand if else. Now let me just remove this code and we are going to just use our shorthand if else in here
Now we have a number, let me just change this to 10 for right now
So now I am going to use shorthand if else. I am going to just say number
So first I am going to write the condition. So if my number is greater than or equal to 10, so if this is the condition, what I am
going to do, so this is my f. So if this is the condition, I am going to see out that number is greater than 10
So if this is the case, now I am going to just see out that number is greater than 10
So let me just end l, end line. Now in the else case, so you can add else case, let me just add it in here
So in the else case, I am going to say that now if this condition becomes false, what
I want to do? I want to see out, so see out that number is, so in here number is greater than, I am
going to just say number is less than 10. Less than 10
And I am going to just end the line in here. Okay, now if I run this, you will receive that number is greater than 10
You can see that number is greater than 10. So here we are going to check the condition
So first we have written our condition that if this is the condition, we are going to
just say this. If this condition is true, we are going to just do execute this statement
Now in the else case, now in the else case, if this condition is false, we are going to
execute this code. So now if the condition becomes false, for example, I am going to just say 8, and if
I run this, you will see that I am receiving that number is less than 10
So you can see that number is less than 10, because this condition is now false
Now if you make this 12, you will again receive that number is greater than 10, because the
condition is true, and our number is not less than 10. So you can see that number is greater than 10
So now this was a simple example of short NFLs. Now let's just make it a little complex example, and we are going to just do our example based
on the user input. So in here I am going to just create some variables
So first I am going to just create int a, after that I am going to just create int b
And now I am going to just create another variable int c, and I am going to make the
value to 20. After this I need to sum, I need sum of these two values
For this I am going to just create another variable, I call it sum, like this
Okay, now after creating of this, I am going to get the user input, and store the user
input in my these two variables. After getting the two values, I am going to sum these two values, and compare the value
the sum of the value with the c. So now let me just show you practical
First I am going to just say c out, and I am going to tell the user that please enter
first number, like this. Let me just end the line. And after the user input a number, I am going to store the number in my a variable
After that I am going to just create, say to please enter second number
And I am going to just end the line in here. After getting the number, I am going to store the number in my b variable
Okay, so now I have got two values from the user. After this what I want to do, I want to sum these two values, a plus b
I want to add these two values, and store that value in my the sum variable that I have
created at the top. After this, now I am going to just use the shorthand if else, or we can call it ternary operators
And we are going to compare our the sum with the c value that I have already created in
here, and assign the value to 20. So now let me just show you
I am going to just say that if my sum is greater or greater than or equal to c
So if this is the condition, I want to see out that sum is greater than c
Sum is greater than c value
End line. So if this condition is true, I want to say this
Now if this condition becomes false, let me remove this. This is the else condition
Now in the else, I am going to just say that sum is less than c value
I want to see out that sum is less than c value
And I am going to just end line. Okay now let me just describe the code again
And here I have created some variables. The first two variable is for getting the user input
And here I have created another variable called c and I have assigned the value to 20
After that I have created an nsum to sum these two values and store that in my sum
After that I am going to get the user input. So I am going to see out that please enter first number
When the user enters the first number, I am going to store that in the a variable
After that I am going to get the second number and I am going to store the second number
in the b value. After that I am going to plus these two values
Add these two values and store that in my sum variable. And at the end, I am using shorthand if else to check the number
So I am going to check that if this condition is true, it means that my sum is greater than c
So for the c, I have 20. If the sum becomes 21, 22, 23, above 20, I am going to just execute this statement that
sum is greater than c value. In this case, this is true
Now if the sum becomes less than 20, for example 19, 18, 17, 5, 10, this condition becomes
false and I am going to execute the else that sum is less than c value
So now let me just execute the code and you will know what I mean
Okay now it's telling me that please enter first number. So first let me just enter the lower numbers like 3 plus 2
So you can see I am receiving the sum is less than c value. This is because if you see in here, my sum is 20 and I have received 5
Now 5 is less than the c value and in this case, this condition becomes false and it's
executing that sum is less than c. Now let me execute it again
This time I am going to just add, for example 10 and I am going to just add 12
Now you can see it's telling me that sum is greater than c value. It's because now this condition is true and if you see the c value and the c value is
20 and if the c value is 20, now this condition is true
And this condition when it's true, I am going to receive the sum is greater than c value
So like this you can use shorthand if else in C++ programming language
I will add the source code to the video description. 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. Thank you