Is the following legal in the GNU Arduino C. The compiler doesn't seem to like it.
If(temp=-1 || temp1=-1){
}
I'm using the logical OR.
Suggest a simple solution.
Is the following legal in the GNU Arduino C. The compiler doesn't seem to like it.
If(temp=-1 || temp1=-1){
}
I'm using the logical OR.
Suggest a simple solution.
No, it isn't legal.
It is spelled "if", not "If"
Also, you're doing an assignment. Did you mean to?
Thanks. Sorry for miss typing.
Was that what the compiler didn't like?
You never actually posted the error message.
Or your code.
The much easier to read functional equivalent code would be (unless the OP overloaded the = operator):
temp = -1;
{}
Notice that the first assignment will evaluate to true thus the second assignment will not be processed because || has short circuit evaluation.
rinthesun, what Udo is trying to point out is that you probably meant :
if ((temp == -1) || (temp1 == -1 )) {....
It's a classic mistake and easy to miss when re-checking your code. The fact you misspelt 'if' suggests you did not realise the difference.
Notice that the first assignment will evaluate to true thus the second assignment will not be processed because || has short circuit evaluation.
No, the compiler doesn't parse it like that. You are looking at it as if it is parsed as:
if ((temp = -1) || (temp1 = -1 )) {
But the compiler sees it as one assignment statement with a syntax error.
if (temp = ( -1 || temp1 = -1 )) {
It finds that it can't turn "-1 || temp1" into an lvalue and spits out an error message.
This, for example, is legal and will compile and is essentially what the compiler was expecting:
if(temp = temp1 = -1){
Pete
But the compiler sees it as one assignment statement with a syntax error.
I disagree.
The parentheses will force it to see it as two assignments, separated by a logical OR.
Since the LHS of the OR is true (because -1 is non zero), the RHS is not evaluated.
Udo was apparently referring to the original statement:
If(temp=-1 || temp1=-1){
which doesn't compile:
sketch_sep12a:4: error: lvalue required as left operand of assignment
Pete
Ah! OK, I'm posting from a phone, so never got around to trying to compile it.
Sorry for the confusion - sometimes people don't specify the error message (like here) if there is one, and assume the compiler has done something wrong because the compiled code always executes the conditional when they think it shouldn't.
Of course, if the OP had cut and pasted the code and the error message, we'd all have saved a lot of time.
Thankyou all. I fixed the problem while waiting.
if(temp == -1 || temp1 == -1) {
statement
}
My problem, I came from coding PIC using Basic and = works.
I will get the hang of it after a while. But this forum is great.
What I am about is using the Arduino to emulate all the functions of the PH Anderson's serial LCD PIC chip.