Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double assignment in single instruction

Tags:

java

This may sound crazy, but i'm curious to know if it is possible to use a single instruction to modify the values of two different variables. For example, suppose i have this code (x and y are int variables):

if(x < 0) {
   y -= x ;
   x = 0;
}

If x is equal to -1, i would obtain the same result by doing the following :

if(x < 0) y -= x++;

Is there a way to generalise the previous result? I tried with the instruction :

if(x < 0) y -=x-=x;

But, while x at the end is equal to 0, y won't be modified. What i'm missing ?

EDIT

I thought (i was wrong probably) it was clear that my question was purely theoretical. I know that this way of doing should be avoided. I was just curious :).

like image 980
Ansharja Avatar asked Jun 09 '26 13:06

Ansharja


1 Answers

Try this:

if(x < 0) x = (y -= x) - y;

It's fine to try tricks like these for a challenge. However, Don't use this type of code in your actual code as it'll create confusion for others.

like image 140
Gurwinder Singh Avatar answered Jun 12 '26 12:06

Gurwinder Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!