+ 1

What if I do if(x+y=0), is that valid?

15th Dec 2016, 12:31 PM
chorditronix
chorditronix - avatar
6 odpowiedzi
+ 5
I suspect you made a typo and mean: if(x + y == 0) If this is the case then, yes, that is valid. If, OTOH, you did not make a typo and really mean if(x + y = 0) - this is not valid since you are assigning 0 to the result of the sum of x + y. the result of x + y is also an rvalue and you cannot assign to an rvalue, only to an lvalue. It is the equivalent to ask if you can say 4 = 0.
15th Dec 2016, 12:41 PM
Ettienne Gilbert
Ettienne Gilbert - avatar
+ 2
if (x+y == 0 ) is valid
15th Dec 2016, 12:51 PM
sinatb
+ 2
That should work, but give you undesirable result. Instead of comparison, assignment will take place. Assignment operations return a non-zero value, and the condition will evaluate to true.
15th Dec 2016, 12:55 PM
Rishi Anand
Rishi Anand - avatar
+ 2
In "IF" condition we cannot assign the value like if(x + y = 0), this is not valid. Instead of this we need to assign the values to the variable x and y first and after that the sum of both values. See below example:- Int x = 0; Int y = 0; If(x + y == 0) { Console.WriteLine(x + y); } When we are assign the values to the declared or while declaring variable at that time we are using "=". When we are comparing the values with the output at that time we are using "==". I hope it's clear. :)
16th Dec 2016, 3:08 PM
Siddharth Warwatkar
Siddharth Warwatkar - avatar
+ 1
you have to use == that will not work.
15th Dec 2016, 12:33 PM
Uran Kajtazaj
Uran Kajtazaj - avatar
- 1
x+y==0
3rd Mar 2017, 4:53 PM
nazanin