the small piece of code in the topic title is "wrong": what I mean is that to compare x to y and do something if they are the same the code should read as follows ( 2 equals signs)
if(x==y){do something;}
but the following code with one equals sign (assign x the value of y) compiles as if there's nothing wrong.
if(x=y){do something;}
I'm assuming what will happen is that if x is successfully assigned the value of y then a 1 / true will be returned that the if statement can act upon.
am I anywhere near correct or am I barking up the wrong tree?
There is nothing wrong with it and no reason it should not compile.
The assignment is made and the result of the assignment either zero or non-zero is used decide the outcome of the if.
x==y compares the value of x to y
x=y put the value of y into x. The reason this compiles is because the compiler doesn't know what you are trying to do with x and y. You could also do if(y=(x==y)) and it will still compile.
No, an assignment will never fail - it will only 'fail' at compile time.
It just returns the value of the assignment. If the value is 0 it is logical false, otherwise it is logical true.
As Keith said, it has nothing to do with the assignment. Consider the following two variations:
int i = 10;
int b;
if (b = 10) {
// ...whatever...
}
The expression above resolves to:
if (b = 10)
if (10)
Since the if expression is non-zero, it is treated as logic True. Now try the following expression:
int i = 0;
int b;
if (b = 0) {
// ...whatever...
}
Now the expression is:
if (b = 0) {
if (0) {
which is viewed as logic False. The conclusion: using the assignment operator (usually by mistake) in an if test is always logic True, unless the assignment results in zero.
Consider the = operator as a function. Besides assigning the actual value, the function also returns the value that was assigned.
You can also say z=x=y;
When evaluating this line it will first assign the value of y to x. The return value of this action is y, it will then use this when evaluating the z=something bit. so z will also be assigned y.
x=y is not a statement; it is an expression, whose value is the value that was assigned. If you write
x=y;
the it assigns the value of y to x, then having no further use for the value, ignores it. Note that
x+y;
is ALSO a valid statement; it computed the sum of x and y and then ignores whatever the result is.
However, this allows us to write statements like
x++;
or
++x;
which have a side effect on x, but the resulting value of the expression is ignored.
There are those of us who consider the assignment expression to be a major design flaw in the language. We tend to instruct our compilers to give a fatal compilation error if one is found. I don't know if g++ (gcc) has an option for this, but you might check the #pragma section of the g++ documentation.
Note that there are external checking programs, such as "lint", which can be instructed to treat this as a reportable error.
There is an urban myth that if you write
if(!(result = somefunction())
that you will get better code than if you write
result = somefunction();
if(!result)
but that is rarely, if ever, true today. It was true in the PDP-11 in 1975, which could only execute 300,000 instructions per second, but has little relevance today when even an Arduino can execute 16,000,000 instructions per second. And compilers like g++ produce identical code for both cases.
joe
int x = 5; //put a value of 5 in x
int y = 10; //put a value of 10 in y
....do your bit of code inc the do something, but don't change the values of x or y
RMurphy195:
Sets x = y then takes the true path because x==y!
Easy to confirm by experiment e.g try
int x = 5; //put a value of 5 in x
int y = 10; //put a value of 10 in y
....do your bit of code inc the do something, but don't change the values of x or y
Serial.println(x);
Serial.println(y);
and I'm guessing you'll find out what happens!
Not quite. It's NOT the evaluation of (x==y) that is assessed. It's the VALUE itself.
Try
int x=5;
int y=0;
if(x=y)
{//try do something here and it won't happen
//even though x is now equal to Y, the value involved was 0
//which equates to false
}