Is that a valid expression? If so, can you rewrite it so that it makes more sense? For example, is it the same as (4 > y && y > 1)
? How do you evaluate chained logical operators?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
I think it's a valid expression (not a statement), but probably doesn't do what you want. It evaluates left-to-right, as
(4 > y) > 1
. The test4 > y
will evaluate to either 0 (false) or 1 (true), and the entire expression will always evaluate to 0 (false).4 > y will evaluate to a boolean value of true or false. The remainder of the expression is then essentially [true|false] > 1, which does not make sense.
The statement
(4 > y > 1)
is parsed as this:The comparison operators
<
and>
evaluate left-to-right.The
4 > y
returns either0
or1
depending on if it's true or not.Then the result is compared to 1.
In this case, since
0
or1
is never more than1
, the whole statement will always return false.There is one exception though:
If
y
is a class and the>
operator has been overloaded to do something unusual. Then anything goes.For example, this will fail to compile:
Expression Validity
Yes, it is a valid expression, assuming y is, or can be implicitly converted into, an integer. If it is not and the
>
operator is overloaded, it's a different story outside the scope of this question.It will be evaluated left to right as
((4 > y) > 1)
.Assuming
y
is an integer, let's consider the two possibilities.4 > y
can returntrue
orfalse
. The next part effectively becomestrue > 1
orfalse > 1
.Given the implicit bool to int conversion, there are two possibilities: A)
4 > y
returnstrue
.true
evaluates to1
.1 > 1
evaluates to false. B)4 > y
returnsfalse
.false
evaluates to0
.0 > 1
evaluates to false.No matter what, the expression will evaluate to false.
Rewritten Interpretation
I assume what you intend is
((4 > y) && (y > 1))
.Example
(4 > y > 1)
is not the same as(4 > y && y > 1)
.Logical Operators
The logical operators (
!
,&&
,||
) use short-circuiting logic.Given
a && b
,a
will be evaluated. Ifa
evaluates to true, thenb
will be evaluated. Else,b
will not be evaluated. As fora || b
, short-circuiting logic works in reverse.a
will be evaluated. Since expressiona
is evaluated first, if it is false, there is no possibility that the entire expression will evaluate true.Given
a || b
,a
will be evaluated. Ifa
evaluates to false, thenb
will be evaluated. Else,b
will not be evaluated. Since expressiona
is evaluated first, if it is true, there is no possibility that the entire expression will evaluate false.Chaining the operators is a matter of operator precedence. Better to use parentheses and be clear rather than risk the wrong behavior.
4 > y > 1 --> MAY BE ANYWAY if y - is class!!.