Lua study notes (1) 2023.08.08

Learning video:

https://www.bilibili.com/video/BV1vf4y1L7Rb/?spm_id_from=333.337.search-card.all.click&vd_source=5c3f770c8a6964a2c7d2456189ea41a1icon-default.png?t=N6B9https://www.bilibili.com/video/BV1vf4y1L7Rb/?spm_id_from=333.337.search-card.all.click&vd_source=5c3f770c8a6964a2c7d2456189ea41a1

Operating environment

LuatOS online debugging tool

LuatOS documentation icon-default.png?t=N6B9https://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)
Output results

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)
Output results

(4) Multiple assignment

Example 1:

a,b=1,2
print(a,b)
Output results

 Example 2:

a,b,c=1,2
print(a,b,c)
Output results

 (5) Number type/numeric type

a=0x11  --十六进制
b=2e10  --科学计数法
c=10
print(a,b,c)
Output results

(6) Operators

Refer to the content in Novice Programming

Novice Programming - Lua operator part icon-default.png?t=N6B9https://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)
Output results

 (7) String type/string

--双引号间的一串字符
str1 = "Lua"
--单引号间的一串字符
str2 = 'Lua'
--[[和]]--间的一串字符
str3 = [[Lua]]
str4 = [[使用双括号时,甚至能包含换行数据
换行了
最后一行]]

--输出所有字符串
print(str1)
print(str2)
print(str3)
print(str4)
Output results

escape character

r = 'ab\\cd\"ef\'g\\h]]'
print(str)
Output results

reference

Rookie tutorial string part icon-default.png?t=N6B9https://www.runoob.com/lua/lua-strings.html

 String concatenation

print('abc'..'def')
Output results

 (8) function function

function function_name(参数1,参数2,...)
    --方法体
end
function_name = function(参数1,参数2,...)
    --方法体
end

Guess you like

Origin blog.csdn.net/weixin_46041788/article/details/132165915