您的位置:首页 > 编程语言 > Lua

Lua学习记录 — (2) 运算符

2018-01-31 17:44 483 查看
Operator.lua

------------------------------------ 算术运算符 ------------------------------------

-- 加减乘除(+ - * /)
a = 21
b = 5
c = a+b; print(c)                                   --> 26
c = a-b; print(c)                                   --> 16
c = a*b; print(c)                                   --> 105
c = a/b; print(c)                                   --> 4.2

-- # 取模 幂 整除(% ^ //)
c = a%b; print(c)                                   --> 1(5*4=20 21-20=1 余1)
c = a^b; print(c)                                   --> 4084101.0(a的b次方)
c = a//b;print(c)                                   --> 4(21/5=4 余 1)

------------------------------------ 关系运算符 ------------------------------------

a,b,c = 1,2,1

print (a==b)                                        --> false
print (a>b)                                     --> false
print (a<b)                                     --> true
print (a>=c)                                        --> true
print (a<=c)                                        --> true
print (a~=b)                                       --> true

------------------------------------ 逻辑运算符 ------------------------------------

-- Lua中,只有false 和 nil 为假,其他都为真(包括数字0);
a,b,c = 1,2,nil
f = false

-- 与 and (有一个为false则返回false或nil,都为true则返回第二个值)
print (a and b)                                     --> 2
print (a and c)                                     --> nil
print (c and a)                                     --> nil
print (a and f)                                     --> false(一个真一个假,则返回假的值)
print (f and a)                                     --> false
print (c and f)                                     --> nil(两个都是假,则返回第一个值)
print (f and c)                                     --> false

-- 或 or (如果第一个为真,则返回第一个值,否则返回第二个值)
print (a or b)                                      --> 1
print (c or b)                                      --> 2

-- 非 not()
print (not b)                                       --> false
print (not c)                                       --> true

------------------------------------ 其他运算符 ------------------------------------

-- Lua目前没有
a443
自增自减运算符,以及其他算数运算的缩写形式,如 a += 1 报错!!

-- Lua5.1没有位运算符;
-- 在Lua5.3中,是支持位运算的:(与&、或|、异或~、非~、左移<<、右移>>)
a = 44  -- 0010 1100
b = 21  -- 0001 0101
print (a & b)                                       --> 4(0000 0100)
print (a | b)                                       --> 61(0011 1101)
print (a ~ b)                                      --> 57(0011 1001)
print (~a)                                         --> -45(1101 0011)
a = 2   -- 0000 0010
print (a<<2)                                            --> 8(0000 1000)
print (a>>1)                                            --> 1(0000 0001)

-- 字符串连接运算符 ..
str1,str2 = "你好","china"
str3 = str1 .. str2 .. "!!!"
print(str3)                                             --> 你好china!!!

-- 字符串取长度运算符 #(也可获取表table的长度,但仅仅只是最大的索引值)
print(#str1)                                            --> 4(1个中文占2个字符长度)
print(#"china")                                     --> 5
print(#(str1 .. "china"))                           --> 9

tbl = {key1 = "123",1,2}
print(#tbl)                                         --> 2(最大索引下标为2)
tbl[4] = "1"
print(#tbl)                                         --> 4(最大索引下标为4,3是空缺的)

tbl2 = {1,2} tbl2[5]=5
print(#tbl2)                                            --> 2(最大索引下标为5,3和4是空缺的,缺了超过1位以上,则长度还是2)

--[=[       《补充》

1. table表成员函数访问符(点. 和 冒号:)
tbl = {name = "BeiJiaan",1,2,3}

function tbl:f1()       -- 函数定义时,使用: ,则默认传入self(即调用者自身)
print(self.name)
end

function tbl.f2(self)   -- 函数定义时,使用. ,则需要手动传入self
print(self.name)
end

tbl:f1()                -- 函数调用时,使用:,表示传入self
tbl.f1(tbl)             -- 函数调用时,使用. ,表示不自动传入self
tbl:f2()
tbl.f2(tbl)

]=]

------------------------------------ 运算符优先级 ------------------------------------

-- 从高到低顺序:

-- ^
-- not -(负数)
-- * /
-- + - (减)
-- < > <= >= ~= ==
-- and
-- or
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: