Compound Assignment Expressions in C++ :: Part-A-16

0

COMPOUND ASSIGNMENT EXPRESSIONS

Compound Assignment Expressions

Increment and decrement operators abbreviate certain kinds of assignments. C++ also allows the assignment to be combined with other operators. The general syntax for these combined assignments is

variable op=  expression

where  op  is a binary operator. The effect of the combined assignment is the same as

variable  =  variable  op  expression

For example, the combined assignment

n  +=  8;

has the same effect the simple assignment

n  =  n + 8;

It simply adds  8 to  n.

EXAMPLE : Assignment Operators

                  This shows how to use some of the combined operators:

#include  <iostream.h>

// Tests  combined operators:

main ()

{

int  n  =  44;

    n  += 9;

   cout  <<  n  <<  end1;

   n  -=  5;

   cout  <<  n  <<  end1;

   n  *=  2;

cout  <<  n  <<  end1;

return  0;

}

53

48

96

The statement  n +=  9 adds 9 to  n, the statement  n  -=  5 subtracts 5 from  n, and the statement n  *=  2  multiplies  n  by 2.

Ref. By: JOHN R. HUBBARD, Ph.D. Professor of Mathematics and Computer Science, University of Richmond

——————– Thanks everyone

Share.

About Author

TN365

Leave A Reply

Powered by themekiller.com