Unit#1 Basic Elements of a C++ Program
Return to Unit

Assignment Operators

 

 



The above picture illustrates the effect of the assignment operator:  Take the data from memory location a, take the data from location b, add the two numbers and then store the sum into the location c.

Note that there is nothing here saying that c is equal to the sum of  a and b.  That is why we call this operator assignment operator.  Later on we will see that there is an equality operator which is shown as  = = ( two equal signs next to each other).

C++   programmers use some additional  assignment operators for brevity.  You don't have to use these additional operators, but you must be aware of their existence and meaning.  They are summarized in the table below:

Operator             

Example

Equivalent to

=

a = a + b

Add the values stored at memory location a and memory location b and store the result again in location a

+=

a += b

a = a + b

-=

a -= b

a = a - b

*=

a *= b

a = a * b          (  a star denotes multiplication )

/=

a /= b

a = a / b           ( a slash denotes division )

%=

a %= b

a = a % b        ( %  denotes the remainder of the division of the                         two numbers, 8 % 3 = 2  )

 a += (45.36 + b / 3.5 )     which translates to        a = a + 45.36 + b / 3.5

For a discussion of the various arithmetic operations and the precedence of their execution check your book and also the next subtopic of  Unit 1.