# x = x < y < 2; WHY IS THIS ALWAYS TRUE!?

• 06-07-2007
sh4k3
x = x < y < 2; WHY IS THIS ALWAYS TRUE!?
sorry, this is a newbie question, but why is this expression always true?

For example:

x = 3; y = 4; x = x < y < 2;

x = 3 < 4 < 2;

3 < 4 = T
4 < 2 = F

therefore T < 2;

is this true because 2 is a non-zero, therefore evaluates to T?

• 06-07-2007
dwks
The expression
Code:

`x = x < y < 2;`
is equivalent to
Code:

```int less = x < y; x = less < 2;```
or
Code:

```int less; if(x < y) less = 1; else less = 0; if(less < 2) x = 1; else x = 0;```
Generally, you don't use that sort of syntax. Maybe you're looking for
Code:

`x = x < y && y < 2;`
which is the same as
Code:

```if(x < y && y < 2) x = 1; else x = 0;```
where && means "and".

• 06-07-2007
Rashakil Fol
it's not the case that 3 < 4 = T. It's the case that 3 < 4 == 1. The comparison operators return 1 or 0.

So x = 3 < 4 < 2 simplifies to x = (3 < 4) < 2 which simplifies to 1 < 2 which simplifies to 1.
• 06-07-2007
sh4k3
Quote:

Originally Posted by dwks
The expression
Code:

`x = x < y < 2;`
is equivalent to
Code:

```int less = x < y; x = less < 2;```
or
Code:

```int less; if(x < y) less = 1; else less = 0; if(less < 2) x = 1; else x = 0;```
Generally, you don't use that sort of syntax. Maybe you're looking for
Code:

`x = x < y && y < 2;`
which is the same as
Code:

```if(x < y && y < 2) x = 1; else x = 0;```
where && means "and".

thanks for the link, ill keep that handy!
• 06-07-2007
sh4k3
Quote:

Originally Posted by Rashakil Fol
it's not the case that 3 < 4 = T. It's the case that 3 < 4 == 1. The comparison operators return 1 or 0.

So x = 3 < 4 < 2 simplifies to x = (3 < 4) < 2 which simplifies to 1 < 2 which simplifies to 1.

okay, that makes perfect sense. Gotcha !

thanks!
• 06-08-2007
iMalc
Make sure you set your compiler to the highest warning levels so that it can point out silly mistakes such as comparing mismatching types. Then fix those errors without resorting to casting.