# if with multiple or conditions

Simple question. Both of these statements compile, but are they equivalent?

1. if (x = a or x = b or x = c) {

2. if (x = (a or b or c)) {

Neither of them is correct so it does not matter whether they are equivalent or not.

= is used for assignment
== is used for comparison

UKHeliBob:
Neither of them is correct so it does not matter whether they are equivalent or not.

= is used for assignment
== is used for comparison

Sorry, I got in a hurry. These statements compile, but are they equivalent?

1. if (x == a or x == b or x == c) {

2. if (x == (a or b or c)) {

1. Say a = 1, b = 2, c = 4
Then if x ==1, or x==2, or x==4, test passes.

2. Say a = 1, b = 2, c = 4
Then (1 or 2 or 4) == 7, because 0001 | 0010 | 0100 = 0111
and now you are testing if x ==7, which is not the same as 1, 2, 4

So I would suggest no, they are not equal.

The only time they are equivalent is if x = 0 and you are testing that the rest are 0 also, which certainly have its uses. Such as testing if 3 buttons are pressed before allowing an action to happen.

The only time they are equivalent is if x = 0 and you are testing that the rest are 0 also, which certainly have its uses. Such as testing if 3 buttons are pressed before allowing an action to happen.

Thanks! That explains why a complex sketch I was testing got screwy after I tried to get fancy with my ‘or’ statements.

I needed additional braces in the first expression to make it compile.

Functional both versions are identical.

``````void var1(bool a, bool b, bool c) {
bool x;
if ((x = a) or (x = b) or (x = c)) {
Serial.print(F("true "));
} else {
Serial.print(F("false"));
}
}

void var2(bool a, bool b, bool c) {
bool x;
if (x = (a or b or c)) {
Serial.print(F("true "));
} else {
Serial.print(F("false"));
}
}

void pParm(char name, bool val) {
Serial.write(' ');
Serial.write(name);
Serial.print(F(" = "));
Serial.print(val ? F("true ") : F("false"));
}

void setup() {
Serial.begin(250000);
for (byte pattern = 0; pattern < 8; pattern++) {
pParm('a', pattern & 1);
Serial.write(',');
pParm('b', pattern & 2);
Serial.write(',');
pParm('c', pattern & 4);
Serial.print(F(" : var1 = "));
var1(pattern & 1, pattern & 2, pattern & 4);
Serial.print(F(", var2 = "));
var2(pattern & 1, pattern & 2, pattern & 4);
Serial.println();
}
}

void loop() {}
``````
`````` a = false, b = false, c = false : var1 = false, var2 = false
a = true , b = false, c = false : var1 = true , var2 = true
a = false, b = true , c = false : var1 = true , var2 = true
a = true , b = true , c = false : var1 = true , var2 = true
a = false, b = false, c = true  : var1 = true , var2 = true
a = true , b = false, c = true  : var1 = true , var2 = true
a = false, b = true , c = true  : var1 = true , var2 = true
a = true , b = true , c = true  : var1 = true , var2 = true
``````

bbrock:
Sorry, I got in a hurry. These statements compile, but are they equivalent?

1. if (x == a or x == b or x == c) {

2. if (x == (a or b or c)) {

Note that the "or" keyword is logical or (||) rather than a bitwise or (bitor, |), but in either case those two pieces of code are different.

Your code "2." is really only valid if all of the variables, x,a,b,c are boolean. With numerical values, then the conditional statement will be executed only under precisely the following conditions

• if x is zero and each of a,b,c are also zero.
• if x is one and any of a,b,c are non zero.

These still each need == to make comparisons

`````` if ((x = a) or (x = b) or (x = c)) {

if (x = (a or b or c)) {
``````

If you want to compare with x, its a perfect legal assignment with a boolan value.

The testresult (a or b or c) will be placed in x, in both cases.

But the OP changed his code and so the question.
The comparisons are clearly different.

stuart0:
Note that the "or" keyword is logical or (||) rather than a bitwise or (bitor, |), but in either case those two pieces of code are different.

Your code "2." is really only valid if all of the variables, x,a,b,c are boolean. With numerical values, then the conditional statement will be executed only under precisely the following conditions

• if x is zero and each of a,b,c are also zero.
• if x is one and any of a,b,c are non zero.

And in my case, x,a,b,and c are not boolean, they are integers read from serial com so things started going screwy. Thanks for helping me understand why.

bbrock:
And in my case, x,a,b,and c are not boolean, they are integers read from serial

I have learned my lesson: "never reply to snippets".