lua 小白问题 a and b or c

CodeU 2015-03-28 12:10:31
教程上说 的是 a and b or c 就像 c里面的 a?b:c
可是我有点想不通
下面是 例子
a = 1;b= false;c = 2;
c++里 是
d= a?b:c; 结果 毫无疑问 是 d = b;//(false)


lua里是
d = a and b or c ;
-- 先算的是 a and b 因为a 是 true 则返回 b
--[[然后是上面的结果 b or c , 因为b是false 所以返回 c
所以结果 是 d = c ]]
end
上面看起来结果完全不同 这是为什么?
...全文
1309 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
玄离gg 2015-11-09
  • 打赏
  • 举报
回复
如果能够断言a and b or c中的b一定为真,那直接用这种写法不会错 如果b为false,就会出错
七擒关羽 2015-03-30
  • 打赏
  • 举报
回复
这里的and or 是指 && ||,我没看到过你提的这种理解
panghuhu250 2015-03-28
  • 打赏
  • 举报
回复
这儿(http://lua-users.org/wiki/TernaryOperator)说到a and b or c的用法, 指出:
引用
The main caveat is that if a or c evaluates to true while b or d respectively evaluate to false, then this expression will not behave exactly like the ternary operator. Here, "evaluate to false" means that the value is either false or nil, and "evaluate to true" means not evaluate to false. In the first line above, a and b or c is interpreted as (a and b) or c (because and has higher precedence than or), and if a evaluates to true, then the expression becomes b or c, and if b evaluates to false, then the expression becomes c (not b as you might want).
zuxi 2015-03-28
  • 打赏
  • 举报
回复
教程也是人写的,难免有错。 你都用程序来证明了,难道就想不到教程是错的? 实践就是最好的教程!

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧