首页 > 其他 > 详细

【转载】Lua脚本语法说明(修订)

时间:2015-05-13 21:18:59      阅读:230      评论:0      收藏:0      [点我收藏+]

原文:http://www.cnblogs.com/ly4cn/archive/2006/08/04/467550.html

 

挑出来几个

.逻辑运算
    and, or, not
    其中,andor 与C语言区别特别大。
    在这里,请先记住,在Lua中,只有false和nil才计算为false,其它任何数据都计算为true,0也是true!
    and 和 or的运算结果不是true和false,而是和它的两个操作数相关。
    a and b:如果a为false,则返回a;否则返回b
    a or b:如果 a 为true,则返回a;否则返回b

    举几个例子:
     print(4 and 5) --输出 5
     print(nil and 13) --输出 nil
     print(false and 13) --输出 false
     print(4 or 5) --输出 4
     print(false or 5) --输出 5


    在Lua中这是很有用的特性,也是比较令人混洧的特性。
    我们可以模拟C语言中的语句:x == a? b : c,在Lua中,可以写成:x == a and b or c。
    最有用的语句是: x = x or v,它相当于:if not x then x = v end

 

【转载】Lua脚本语法说明(修订)

原文:http://www.cnblogs.com/lan0725/p/4501475.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!