2014-09-24 38 views
2

我不明白的LUA逻辑运算符/ A B:C运营商

aBoolValue = false 
    ans = aBoolValue and 'yes' or 'no' 
    print (ans) 

什么是“和”和“或”操作正是工作的逻辑是什么?

+2

查找Lua的短路评价 – 2014-09-24 07:19:48

回答

4

检查这个

http://www.lua.org/pil/3.3.html

为了您例如,如果aBoolValue ==真,那么它将打印 '是' 别人 '不'

+0

我倒是投票下来这是一个唯一的链接的答案,但“文档是你的朋友”始终是一个很好的答案。 – 2014-09-24 23:23:18

+0

@Tom Blodget“lua and or”几乎立即引导到该页面,我很确定这个问题应该是downvoted,而不是一个答案。 – user3125367 2014-09-25 16:29:35

0

考虑以下代码:

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