Using multiple > in a if statement

When debugging my code I realized that I did not get the result I had anticipated and want to understand why.

I have an IF statement that goes as follows:

if (A>B>C ||A<B<C || B>A>C || B<A<C );
{"code"};

Lets say when debugging values of A, B and C are:

A= 80
B=72
C=73

I was getting the following results when I analyzed each condition independently:

A>B>C = FALSE
A<B<C = FALSE
B>A>C = TRUE
B<A<C = TRUE

When my understanding is that (72(B) >80(A)>73(C)) and (72(B)<80(A)<73(C))should be FALSE as well.

If I changed the code to:
(A>B && B>C ||A<B && B<C || B>A && A>C || B<A && A<C )

it performs as desired.

It seems using multiple comparators in a single condition (i.e A>B>C) is calculated in an odd way that I am not understanding.

Can anyone help explain the order of operations to me for future reference?

Thanks!

It's evaluated left to right, so, say "A>B>C".
A>B gives true (1) or false (0), so one or zero > C is usually always false

1 Like

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.

1 Like

Hello jgeidel1022

Take a view here:

HTH

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.

It's all perfectly logical.

a7

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:

( X < Y < Z ) as a chain X < Y returns 0 or 1, is that < Z?

(( X < Y ) && ( Y < Z )) forces the compiler to a single path

j, the CPU is a machine and compilers were inspired by Murphy!

This was a great explanation! Thank you!

1 Like

When you understand why something, the rules make better sense?

Not to beat a dead horse but...

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.

0 is false and non-zero is true which gives an integer all-bits test, great when you have multiple flag bits to see if any flags need to be processed.

This is how the CPU defines logical true and it refers to logical args but IIRC there was at least 1 system or cpu that set all bits to 1 for true.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.