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

1. ## 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?

2. 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".

3. 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.

4. 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".