C++: Increment and Decrement operators
In this short tutorial, we're just going to learn a little bit about incrementing and decrementing using some operators built into the core language of C++.
Incrementing
Remember the while loop code we wrote a while back? We wrote number = number + 1 to add one to the variable number that we were using at the time, to step through the while loop a certain number of times. We glossed over the fact that this was called incrementing and that there are a variety of ways to do it. The first I've just covered, the second is using the += operator to do something like number += 1, but there is also a third, even easier way to accomplish this. This way, unlike the others, can only be used to add one to a variable rather than adding a number of your choice. This method is using something called the increment operator. The increment operator is shown in C++ by writing ++ on either the left or right side of a variable – as you might expect, it increments that variable.
So if we were to go back and refactor our while loop code to make it use the increment operator instead of using the current, stretched out method – we would end up with something like this:
int number = 1;
while(number <= 5){
cout << number << endl;
number++;
}
There we go, doesn't that look a bit better! What's great about this is that you can also use the variable while it's being incremented, for example in a simple cout! It's important to note that the operator does different things when put at different sides of a variable – this can be seen most noticeably when using the variable while incrementing as previously described. So firstly let's use the post-increment (the one with the ++ after the variable name), this should use the variable and then increment it. For example the following will output 10, then 11:
int number = 10; cout << number++; << endl; cout << number << endl;
In contrast to this, using the pre-increment (the one with the ++ before the variable name) will increment the variable before using it. So the following should output 11, then 11:
int number = 10; cout << ++number; << endl; cout << number << endl;
A small difference, but incorrect usage in a program could completely screw up a system.
Decrementing
Decrementing is extremely similar to incrementing, however minuses one from the variable instead of adding one. The decrement operator, rather unsurprisingly, is -- and just like the increment operator has pre and post versions (which act just like the increment ones). I'm not really going to go into much depth here, as it should be blindingly obvious how to use the decrement operator if you know how to use the increment operator correctly. Take the following pieces of code for example, try and guess what will output what:
int number = 10; cout << number--; << endl; cout << number << endl;
int number = 10; cout << --number; << endl; cout << number << endl;
If you're stuck, try re-reading the incrementing section or just compiling the code to see the results. Just remember, the post operator always does the operation after usage, and the pre operator always does the operation before usage.
Back to C++

