Programming logic question

Can one of you code gurus confirm that these two lines of code will give the same result:

//line 1
if(x == y && x == 1)
//line 2
if(x == y == 1)

They both compile OK. Am I correct in assuming that line 2 will execute faster than line 1?

Yes, they will give the same results, but that depends on the value 1. A similar construct:

x == y == 2;

would return false even if x == 2 and y == 2.

Reason: true equals 1, and evaluation proceeds from left to right. So x == y will always be 1 if x and y are the same value.

Thanks for the rapid reply, Spatula.

I was only using 1 as an example My 'x' can be any number from 0 to 15, so it won't give the same results.

H_B this is a good case for using serial.print to debug.... easy enough to code the example as you like, and put serial.prints in to each possible outcome of the "if"... have it send a message like "i'm in the if part" or "i'm in the else part", then you can easily see what tests are passing and failing.

You are not correct in assuming that the second will run faster. The first will "short circuit". If x does not equal y, the comparison of x and the constant is not performed. Both tests will have to be performed in the second case.

Reason: true equals 1

Wrong. Any non-zero value is true.

Henry_Best: Can one of you code gurus confirm that these two lines of code will give the same result:

//line 1
if(x == y && x == 1)
//line 2
if(x == y == 1)

They both compile OK. Am I correct in assuming that line 2 will execute faster than line 1?

Doubtful that the compiled code would be any faster, or that the 2nd line would be bug-free. There's 3 operations either way.

PaulS:

Reason: true equals 1

Wrong. Any non-zero value is true.

Ok, perhaps I should have written "true evaluates to 1". But wait, I could just quote arduino.h:

#define true 0x1

Which really helps when using bit-zero XOR.

Actually any non zero value is just not 'false' which is NOT the same as 'true'

Actually any non zero value is just not 'false' which is NOT the same as 'true'

That's an interesting definiton and one which mirrors some early C definitions

#define false 0
#define true (!false)

KeithRB: You are not correct in assuming that the second will run faster. The first will "short circuit". If x does not equal y, the comparison of x and the constant is not performed. Both tests will have to be performed in the second case.

In the case of x and y both being 1, the first test would have to do two equality checks and then AND the results. The second test would only need to do the two equality checks. But that's moot, as the second line doesn't work how I expected it to.

lloyddean:
Actually any non zero value is just not ‘false’ which is NOT the same as ‘true’

And yet,
if (8)
will evaluate to true.
8 is not even the same bit as 1.

Maybe this might help: ( x & y ) == z

With a cast to prevent int promotion, this compiles smaller anyway ( untested )

void loop(){}

uint8_t x;
uint8_t y;

void setup(){
  
  x = analogRead(0);
  y = analogRead(0);
  const uint8_t z = 2;
  
  //if( x == y && x == z ){
    
  if( uint8_t( x & y ) == z ){
    x = analogRead(0);
  }
  y = analogRead(0);
}

Henry_Best: if(x == y == 1)

I strongly advise against this. For a start, it looks like a typo. And then you have to sit and think about operator evaluation order, and what is really meant. And certainly, it would only work for 1 and not 2.

Plus, it may not run faster.

Without wishing to get bogged down in semantics, this page from MSDN cautiously expresses how an "if" works without using the words "true" or "false".

http://msdn.microsoft.com/en-us/library/fh88ctk1%28v=vs.80%29.aspx

The if statement controls conditional branching. The body of an if statement is executed if the value of the expression is nonzero.

True.

It is possible to narrow things down by using type boolean arguments, but not faster if you have to cast other types to boolean.

pYro_65: Maybe this might help: ( x & y ) == z

With a cast to prevent int promotion, this compiles smaller anyway ( untested )

void loop(){}

uint8_t x; uint8_t y;

void setup(){     x = analogRead(0);   y = analogRead(0);   const uint8_t z = 2;     //if( x == y && x == z ){       if( uint8_t( x & y ) == z ){     x = analogRead(0);   }   y = analogRead(0); }

Maybe I'm wrong, but I can't see how bitwise ANDing x and y determines if they are the same. Lets say x = 15 (1111) and y = 1 (0001). Bitwise ANDing will give 0001. If z = 1, the result will be true, even though x != y.

GoForSmoke: 8 is not even the same bit as 1.

I think it would be more accurate to say that the logical operators will treat any value which is not false, as true.