If With Multiple Or Conditions - Programming Questions - Arduino Forum

if with multiple or conditions Projects Programming December 27, 2016, 4:14pm 1

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)) {

December 27, 2016, 4:21pm 2

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

= is used for assignment == is used for comparison

December 27, 2016, 4:26pm 3

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)) {

December 27, 2016, 4:38pm 4
  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.

December 27, 2016, 4:40pm 5

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.

December 27, 2016, 4:47pm 6

CrossRoads: 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.

December 27, 2016, 4:50pm 7

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 December 27, 2016, 5:00pm 8

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.
December 27, 2016, 5:01pm 9

These still each need == to make comparisons

if ((x = a) or (x = b) or (x = c)) { if (x = (a or b or c)) { December 27, 2016, 5:03pm 10

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.

December 27, 2016, 5:19pm 11

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.

December 27, 2016, 5:26pm 12

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".

Topic Replies Views Activity
Multiple comparisons in an if statement Programming 5 2124 May 5, 2021
Programming logic question Programming 33 9197 May 5, 2021
Arduino && and || in same if statement Programming 4 3728 May 6, 2021
What's the difference of Programming 7 466 January 8, 2023
what does if(x=y) do? Programming 10 4644 May 5, 2021
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino If Statement Multiple Conditions