Friday, January 20, 2012

3.1.4. Abbreviated Operations: +=

There's more. We have reduced forms of writing things like "a = a +5", as well. They are:

   a += b ;           is the same as    a = a+b;
   a -= b ;           is the same as    a = a-b;
   a *= b ;           is the same as    a = a*b;
   a /= b ;           is the same as    a = a/b;
   a %= b ;           is the same as    a = a%b;



Suggested exercises:

  • Create a program that uses three variables x, y, z. Their initial values will be 15, -10, 214. It should increase the value of these variables in 12 units, using the abbreviated format. What values do you expect to obtain? Compare it with the results obtained by the program. 
  • What would be the result of the following operations? a=5; b=a+2; b-=3; c=-3; c*=2; ++c; a*=b;

No comments:

Post a Comment