In this video, we'll go over c++'s increment and decrement operators.
I'm in the section 8 workspace
in the increment decrement operators project.
This is a pretty simple looking operator, but there's a lot to it, and it can be extremely
confusing if you overuse it. Let's talk about it.
Basically, it's the ++ and -- operator.
You've got two +es side by side or two minuses side by side.
All that means is increment something by 1
or decrement something by 1.
In this example, I'm only going to be using the increment operator,
but everything will apply to the decrement operator.
Okay. So all it does is increment or decrement its operand by 1.
So if you use it with an integer, it increases the integer by 1
or decreases the integer by 1,depending on which one you're using.
If you're using it with a floating point number, same thing. It'll increase it by 1
or decrease it by 1.
And we can also use it with pointers to move pointers along.
We haven't talked about pointers yet,
but we'll use this operator when we talk about pointers in a future section.
There are two variants to this operator. There's a prefix notation
and a postfix notation.
All that means is that the operator
is either before its operand or after its operand.
You can see here on line number 9
that the operand is num
and the operator is to the left of it, so that's prefix notation.
The postfix notation is when it appears to the right of the operand.
Okay. I'll say this a few times. Don't overuse this operator,
and never use it twice for the same variable in the same statement
because the behavior is undefined. You really don't know what you're going to get.
Good, so now let's talk about the operator. Let me scroll down just a little bit.
And I've got some examples here, and I'll walk through these examples
really really carefully and slowly so you can understand what's going on.
For all of the examples, I'll be using these two variables right here.
I'll be using a variable called counter and a variable called result.
They're both integers,
counter will be initialized to 10 and result to 0.
Okay so real straightforward. Counter will be 10, result will be 0.
And we're going to manipulate those variables, and we'll see how this operator really works.
So let's start right here. Let me uncomment example one.
I'll walk through this really slowly. You can see what's happening.
I'm outputting counter.
Okay, well counter is 10, so this you expect it to display a 10.
And that's exactly what happens.
In this case, I'm saying counter = counter +1,
right. So what's counter 10,
I'm adding 1 to it, and then I'm putting it right back into counter. So I'm changing this to an 11.
And this output statement will display an 11.
In this case, I'm using counter++.
That's the increment operator. That's going to increment the variable counter
by 1.
When you use it all alone like that if you've got an operand,
followed by a ++ or a ++ before the operand,
it means exactly the same thing. All it means is increment the operand by 1.
Okay. It'll get a little more confusing in the next few examples,
but let's just get this one under our belt so we can understand what's going on.
So what's happening here is we're incrementing that l value counter by 1.
So we're going to counter incrementing it by 1, now it's 12.
And we're going to display counter here which is 12.
The pre-increment works exactly the same as the post
increment when that thing is all alone on the line, just like I said over here.
So in this case, exactly the same thing will happen.
I'm going to take the 12 and add 1 to it, make it a 13.
Contour will now be 13, and I'll output 13.
All right so in this context, wherever you want to increment something, you could say
counter = counter +1 or you could just say counter++
or you could say ++counter.All three of them mean the same thing.
Later on we'll talk about a different operator,
the + = operator, that does does the same thing as well.
So there's a bunch of different ways that you can achieve the same result.
I'm going to clear this. And remember, we've got 10, 11, 12 and 13 as the output.
So I'll clear this. And let's run this example.
And you can see that counter is
10, 11, 12 and 13, just like what we expected.
All right. So now let's look at a little bit more complicated example. And I'm going to comment this out
and I'll comment this out, not delete it.
And I will uncomment out example two.
Okay. So let's go through this one again really carefully.
What we've got here is, again I've reset counter to 10
and result to 0. All of these examples are going to start in the same place.
Counter will be 10, result will be 0.
So here, I'm going to say I want to output counter. Well, counter is 10, right.
So we expect this to display a 10.
Now we're doing a pre-increment of counter,
and we're storing the result into that variable called result.
So what's happening here is there's really a few things going on here.
If you just see that ++ and just take it out for a second
and the result is something like this, result
= counter,
that's the real assignment statement that's going on here.
The ++ in front of counter means it's prefix notation,
which means that counter will be incremented before it's used.
So in this case,
before happens here. Its uses right here.
But before it's used,we're going to say counter
=, I'll just say counter +1 just to be really explicit.
So this statement, right here, that's the effect of that statement.
I want to increment counter before I use it,
and then I want to use it by assigning it to result.
All right. So let's do those two steps and see what happens.
Counter = counter +1. We just made counter 11.
And then we're going to assign counter to result. Well, counter is 11. So result
will now be 11.
Now I want to display counter. Counter will be 11,
and result will be 11. So this is what I expect is the output. 10,
11 and 11.
Okay. Remember, this is the use,
and this is the effect of that pre-increment.
All right. So let me clear this and run it. Again, we're expecting 10, 11 and 11.
Build and run,
and there it is. 10, 11 and 11, exactly what we expected.
All right. I'll comment out that piece,
and we'll do another example this time with a post increment.
And remember, I'm using the increment but the decrement works exactly the same way
it just decreases the value by 1.
All right. I'll uncomment this example, and let's walk through this one again
one more time.
I'm initializing count or I'm assigning 10 to counter and 0 to result.
And I'm saying print out the counter. Well, it's 10.
We expect the 10 here. That's an easy one.
But now we're doing a post increment.
Notice it's on the right side or it's following counter it's operand.
So this becomes result
= counter
and the post increment simply means that we're going to increment counter
after we use it.
Well, we're using it here. So let's increment it after this time.
So again, I'll be really explicit here and I'll say counter = counter +1.
So that's the effect of that statement.
So let's execute this statement. Result is counter.
Well, counter is 10, so let's put 10 in here.
Now we increment counter by 1. So that becomes 11.
And now we display counter, which is 11
and result which is 10.
So in this case, this is the output we expect. 10,
11 and then 10. All right. Let's give that a shot.
And there we go. 10, 11 and 10.
Let's do a few more examples so I want to be sure that you really understand this.
Let's uncomment example 4 here.
And example 4 just uses the expression that's a little bit more complicated,
not much but a little bit so you can see how this works. In this case,
one more time I've set those to 10 and 10,
and I'm displaying counter. Well, counter is 10. Again, real easy.
And now we've got this statement going on right here.
Result = ++counter +10.
The best way to really look at these things is just
get rid of that guy right now. So just say result
= counter +10.
That's the statement.
And now since the ++ is a pre-increment,
it's on the left side of counter that means that we increment counter before we use it.
We're using it here. So that means that we use, we do this before we execute
that statement.
So again, counter = counter +1. All right.
So let's do this counter is counter +1. This becomes 11.
Then we say result is counter +10. Well, counter is 11.
So 11 + 10 is going to give us a 21, which we're going to store in result.
So result here, we'll get a 21.
Got it. Perfect. Now we're displaying counter. It's 11.
Result is 21. That's what we expect to display.
10, 11 and 21. This may seem a little
contrived and puzzle like and it may be,
but this kind of code is seen out there. And you really need to understand the effect
of the pre-increment and the post increment with these operators.
So 10, 11 and 21 is what we expect. Let's run this.
And 10, 11 and 21 is what we get.
Okay. So let's comment this one out.
And you'll notice what this one the next one example 5
is exactly like example 4 except.
We've got a post increment right here.
Right there, you can see it's the same idea except it's the ++ is
after the operand. Okay.
So again, 10 and 0, we're starting out. We're saying display the counter.
Well, the counter is 10. That's pretty easy.
Here's the statement. Let's just rewrite it
without the increment. So we're going to say result =
again counter +
+10. I'm trying to write nice and big, so hopefully everybody can see this, okay.
And we're doing the increment of counter after we use it. That means
after we use it. We're using it here. So this becomes counter
is counter +1.
All right. So let's execute these two statements. We'll put some semicolons there just to be complete.
So result is counter +10. Well, what's counter. Counter is 10.
So that becomes a 20, and we're storing 20 into result.
And then we're incrementing counter by 1, so counter becomes 11.
Okay. The output says what's counter, 11. What's result, 20.
So in this case 10, 11 and 20 is what we expect.
10, 11 and 20. Let's run this.
And 10, 11 and 20 is exactly what we get.
Okay. So hopefully that clears it up.
And one of the examples if you know if you remember up at the top I mentioned,
I'll scroll up a little bit it says here
never used it twice for the same variable in the same statement.
That could be something like suppose you do
cout,
and you've got something like,
I don't know, let's just say we have an integer I.
You'll see I++, and then you'll do something like ++I
and something like that, you really have no clue what's going to happen
because you're incrementing I two times in the same statement.
And that's not a good idea. Sometimes you'll see people do silly things like
I++ +++I or something
like that just because you can in
c++ but that's a bad thing to do because you're
using it twice on the same variable in the same statement.
And that's that behavior is totally undefined. So your guess is as good as mine
as to what the result is going to be.
Okay. So hopefully, this gives you a little bit of
a little bit of an insight into this operator. a simple operator when you just say
I++ or count++ or ++count. Really, really simple.
But when you start combining it with other operands and other expressions and operators,
it can get pretty complicated. And that's why I say don't overuse this operator.
It's really a great operator to use when we loop,
and we'll do that later and we use pointers. But keep your eye on it, don't overuse it.