Arithmetic Assignment Operators in C++

C++ offers several ways to shorten and clarify your code. One of these is the arithmetic assignment operators. This operator helps to give C++ listings their distinctive appearance. The following kind of statement is common in most languages.

total = total + item; // adds “item” to “total”

In this situation you add something to an existing value (or you perform some other arithmetic operation on it). But the syntax of this statement offends those for whom brevity is important, because the name total appears twice. So C++ offers a condensed approach: the arithmetic assignment operator, which combines an arithmetic operator and an assignment operator and eliminates the repeated operand. Here’s a statement that has exactly the same effect as the preceding one.

total += item; // adds “item” to “total”

Figure emphasizes the equivalence of the two forms. There are arithmetic assignment operators corresponding to all the arithmetic operations: +=, -=, *=, /=, and %= (and some other operators as well). The following example shows the arithmetic assignment operators in use:

// assign.cpp
// demonstrates arithmetic assignment operators
#include <iostream>
using namespace std;

int main()
   {
    int ans = 27;

    ans += 10;          //same as: ans = ans + 10;
    cout << ans << ", ",
    ans -= 7;          //same as: ans - 7;
    cout << ans << ", ",
    ans *= 2;          //same as: ans * 2;
    cout << ans << ", ";
    ans /= 3;          //same as: ans / 3;
    cout << ans << ", ",
    ans %= 3;          //same as: ans % 3;
    cout << ans << end1;
    return 0;
    }
Here’s the output from this program:
37, 30, 60, 20, 2

You don’t need to use arithmetic assignment operators in your code, but they are a common feature of the language; they’ll appear in numerous examples in this book.

Increment Arithmetic Assignment Operators

Here’s an even more specialized operator. You often need to add 1 to the value of an existing variable. You can do this the “normal” way: count = count + 1; // adds 1 to “count” Or you can use an arithmetic assignment operator: count += 1; // adds 1 to “count” But there’s an even more condensed approach: ++count; // adds 1 to “count” The ++ operator increments (adds 1 to) its argument.

Prefix and Postfix

As if this weren’t weird enough, the increment operator can be used in two ways: as a prefix, meaning that the operator precedes the variable; and as a postfix, meaning that the operator follows the variable. What’s the difference? Often a variable is incremented within a statement that performs some other operation on it. For example

totalWeight = avgWeight * ++count;

The question here is this: Is the multiplication performed before or after count is incremented? In this case count is incremented first. How do we know that? Because prefix notation is used: ++count. If we had used postfix notation, count++, the multiplication would have been performed first, then count would have been incremented.

Here’s an example that shows both the prefix and postfix versions of the increment operator:

// increm.cpp
// demonstrates the increment operator 
#include <iostream>
using namespace std;

int main()
    {
    int count = 10;
    cout << “count=” << count << endl; //displays 10
    cout << “count=” << ++count << endl; //displays 11 (prefix)
    cout << “count=” << count << endl; //displays 11
    cout << “count=” << count++ << endl; //displays 11 (posfix)
    cout << “count=” << count << endl; //displays 12
    return 0;
    }
Here's the program's output:
    count=10
    count=11
    count=11
    count=11
    count=12

The first time count is incremented, the prefix ++ operator is used. This causes the increment to happen at the beginning of the statement evaluation, before the output operation has been carried out. When the value of the expression ++count is displayed, it has already been incremented, and << sees the value 11. The second time count is incremented, the postfix ++ operator is used. When the expression count++ is displayed, it retains its unincremented value of 11.

Following the completion of this statement, the increment takes effect, so that in the last statement of the program we see that count has acquired the value 12.

The Decrement (–) Operator

The decrement operator, –, behaves very much like the increment operator, except that it sub-tracts 1 from its operand. It too can be used in both prefix and postfix forms.

Read More Topics
C Programming Languages
C++ Programming Basics
Characteristics of OOP
Common Programming Errors in C

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply