我的第一个lua程序之声明变量和逻辑运算符

--注释:我的第一个lua程序
print("test")

io.write("Hello world, from ",_VERSION,"!\n")

local a=12  --声明局部变量
local d , f = 5 ,10 --declaration of d and f as local variables. 
d , f = 5, 10;      --声明全局变量
d, f = 10           --[[declaration of d and f as global variables. 
                        Here value of f is nil --]]
print ("d=",d);


function add(a,b)
  local c=a+b;
  return c;
end

--http://www.yiibai.com/lua/lua_data_types.html
function printType()
  print(type("What is my type"))   --> string
  t=10
  print(type(5.8*t))               --> number
  print(type(true))                --> boolean
  print(type(print))               --> function
  print(type(type))                --> function
  print(type(nil))                 --> nil
  print(type(type(ABC)))           --> string
end

--逻辑运算符测试
--http://www.yiibai.com/lua/lua_logical_operators.html
function lua_logical_operators()
a = 5
b = 20
if ( a and b )
then
   print("Line 1 - Condition is true" )
end

if ( a or b )
then
   print("Line 2 - Condition is true" )
end

--lets change the value ofa and b
a = 0
b = 10
if ( a and b )
then
   print("Line 3 - Condition is true" )
else
   print("Line 3 - Condition is not true" )
end

if ( not( a and b) )
then
   print("Line 4 - Condition is true" )
else
   print("Line 3 - Condition is not true" )
end

end



-- Variable definition:
local a, b
-- Initialization
a = 10
b = 30
print("value of a:", a)
print("value of b:", b)

-- Swapping of variables
b, a = a, b
print("value of a:", a)
print("value of b:", b)

f = 70.0/3.0
print("value of f", f)

print("add result c=", add(a,b))

printType();

a = "Hello "
b = "World"

print("链接字符串 a with b is ", a..b )
print("Length of b is=",#b ) --去字符串长度
print("Length of b is=",#"Test" )
lua_logical_operators();




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值