Grouping (this is what non-comparison operators do):
a + b + c means (a + b) + c
Chaining (this is what comparison operators do):
a < b < c means (a < b) and (b < c)
Grouping left to right (this is the way things are grouped):
5 - 2 - 1 means (5 - 2) - 1 == 2
as opposed to grouping right to left (this would produce a different result):
5 - (2 - 1) == 4
Chaining left to right
Chaining is left to right, so in a < b < c
, the expression a < b
is evaluated before b < c
, and if a < b
is falsey, b < c
is not evaluated.
(2 < 1 < f())
gives the value False
without calling the function f
, because 2 < 1
evaluates to false, so the second comparison does not need to be performed.
f() > 1 > g()
calls f()
in order to evaluate the first comparison, and depending on the result, it might or might not need to evaluate the second condition, which requires calling g()
.
NB. Each operand is evaluated at most once. So in the expression 1 < f() < 2
, the function f()
is only called once, and the value it gives is used in both comparisons (if necessary).
https://en.wikipedia.org/wiki/Short-circuit_evaluation
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…