Lua有OR比较吗?

Pol*_*yov 17 logic lua if-statement

我刚刚开始使用Lua,我想知道(因为我在网站上找不到它)如果Lua有一个OR运算符,就像其他语言中的那样||:

if (condition == true || othercondition == false) {
 somecode.somefunction();
}
Run Code Online (Sandbox Code Playgroud)

而在Lua,有

if condition then
    x = 0
end
Run Code Online (Sandbox Code Playgroud)

我如何在Lua中编写IF块来使用OR?

Pup*_*ppy 27

用"或".

if condition or not othercondition then
    x = 0
end
Run Code Online (Sandbox Code Playgroud)

正如Lua手册中明确指出的那样.

  • 应该注意,在Lua中,"或"表达式不一定返回布尔值.如果其值不为false或为nil,则返回第一个参数(甚至不执行第二个参数). (7认同)
  • 不是`~`(仅用于`〜=`,即"不等于"),但是"不".修复了答案中的示例. (3认同)
  • 哎呀。用一种语言编码,用另一种语言回答,不可避免地会导致错误。 (2认同)