bool valor = 0<=50>=6;
I thought that the sequence would evaluate the values two by two, that is, it evaluates 0<=50
, which is true
and then 50>=6
, which is also true
, so that the final value is also true
, but I see that it is not.
So how would such an expression be evaluated?
In C++, evaluations can be unary or binary, that is, they can evaluate one term or two... never three or more:
In the case that you propose, and since ternary evaluations do not exist, the equivalent would be the following:
The processor only understands numerical digits. Thus, false conditions return 0 and true conditions return 1.
The first condition the compiler encounters is
0<=50
. Since the condition is true, the return result will be 1. This value is used for the second condition,1>=6
. This last condition is false, so the final result will befalse
.What you intend to do is solved using operators:
In the instruction that you comment you are really doing 2 operations. First
0<=50
it returnstrue(1)
.The second one that it does to you is the equivalent to
(0<=50)>=6
which it returnsfalse(0)
, really the comparison that it does to you in the second case is the result of0<=50
comparing it with the6
=1>=6
(taking into account thattrue = 1
andfalse = 0
).