美文网首页
Lua逻辑运算符

Lua逻辑运算符

作者: YellowFish | 来源:发表于2017-08-14 16:42 被阅读0次

    Lua逻辑运算符

    and or not --对应c#的 && || !
    
    • 逻辑运算符认为false和nil是假(false),其他都为真,0也是true.
    if 0 then
        print("do it")
    end
    
    --输出结果 do it
    
    if false then
        print("do it")
    end
    --输出结果 (无)
    
    if nil then
        print("do it")
    end
    --输出结果 (无)
    

    Lua和C#逻辑运算符的异同

    CSharp###

    • 我们都知道c# a&&b 只会返回一个true或者false
    • 真真为真,真假为假,假假为假
    a && b  //如果a为false,b为true,返回false.
    

    lua

    • lua则不同,不是返回一个bool值,而是返回判断的变量,如下代码
    a and b -- 如果a为false,则返回a,否则返回b
    
    a or b -- 如果a为true,则返回a,否则返回b
    

    相关文章

      网友评论

          本文标题:Lua逻辑运算符

          本文链接:https://www.haomeiwen.com/subject/tgwurxtx.html