目录:1.输出2.注释3.控制语句4.赋值语句5.运算符6.关键字7.变量类型8.其余函数
原文地址http://blog.csdn.net/dingkun520wy/article/details/49930543spa
1.输出.net
print("Hello world")线程
2.注释指针
单行注释中,连续两个减号"--"表示注释code
多行注释中,由"--[["表示注释开始,而且一直延续到"]]"为止blog
3.控制语句
字符串
(If)get
if 条件 then ... elseif 条件 then ... else ... endit
实例
if 1+1=2 then print("true") elseif 1+2~=3 then print("true") else print("false") end
( While )
while 条件 do ... end
实例
while 1+1~=2 do print("true") end
(Repeat)
repeat ... until 条件
实例
repeat print("Hello") until 1+1~=0
(For)
for 变量=初值, 终点值, 步进 do ... end
实例
for i = 1, 10, 2 do print(i) end
for 变量 1, 变量 2, ... 变量 n in 表或枚举函数 do ... end
实例
for a,b in mylist do print(a, b) end
4.赋值语句
Lua中赋值是能够同时给多个变量赋值的
如:
a,b,c = 1,2,3
当左边变量少时舍弃
当右边变量少时补nil
5.运算符
运算符优先级,从低到高顺序以下:
or 逻辑或 a or b:若是 a 为 true,则返回 a;不然返回 b
and 逻辑与 a and b:若是 a 为 false,则返回 a;不然返回 b
< > <= >= ~= ==比较运算,~=为不等于
.. 字符串连接 print("Hello world".."Lua")
+ - 数值运算
* / % 数值运算
not # - 一元运算: not是取反,#()区长度,-负数
^ 数值运算:指数乘方运算
6.关键字
and |
break |
do |
else |
elseif |
|
|
|
|
|
|
|
end |
false |
for |
function |
if |
|
|
|
|
|
|
|
in |
local |
nil |
not |
or |
|
|
|
|
|
|
|
repeat |
return |
then |
true |
until |
while |
7.变量类型
nil:空值,全部没有使用过的变量,都是 nil。nil 既是值,又是类型。
Boolean:布尔值,只有两个有效值:true 和 false。在Lua中只有false和nil是false,其余都是true
Number:数值,在 Lua 里,数值是实数。
String:字符串,若是你愿意的话,字符串是能够包含"\0"字符的(这和 C 语言老是以"\0"结尾是不同的)
详见 http://blog.csdn.net/dingkun520wy/article/details/50434530
Table:关系表类型,这个类型功能比较强大,详见http://blog.csdn.net/dingkun520wy/article/details/50231603
Function:函数类型,函数也是一种类型,也就是说,全部的函数,它自己就是一个变量
例如:
function add(a,b)
return a+b
end
详见:http://blog.csdn.net/dingkun520wy/article/details/50275387
Userdata:这个类型专门用来和 Lua 的宿主打交道的。宿主一般是用 C 和 C++来编写的,是宿主的任意数据类型,经常使用的有 Struct 和指针。
Thread:线程类型,在 Lua 中没有真正的线程。Lua 中能够将一个函数分红几部份运行
7.其余
在 Lua 中,一切都是变量,除了关键字。
在 Lua 中,语句之间能够用分号";"隔开,也能够用空白隔开。
在 Lua 中,for 的循环变量老是只做用于 for 的局部变量;当省略步进值时,for 循环会使用 1 做为步进值
在 Lua 中,语句块是用 do 和 end 括起来的。
在 Lua 中,变量老是全局变量,除非咱们在前面加上"local"。