lua逻辑运算符/a?b:c运算符
我不明白这件事的逻辑lua逻辑运算符/a?b:c运算符,lua,ternary-operator,Lua,Ternary Operator,我不明白这件事的逻辑 aBoolValue = false ans = aBoolValue and 'yes' or 'no' print (ans) “and”和“or”操作符究竟起什么作用?检查此项 对于您的示例,如果aBoolValue==TRUE,则它将打印“yes”或“no”考虑以下代码: local example1 = true and "yes" or "no" local example2 = false and "yes" or "no" pr
aBoolValue = false
ans = aBoolValue and 'yes' or 'no'
print (ans)
“and”和“or”操作符究竟起什么作用?检查此项
对于您的示例,如果aBoolValue==TRUE,则它将打印“yes”或“no”考虑以下代码:
local example1 = true and "yes" or "no"
local example2 = false and "yes" or "no"
print(example1, example2) --> yes no
如果布尔值为true,则返回和之后的值。如果布尔值为false,则返回或之后的值。你可以这样想:
local example1 = if (true) then "yes" else "no"
--obviously this code won't work, but it shows how ternary operations work
查找Lua短路评估我会将此作为一个仅链接的答案否决,但“文档是你的朋友”始终是一个好答案。@Tom Blodget“Lua and or”几乎立即引导到该页面,我非常确定该问题应该否决,而不是答案。