捣鼓Nodemcu02_初识Lua

前言

如果已经有C语言基础的朋友,会发现Lua真的很简单。这里直接引用土神的笔记。原文链接:https://www.jianshu.com/p/5927eae240ba

注意的地方

部分与C语言不一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
不要在结尾使用                 ;
数组的第一个元素下标 是1不是0
----------------------------------------------------------------------
不等于 ~=
逻辑与 and
逻辑或 or
逻辑非 not
---------------------------------------------------------------------
注释用 --
空值 nil
连字符 ..
所有代码块需要结束标志 end
--------------------------------------------------------------------
不能使用 i++ i-- ++i --i

输出

依然是熟悉的

print(“XXX”)

1
2
3
4
print("hello world!")
----输出hello world!
print("5+3="..(5+3))
----输出5+3=8

if

if () then

end

1
2
3
4
5
i=6
if (i<7) then
print("yes!")
end
--输出yes!

if-else

if () then

else

end

1
2
3
4
5
6
7
i=9
if (i<5) then
print("yes!")
else
print("no!")
end
--输出no!

elseif

if () then

elseif () then

end

1
2
3
4
5
6
7
i=7
if (i<5) then
print("i<5")
elseif (i>6) then
print("i>6")
end
--输出i>6

while

while () do

end

1
2
3
4
5
6
7
8
9
10
11
i=5
while (i>0) do
print(i)
i=i-1
end
--输出
5
4
3
2
1

for循环

1
2
3
4
5
6
7
8
9
for i=1,5,1 do 
print(i)
end
--输出
1
2
3
4
5
0%