ALL expressions in c/c++ are evaluated according to strictly defined rules which define operator precedence order of evaluation, and many other critical factors. It is WELL worth taking the time to study and understand those rules, rather than just assuming they will work as you want them to. Doubly so when the expression format you are attempting to use will not be found in ANY valid code examples or tutorials.
Yes. In particular, that cannot be used to see if one number is in between two other numbers. The values of the variables, I mean.
In this respect it is different to how you are thinking about it from maths class, where
X < Y < Z
means Y is between X and Z.
What it means in C/C++, where it is a perfectly correct expression syntax-wise, is almost never something you want to do. And if somehiw what it does do is what you wanted would be a place where it should be written more carefully, as anyone reading it would have to hands and knees their way through to seeing what 'xactly you were up to.
Catches many ppl at a certain point. The "work arounds" or how it must be done in most programming languages has been shown above.
You can hear it when you explain it⦠X is less than Y and Y is less than Z.
Let's say it in code, so we're clear about what we're saying:
void setup() {
// put your setup code here, to run once:
int A = 80;
int B = 72;
int C = 73;
Serial.begin(115200);
Serial.print("A:");
Serial.println(A);
Serial.print("B:");
Serial.println(B);
Serial.print("C:");
Serial.println(C);
if (A > B > C || A < B < C || B > A > C || B < A < C )
{
Serial.println("(A > B > C || A < B < C || B > A > C || B < A < C) TRUE");
} else {
Serial.println("(A > B > C || A < B < C || B > A > C || B < A < C) TRUE");
}
//Lets say when debugging values of A, B and C are:
Serial.print("A>B>C "); Serial.println(A > B > C ? "TRUE" : "FALSE" );
Serial.print("A<B<C "); Serial.println(A < B < C ? "TRUE" : "FALSE" );
Serial.print("B>A>C "); Serial.println(B > A > C ? "TRUE" : "FALSE" );
Serial.print("B<A<C "); Serial.println(B < A < C ? "TRUE" : "FALSE" );
}
void loop() {
// put your main code here, to run repeatedly:
}
I get different answers than you report:
A:80
B:72
C:73
(A > B > C || A < B < C || B > A > C || B < A < C) TRUE
A>B>C FALSE
A<B<C TRUE
B>A>C FALSE
B<A<C TRUE
Also, this extra ';' on the end of the if statement is a statement itself, so the next line is unconditional:
The key here is the if condition evaluation is logical not "analog". So any evaluation is 0 or 1 or False or True. So after the first test any actual values are no longer available for comparison.