Wednesday, May 20, 2009

Assignment Operators

compound assignment
When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators:


expression is equivalent to
value += increase; ----- value = value + increase;
a -= 5; --------- a = a - 5;
a /= b; -------- a = a / b;
price *= units + 1; -------- price = price * (units + 1);



and the same for all other operators. For example:

compound assignment operators in

#include "iostream"

using namespace std;

int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout <<>
return 0;
}

No comments:

Post a Comment