--注释:我的第一个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();
我的第一个lua程序之声明变量和逻辑运算符
最新推荐文章于 2022-01-26 08:35:55 发布