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

lua tutorial 06

2016-01-04 15:42 411 查看
运算符
1.算术运算符

+
加法
-
减法
*
乘法
/
除法
%
取余
^
乘幂
-
负号
unction printTxt( str,num )
print("通过"..str.."计算得到:"..num)
end
a = 10
b = 5
c = a + b
printTxt("加法",c)
c = a - b
printTxt("减法",c)
c = a * b
printTxt("乘法",c)
c = a / b
printTxt("除法",c)
c = a % b
printTxt("取余",c)
c = a ^ b
printTxt("乘幂",c)
c = -a
printTxt("负号",c)

2.关系运算符

==
等于
~=
不等于
>
大于
<
小于
>=
大于等于
<=
小于等于
--关系运算符
function printTxt02(idx, txt )
print("Line"..idx.." - a "..txt.." b")
end
pt = printTxt02
a = 15
b = 10
if a == b then
pt(1,"equal to")
else
pt(1,"not equal to")
end

if a ~= b then
pt(2, "not equal to")
else
pt(2, "equal to")
end

if a < b then
pt(3, "less than")
else
pt(3, "not less than")
end

if a > b then
pt(4,"greater than")
else
pt(4,"not greater than")
end

if a >= b then
pt(5,"greater than or equal to")
else
pt(5,"less than")
end

if a <= b then
pt(6,"less than or equal to")
else
pt(6,"greater than")
end


3.逻辑运算符

and
与 &&
or
或 ||
not
非 !
--逻辑运算符
a = 10
b = 5
if (a > 0) and (b > 0) then
print("a and b all greater than 0")
else
print("a or b all less than 0")
end

a = 10
b = -5
if a > 0 or b > 0 then
print("a or b is greater than 0")
else
print("a and b all less than 0")
end

if not(a > 0) then
print("a is less than 0")
else
print("a is greater or equal to 0")
end


5.其他运算符
..   字符串连接运算符
#  单目运算符:取字符串或表的长度
--其他运算符

a = "Hello "
b = "Lua"

c = a..b
print(c)

print(#c)

tab = {1,2,3,4,5}
print(#tab)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: