If With Multiple Or Conditions - Programming Questions - Arduino Forum
Maybe your like
Simple question. Both of these statements compile, but are they equivalent?
-
if (x = a or x = b or x = c) {
-
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
bbrock December 27, 2016, 4:26pm 3UKHeliBob: 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?
-
if (x == a or x == b or x == c) {
-
if (x == (a or b or c)) {
-
Say a = 1, b = 2, c = 4 Then if x ==1, or x==2, or x==4, test passes.
-
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.
CrossRoads December 27, 2016, 4:40pm 5The 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.
bbrock December 27, 2016, 4:47pm 6CrossRoads: 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.
Whandall December 27, 2016, 4:50pm 7I 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 stuart0 December 27, 2016, 5:00pm 8bbrock: Sorry, I got in a hurry. These statements compile, but are they equivalent?
if (x == a or x == b or x == c) {
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)) { Whandall December 27, 2016, 5:03pm 10If 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.
bbrock December 27, 2016, 5:19pm 11stuart0: 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.
Whandall December 27, 2016, 5:26pm 12bbrock: 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".
Related topics
| 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 |
Tag » Arduino If Statement Multiple Conditions
-
How To Make Two Conditions In An If Statement - Arduino Forum
-
If Statement With Multiple Conditions - Arduino Forum
-
IF Statement Multiple Conditions - Arduino Forum
-
Multiple If Statement Conditions - Arduino Stack Exchange
-
Arduino If-else Statement - Linux Hint
-
How To Use Conditional Statements In Arduino Programming
-
Arduino Nested If Statement | Delft Stack
-
More Decisions With If... Else If - Parallax Inc
-
2.7 Understanding If / Else And While Statement In Arduino
-
[PDF] Arduino If Statement With Multiple Conditions
-
Arduino If Statement Multiple Conditions - CopyProgramming
-
Arduino If-else And Else-if - JavaTpoint
-
How Do You Code An If Statement With Multiple Conditions? : R/arduino
-
Arduino IDE: Conditional(if-else-if) Statements - STEMpedia