Learning video:
Operating environment
LuatOS online debugging tool
LuatOS documentation https://wiki.luatos.com/Experience LuatOS online->Lua test
(1) Hello World
print("hello world!")
(2) How to declare variables
Example: Declare a number variable a
a=1
print(a)
In Lua, as long as a variable is assigned a value, it means that a variable has been declared. Variables declared in Lua are global by default.
(3) nil type
The nil type means that there is no valid value, as long as the value is not declared, it is nil. Similar to null in other languages, the difference is that undeclared variables in Lua are output as nil, while in many other languages an error will be reported.
a = 1
print(a)
print(b)
(4) Multiple assignment
Example 1:
a,b=1,2
print(a,b)
Example 2:
a,b,c=1,2
print(a,b,c)
(5) Number type/numeric type
a=0x11 --十六进制
b=2e10 --科学计数法
c=10
print(a,b,c)
(6) Operators
Refer to the content in Novice Programming
Novice Programming - Lua operator part https://www.runoob.com/lua/lua-miscellaneous-operator.html Supplement: Support for left shift and right shift symbols has been added in Lua5.3
print(1<<3)
print(8>>3)
(7) String type/string
--双引号间的一串字符
str1 = "Lua"
--单引号间的一串字符
str2 = 'Lua'
--[[和]]--间的一串字符
str3 = [[Lua]]
str4 = [[使用双括号时,甚至能包含换行数据
换行了
最后一行]]
--输出所有字符串
print(str1)
print(str2)
print(str3)
print(str4)
escape character
r = 'ab\\cd\"ef\'g\\h]]'
print(str)
reference
Rookie tutorial string part https://www.runoob.com/lua/lua-strings.html
String concatenation
print('abc'..'def')
(8) function function
function function_name(参数1,参数2,...)
--方法体
end
function_name = function(参数1,参数2,...)
--方法体
end