# Increment and Decrement Operators

Increment means to increase a value by one. Conversely, decrement means to decrease a value by one. C++ has an increment operator that you can use to increase a value by one and a decrement operator that you can use to decrease a value by one. This section will show you how to use both, something that will be useful in the next section on the for loop, which uses increment and decrement operators.

### The Increment Operator

In the following program, the statement num += 1 increases the value of the integer variable num, which was initialized to the value 2, by 1, so the output will be 3.

` #include <iostream> using namespace std; int main(void) {  int num = 2;  num += 1;  cout << num;  return 0; } `

Another way to accomplish the same result is by using the increment operator, ++. The increment operator is unarythat is, it operates on one operand. That operand generally is a whole number variable, such as an int. We can use the increment operator simply by changing the program we just ran by replacing the statement num += 1 with the statement num++ :

` #include <iostream> using namespace std; int main(void) {  int num = 2;  num++;  cout << num;  return 0; } `

The same output would occur if you substituted the statement ++num for num++ :

` #include <iostream> using namespace std; int main(void) {  int num = 2;  ++num;  cout << num;  return 0; } `

Placing the ++ before the variable num is called prefix incrementingthe pre indicating that the increment operator precedes its operand. Placing the ++ before the variable num is called postfix incrementingthe post indicating that the increment operator follows its operand.

In this example, it makes no difference to the output of the program whether you use prefix or postfix incrementing. The reason is that the statement ++num has only one operator; the same is true of the statement num++ . However, there is a difference between prefix and postfix incrementing when the statement has more than one operator. This is discussed later in this chapter in the section The Difference Between Prefix and Postfix.

### The Decrement Operator

In the following program, the statement num = 1 decreases the value of the integer variable num, which was initialized to the value 2, by 1, so the output will be 1.

` #include <iostream> using namespace std; int main(void) {  int num = 2;  num -= 1;  cout << num;  return 0; } `

Another way to accomplish the same result is by using the decrement operator, --. The decrement operator, like the increment operator, is unary, operating on one operand which generally is a whole number variable, such as an int. We can use the decrement operator simply by changing the program we just ran and replacing the statement num = 1 with the statement num-- :

` #include <iostream> using namespace std; int main(void) {  int num = 2;  num--;  cout << num;  return 0; } `

The same output would occur if you substituted the statement --num for num-- :

` #include <iostream> using namespace std; int main(void) {  int num = 2;  --num;  cout << num;  return 0; } `

As with the increment operator, placing the -- before the variable num is called prefix decrementing , while placing the -- after the variable num is called postfix decrementing.

Also, as with the example of the increment operator, in this example it makes no difference to the output of the program whether you use prefix or postfix decrementing because the statement --num (or num-- ) has only one operator. However, as discussed in the next section, The Difference Between Prefix and Postfix, there is a difference between prefix and postfix decrementing (or incrementing) when the statement has more than one operator.

### The Difference Between Prefix and Postfix

The following program is similar to the previous program that illustrated the increment operator.

` #include <iostream> using namespace std; int main(void) {  int num = 2;  cout << num++;  return 0; } `

However, instead of two statements:

` num++;  cout << num; `

This program uses one statement:

` cout << num++; `

There are two operators in this cout statement: the increment operator ++ and the stream insertion operator <<. The issue is one of precedence; which operation occurs first.

The output of this program is 2. The reason is that when an increment or decrement operator is postfix, that operation is the last to occur. Therefore, the output of num occurs first while the variables value is still 2, and then the value of num is incremented from 2 to 3.

Now, change the line:

` cout << num++; `

to the line:

` cout << ++num; `

so the program now reads

` #include <iostream> using namespace std; int main(void) {  int num = 2;  cout << ++num;  return 0; } `

This time, the output of this program is 3 instead of 2. The reason is that when an increment or decrement operator is prefix, that operation is the first to occur. Therefore, num first is incremented from 2 to 3 before the value of num is outputted.

The distinction between prefix and postfix also arises frequently with arithmetic operators. In the following code fragment, the value of result is 15, not 18, because op 2 is incremented from 5 to 6 after the multiplication and assignment occurs.

` int op1 = 3, op2 = 5, result; result = op1 * op2++; `

If prefix incrementing instead were used, as in the following code fragment, the value of result is 18, not 15, because op2 is incremented from 5 to 6 before the multiplication and assignment occurs.

` int op1 = 3, op2 = 5, result; result = op1 * ++op2; `

The distinction between prefix and postfix arises as well with relational operators. In the following code fragment, the output is 1 (the integer representation of Boolean true) because the integer variable num is compared to 5 for equality before num is incremented from 5 to 6.

` int num = 5; cout << (num++ == 5); `

If prefix incrementing instead were used, as in the following code fragment, then the output would be 0 (the integer representation of Boolean false) because the integer variable num is incremented from 5 to 6 before it is compared to 5 for equality.

` int num = 5; cout << (++num == 5); `

The increment and decrement operators generally are not used by themselves , but in conjunction with loops . The next section covers one type of loop: the for loop. C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148