if(20<x>40);

int x = ultrasonic.read();
if ( 2040 ) {
}

is this working???

No

int x = ultrasonic.read();
if ( 20 > x & x <40 ) {
}

Safest to use logical AND in if expressions, as the bitwise AND has weird precedence.

if ( (x>20) && (x<40) ) {

Your mistake needs a correction. You said "2040" which means "x > 20 and x > 40"... you need to at least get your math right, before you program.

Oh @aarg “ Your mistake needs a correction. ”

LOL

a7

imeshd:
int x = ultrasonic.read();
if ( 2040 ) {
}

is this working???

Let's analyse.
"x < 20" evaluates to true ( 1) or false (0).
Neither 1 or 0 is greater than 40, so the result is always false, whatever the value of x.

HTH.

Given:
2040

1. Let us first see the meaning of: 20<x (a < b)

If a and b are real numbers, we say that a is less than b and write a < b if (and only if) b - a is positive.

So, x - 20 must be greater than 0 in order to establish that "20<x" is evaluated to true (1).
Therefore, x must be greater than 20.

In the light of above analysis/proposition, Post-3 & 4 are correct.

2. Meaning of x > 40 is obvious.

imeshd:
int x = ultrasonic.read();
if ( 20<x<40 ) {
}

is this working???

imeshd:
int x = ultrasonic.read();
if ( 2040 ) {
}

is this working???

Neither of those work because they aren't valid expressions in C/C++ language. Did you somehow skip over and not read reply #2 and #3?

if ( 20<x<40 )
20<x will return 0 or 1 depending on the value of x,
0 or 1<40 will always be true :

The whole if will always be true.

This is NOT the way to do a range comparison.

if( (x > 20) && (x < 40))
{}

would be the correct way

aarg:
Neither of those work because they aren't valid expressions in C/C++ language. Did you somehow skip over and not read reply #2 and #3?

They are perfectly valid expressions, if they weren't, the compiler would bitch about them.

That they don't do what the author expects is ... unfortunate.

There are many roads to Rome.

if ( ! ( ( x <= 20 ) || ( x >= 40 ) ) ) { }

should also be valid.

In an if/else construct the amount of typing is more or less the same.

if( (x > 20) && (x < 40))  { /* do A */ }
else { /* do B */ }

if( ( x <= 20 ) || ( x >= 40 ) ) { /* do B */ }
else { /* do A */ }

TheMemberFormerlyKnownAsAWOL:
They are perfectly valid expressions, if they weren't, the compiler would bitch about them.

That they don't do what the author expects is ... unfortunate.

I think the compiler would generate a type mismatch warning, if the warnings are enabled.

aarg:
I think the compiler would generate a type mismatch warning, if the warnings are enabled.

I'm sure it does, but how many noobs have warnings enabled, or know how to interpret them?

I am Not Sure why this topic got so many responses. This is not even a mathematical correct Term :smiley:

dr-o:
I am Not Sure why this topic got so many responses. This is not even a mathematical correct Term :smiley:

"x = x + 1" is not mathematically correct, but stick a semicolon on the end, and no-one here will give a damn.

TheMemberFormerlyKnownAsAWOL:
"x = x + 1" is not mathematically correct, but stick a semicolon on the end, and no-one here will give a damn.

x=x+1 is mathematically correct

abdelhmimas:
x=x+1 is mathematically correct

Don't be silly.

TheMemberFormerlyKnownAsAWOL:
Don't be silly.

It is mathematically correct in series with indices like Xn+1 = Xn
I am serious