lua系统学习05-表达式

19 篇文章 0 订阅
14 篇文章 0 订阅

函数:

函数可以存储在变量中,可以作为函数的参数 可以作为返回值。
可以重新定义函数增加新的功能 ///这句话暂时没理解 是指动态时期重新定义?
还可以避免运行不可靠代码创建安全运行环境而隐藏函数
lua的所有标准库都是c实现的。所以可以调用lua或c实现的函数。

UserData自定义类型

userdata可以将c数据置入在lua变量中。
userdata 用来描述应用程序或者使用 C 实现的库创建的新类型。
例如:用标准 I/O 库来描述文件。

表达式

常量 、变量、点操作符、一元运算符、二元运算符、括号优先级
关系运算符、逻辑运算符、连接运算符

算术运算符
二元运算符:+、-、*、/、^(幂运算)
一元:只需要一个操作数的运算符称为一元运算符(或单目运算符)。在c语言和lua中好像不存在一元操作符啊,貌似只有一个 -(负号) print(-1)
关系操作符
<> <= >= == ~=
在lua中不对等使用~=关系运算符判断
比较两者变量 :引用比较表、userdata、函数,也就是说当且仅当两者表示 同一个对象 时相等
在这里插入图片描述
上面想要像c#那样文本拼接结果,结果它会优先进行拼接的操作符运算 得到的结果就是 “b与c是否相等:”…b=》 b与c是否相等:1!=c
尝试使用括号优先级,结果发生报错
在这里插入图片描述
暂时不知道如何处理,问题延后解决。
注意以后要注意lua的优先级

测试表的对比和值对比

function expressionTest01()
    a=0
    b=1
    c=1
    print(a==b)
    print(a==c)
    print((b==c))
    a = {}; a.x = 1; a.y = 0
    b = {}; b.x = 1; b.y = 0
    print(a==b)
    print(a.x==b.x)
end
expressionTest01()

输出结果

false
false
true
false
true

为了避免不一致的结果,混合比较数字和字符串,Lua 会报错,比如:2 < “15”。

逻辑运算符

and or not
逻辑运算符也认为false和nil是假 其他是真 0也是真。
and 和 or 的运算结果不是 true 和 false

a and b -- 如果 a 为 false,则返回 a;否则返回 b 
a or b -- 如果 a 为 true,则返回 a;否则返回 b

示例:

function expressionTest02()
    a=nil
    b=0
    res=a and b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end

    a=0
    b=false
    res=a and b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end

    a=0
    b=nil
    res=a and b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end

    a=0
    b=0
    res=a and b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end

    a=1
    b=nil
    res=a or b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end

    a=nil
    b=0
    res=a or b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end

    a=nil
    b=true
    res=a or b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end

a=nil
    b=false
    res=a or b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end

    a=1
    b=2
    res=a and b
    print("逻辑运算符结果:");
    print(res)
    if res then
        print("逻辑正确");
    else
        print("否则");
    end
    
end
expressionTest02()

输出结果:

逻辑运算符结果:
nil
否则
逻辑运算符结果:
false
否则
逻辑运算符结果:
nil
否则
逻辑运算符结果:
0
逻辑正确
逻辑运算符结果:
1
逻辑正确
逻辑运算符结果:
0
逻辑正确
逻辑运算符结果:
true
逻辑正确
逻辑运算符结果:
false
否则
逻辑运算符结果:
2
逻辑正确
总结:
1.a and b  当遇到假 就直接返回。两个操作数都为真 则返回最后一个真的操作数。
2.a or b 两者只要有一个为真就返回当前操作数,都为假返回最后一个操作数

not关键字与c#中!取非 是一样的,且not 后的结果 只返回false 或 true
测试:

a=nil
    b=false
    res=a or b
    print("逻辑运算符结果:");
    print(res)
    if not res then
        print("逻辑正确");
    else
        print("否则");
    end

结果:

逻辑运算符结果:
false
逻辑正确

在lua中and的操作符总是比or优先参与运算。md
示例:

function logicoperatorLevel()
    a=1
    b=false
    ---and 的结果为false or的结果为1
    --[[1--]]levelRes=a and b or a and b---false
    print(levelRes)
    --[[2--]]levelRes=a or b or a and b---1
    print(levelRes)
    --[[3--]]levelRes=b or a and a or b---1
    print(levelRes)
end

logicoperatorLevel()
输出结果:
false
1
1

由编号3的表达式可以看出 and总是比or优先。

c语言中的三元运算符 a?b:c
在lua中对应的实现是这样的
(a and b)or c
测试代码:

function thirdOperation()
    --a?b:c  如果a为true 返回b 否则返回c
    --通过这个 先想到如何让a不返回 又能有返回b 或c的两种情况。
    --通过 and or 特性
    --and遇到false就返回
    --or 遇到true就返回
    a=1 b=2 c=3
    print(a and b or c)

    a=false b=2 c=3
    print(a and b or c)

    a=1 b=false c=3
    print(a and b or c)

    a=false b=false c=3
    print(a and b or c)
end
thirdOperation()

结果

2
3
3
3

通过结果发现这样的表达式并不能完全代替三元运算符。必须要保证b与 c的值不能为假。
意思就是说在lua中只有在b c都为真时候,通过判断是否为真来返回b 与c 。
相比这样我还是喜欢三元操作符 简单易懂。

连接运算符

… – 两个点
字符串连接,如果操作数为数字,Lua 将数字转成字符串。

print("Hello " .. "World") --> Hello World 
print(0 .. 1) --> 01

优先级

从高到低的顺序:

^ 
not - (unary) 
* / 
+ - 
.. 
< > <= >= ~= == 
and 
or
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值