Lua中多个条件的评估顺序和优先级

我需要评估一个条件,如下所示:

condition1 and (condition2 or condition3)

其中 condition2condition3 只有在 condition1 为真时才应被评估。

我如何在 lua 中写这个条件?

下面的代码是否正确?

if condition1 and (condition2 or condition3) then
...
end
点赞
用户1009479
用户1009479

Lua手册

Both and and or use short-cut evaluation; that is, the second operand is evaluated only if necessary.

这意味着在表达式condition1 and (condition2 or condition3)中,只有在条件1为真时,才会计算condition2 or condition3

请注意,这也意味着在表达式condition2 or condition3中,只有当条件2为假时才会计算condition3

2014-05-05 11:17:38