Change the numeric values of two variables without a temp variable

I found a nice way to change the numeric values of two variables without the use of a temporary variable. It goes like this:

 
// set up two variables to play with
var value1:Number = 10;
var value2:Number = 20;
 
trace("values before");
trace("value1 = " + value1);
trace("value2 = " + value2);
 
// change values
value1 = value1 + value2; // value1 = 30
value2 = value1 - value2; // value2 = 30 - 20 = 10
value1 = value1 - value2; // value1 = 30 - 10 = 20
 
trace("values after");
trace("value1 = " + value1);
trace("value2 = " + value2);

It works with all kind of numbers I can think of both negative and positive integers and floting point numbers

Tags: ,

Leave a Reply