Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
lua逻辑运算符/a?b:c运算符_Lua_Ternary Operator - Fatal编程技术网

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”几乎立即引导到该页面,我非常确定该问题应该否决,而不是答案。