In this video, we'll learn about c++ conditional operator.
This is a really neat and very useful operator.
The operator is represented by the question mark and the
colon symbols, but they aren't used next to one another.
The conditional operator is a ternary operator, which means that
it operates on three operands.
The first operand is the conditional expression, which
is usually in parentheses.
The conditional expression is evaluated first and it must evaluate
to a Boolean or true false value.
Then we follow the conditional expression with the question mark part
of the conditional operator, followed by expression one, then the colon
part, followed by expression two.
This may sound complicated but it's really super simple.
The conditional expression is evaluated.
If it's true, then the expression returns the value of expression one.
If it's false, then the expression returns the value of expression 2.
You can see it's like an if else construct in a single expression.
While this operator is very handy, it's also easy to abuse.
Best practice is to never nest a conditional operator
expression within another one.
This leads to if else if logic that quickly becomes unreadable
and difficult to manage.
Let's see some examples of the conditional operator.
In this example, we have variables a and b initialized
to 10 and 20, respectively.
We also have score initialized to 92, and result initialized to 0.
The first assignment statement uses the conditional operator
on the right hand side.
First, we evaluate a greater than b.
If a is greater than b, then the value of a is assigned to the result.
If a is not greater than b, then we assign the value of b to result.
That's it.
You see it's very straightforward.
You can see this works exactly like an if else statement,
but it's more concise.
The second example evaluates if a is less than b.
If this is true, then the value of b minus a is assigned to result.
Otherwise, the value of a minus b is assigned to result.
In the third example, we're using the conditional operator
to avoid a divide by 0 error.
First, we check if b is not equal to 0.
If this is true, then we divide a by b and assign the result back to result.
Otherwise, we simply assign 0 to the result.
Notice how we're avoiding a divide by 0 error by making sure that b
is not equal to 0 before we divide.
Finally, we often see the conditional operator used in output statements.
In this case, we check if score is greater than 90.
And if it is, we display excellent.
Otherwise, we display good.
You can see the conditional operator can be very handy and easy to read and
understand when it's used this way.
Let's head over to the IDE and see it in live code.
Okay, so now I'm in CodeLite, I'm in the section 9 workspace in
the conditional operator project.
And what I'd like to do is just go over a couple of real simple examples
of using the conditional operator.
First, we'll write it using a regular if else statement, then we'll write
it using the conditional operator.
Okay, so let's get started.
Let's create a variable here, we'll call it num, and this is what the
user is going to enter an integer into we're going to save it into here.
We'll prompt the user to enter an integer, and we'll
read that integer into num.
Okay, simple as that now.
What we want to know is we want to know whether the integer they
typed in is even or it's odd.
So let's use a simple if else statement.
We'll say if the number they entered mod 2 is equal to 0.
Okay, In this case, we're dividing by 2, and we're looking at the remainder.
So obviously, if it's an even number, the remainder will be 0.
That's what we're testing.
So in this case, we'll just say cout.
Let's say num is even.
And in the else case, obviously, it has to be odd.
Okay, so that's real simple logic here.
Let's test this out.
Yeah, I forgot the l in in the endline.
Okay, so an even integer, let's say we want to type in 6.
6 is even.
That's correct.
We'll run it one more time with 7.
7 is odd simple as that.
Now let's rewrite this if else statement just using a simple
conditional operator, that's inside us of the output statement.
So we'll say cout, and we'll say same thing num.
Now here's where we'll put the conditional operator.
Num is and right here is where we'll put the conditional operator.
Okay, so in this case, we need to wrap this up, and we'll put the
conditional operator right in here.
So we're going to test to see if num mod 2 is equal to 0.
Okay, if that is the case, then I've got my question mark, which
gives me the true side and I'll just say even and the colon odd.
That's it.
And we'll end this with a new line as well.
And let's run this and see what it looks like.
So we'll put in 2, and we can see both of those statements
fire because we've got the regular if else statement going.
And let's try to run it again with 3.
There we go.
Now let's see what's really going on here.
In this case, again we are testing to see if the number is even.
If this is true, then this piece is the one that's going to remember.
We've got an output operator here an insertion operator.
So this is going to be replaced this whole thing right here, will be
replaced with even, the string even.
If it's false, it'll be replaced with odds.
So we've got the same thing going on here, right.
Num is even.
Numb is odd, except that it's all in one statement like that.
This may you may be wondering why in the world would you do this.
Well, a lot of times it's really handy to not have this sort of
logic here but rather put something right in a print statement.
So suppose we've got some sort of output statement, and we've
got some kind of looping thing going on and every 10th time
I need to do something, I need to print something different.
It's real handy to use this operator in that context because
it makes your code a lot cleaner.
And if you use it properly, like this, it's really easy to understand.
Okay, let's do another example, and I'll comment this out here.
And let's just do a real simple example it's just
going to compare two integers.
Okay, so we need those two integers here.
We'll call them num1 and num2.
And we'll tell the user to enter to integers separated by a space.
Okay, and we'll read into those two integers, num1 and num2.
All right, so in this case, I want to display the largest and the
smallest of those two integers.
But I want to do it using the conditional operator.
So the first thing we need to do is we need to see if the
integers are the same, right.
If they are the same, then we'll just say they're the same.
There is no largest or smallest.
So let's do that logic first.
So we'll say if num1 is not equal to num2, in this case,
we want to say, okay, one's bigger than the other which is
biggest which is smallest, right.
The else case means that they're the same.
So let's handle that case first.
So we'll just say cout and we'll just say something like
the numbers are the same.
And I'll put a new line at the end.
Okay, now this is the logic we need to figure out now.
We need to figure out what's the largest and what's the smallest.
I could write an if else statement in here a couple of times, right.
But in this case, I'm just going to do it right in line
with an output statement.
So let's do the first one.
We'll say cout.
And what I want to do here is I'll just say the largest because that's
what we're going to look for first.
And now we'll use the conditional operator.
So again, I'll put the parens in there and I'll put an end line at
the end might as well do it now.
Now the logic in here says, okay let's test to see if num1
is greater than num2, right.
If it is, I use the conditional operator and display num1.
If it's not, I display num2.
That's it.
Simple as that.
And over here, we'll say cout smallest and same sort of logic.
We'll come right into these parens right in here.
And what we'll do is we'll test.
In this case, num1 less than num2.
And if it is, num1.
Otherwise, num2.
Okay, that's it.
Let's run this.
And it says no.
It should have been num1.
Enter two integers separated by a space.
Let's do the equal case first.
The numbers are the same, that's good.
Let's do a 5 and 10.
Largest is 10, smallest is 5.
And now let's do a 10 and a 5.
Largest is 10, smallest is still 5.
So we're good.
So here's a real simple example of using the conditional operator.
We'll use the conditional operator in a couple places
as the course progresses.
And typically, it's going to be inside the loop as we're looping
and we want to do something every fifth time, tenth time.
Let's say you every fifth time you want to print a new
line or something like that.
Then this makes a lot of sense to do it inside an
output statement like this.