+ 1
What if I do if(x+y=0), is that valid?
6 Answers
+ 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.
+ 2
if (x+y == 0 ) is valid
+ 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.
+ 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. :)
+ 1
you have to use ==
that will not work.
- 1
x+y==0