Increment/Decrement operators in C++
operators in C++

"Don't let what you cannot do interfere with what you can do." - John R. Wooden
In this article, I will be talking about the increment/decrement operator in C++. This is a type of operator that may get a little bit confusing for beginners in C++, especially postfix/prefix distinction which we will soon see. Let's hop in and make this easy for you.
C++ includes two useful operators not generally found in other programming languages (except C). These are the :
Increment operator ++
Decrement operator --
The C++ name itself is influenced by the increment operator ++. In simple words, these operators add/subtract 1 from their operands. i.e
a = a + 1; is same as ++a; or a++;
a = a - 1; is same as --a; or a--;
However, both come in two varieties: they may either precede or follow the operand.
The Prefix version comes before the operand (as in ++a or --a)
The Postfix version comes after the operand (as in a++ or a--)
The two versions have the same effect on the operand, but they differ when they take place in an expression.
Working with the Prefix version
When an increment or decrement operator precedes its operand, C++ performs the increment or decrement operation before using the value of the operand. It follows the change-then-use rule i.e., they first change the value of their operand, then use the new value in evaluating the expression.
For example, consider below assuming the initial values of sum and count are 0 and 10 respectively.
sum = sum + (++count);

The expression below will also similarly take place, assuming the initial values of P and N are 4 and 8 respectively.
P = P * --N;

Working with the Postfix version
When an increment or decrement operator follows its operand, C++ first uses the value of the operand in evaluating the expression before incrementing or decrementing the operand's value. It follows the use-then-change rule i.e., they first use the value of their operand in evaluating the expression and then change the operand's value.
For example, the expression below will take place in the following fashion assuming the initial values of sum and count are 0 and 10 respectively.
sum = sum + count++ ;

The expression below will take place similarly, assuming the initial values of P and N are 4 and 8 respectively.
P = P * N--

Please note that the overall effect on the operand's value, in both cases of prefix and postfix operators, is the same (the final value of ++count & count++ and --N & N-- are the same). However, they differ in when they take place in the evaluation of an expression.
Now that we have understood the basics, we will see some examples.
Example 1
Predict the output of the following code snippet:
int n = 7;
cout<< "n++ =" << n++<<endl; //first-use-then-increment rule.
cout<<"n= " << n <<"\n"; //incremented value is shown here
The output of the above code will be:
n++ = 7
n=8
Example 2
Predict the output of the following code snippet:
int n = 7;
cout<< "++n =" << ++n <<endl;
cout<<"n =" << n <<"\n"
The output of the above code will be:
++n =8
n = 8
Hope this article has cleared up the confusion on prefix and postfix increment/decrement operators.
Follow this space for more articles on C++



