LUA中的and与or
来源:互联网 发布:剑灵最美天族捏脸数据 编辑:程序博客网 时间:2024/06/03 13:12
逻辑运算符认为false和nil是假(false),其他为真,0也是true.
and的优先级比or高
其它语言中的and表示两者都为真的时候,才返回为真,而只要有一个假,都返回假.lua虽不仅返回假的语义,还返回导致假的值.也就是说
a and b
在a为false的时候,返回a,否则返回b.
or的处理与之类似,
a or b
在a为true的时候,返回a,否则返回b.
总之,and与or返回的不仅有true/false的语义,还返回了它的值.
a = nilb = 1exp = 1 < 2 and a or bprint(exp == a) --falesexp = 1 > 2 and a or bprint(exp == b) --trueexp = (1 < 2 and {a} or {b})[1]print(exp == a) --trueexp = (1 > 2 and {a} or {b})[1]print(exp == b) --true
false
true
true
true
不过正确的方案书写太过复杂,反而弄巧成拙。lua中没有提供语言内置的支持,还是转向正常的if/else吧。
当然,如果编码者能够断言c and a or b中的a一定为真,那直接用这种写法也不会错,但这就是一个hack了,不值得推崇。
- LUA中的and与or
- LUA中的and与or
- LUA中的and与or
- LUA中的and与or
- LUA中的and与or
- lua and or not
- lua中逻辑运算符and与or的用法
- lua中逻辑运算符and与or的用法
- Mysql中的 and 与or 优先级别
- Python中的逻辑表达式and与or
- SQL 中的 AND OR
- Python中的and、or、and-or
- 【转载】python 中的 and、or、and-or
- lua and or not 逻辑运算符
- lua and or not 逻辑运算符
- lua and or not 逻辑运算符
- LUA中and or 特殊用法
- lua: 运算符易错点: and or 连接符'..'
- 剪枝导论
- AIDL
- 关于so依赖关系
- Ubuntu下搭建android NDK R7开发环境
- Debian安装MYSQL
- LUA中的and与or
- android ListView详解
- vs2010最全快捷键
- 你真的了解HTML吗?–雅虎面试题
- Nginx 调用CGI
- Java1.6 发布web service的方法
- jqPlot的Option配置对象详解 .
- android判断网络的类型
- 确定对话框框架边框的大小及重绘