Lua 簡單基礎語法

2021-10-10 07:42:17 字數 3271 閱讀 4319

print

("hello world"

)

--表示注釋

print

("--print表示控制台輸出"

)

--[[

這樣表示多行注釋

--]]

---[[

小技巧:開頭處多加乙個-表示,取消注釋

--]]

1、變數的資料型別,取決於賦值的型別

2、多變數的同時賦值,型別也可以不一致

--預設全域性變數,變數型別無需宣告

a =1

print

(a)--輸出 1

--local 區域性變數、私有變數

local b =

2print

(b)--輸出 2

--多變數的同時賦值,型別也可以不一致

a,b,c =1,

false

,"c"

print

(a,b,c)

--輸出 1 false c

a,b =1,

false

,"c"

print

(a,b)

--輸出 1 false

a,b =

1print

(a,b)

--輸出 1 nil

nil 刪除作用,記憶體中銷毀

a =

1print

(a)--輸出 1

--置空, 不要某個物件(刪除時),直接nil

a =nil

print

(a)--輸出 nil

--nil 刪除作用,記憶體中銷毀

tab =

print

(tab.key1)

print

(tab.key2)

tab =

nil;

print

(tab.key1)

--這裡報錯,為什麼

print

(tab.key2)

--因為nil,tab物件已經在記憶體中被刪除了

lua 中有 8 個基本型別分別為:

nil、boolean、number、string、userdata、function、thread 和 table。

thread(執行緒):

在 lua 裡,最主要的執行緒是協同程式(coroutine)

userdata(自定義型別):

userdata 是一種使用者自定義資料,用於表示一種由應用程式或 c/c++ 語言庫所建立的型別,可以將任意 c/c++ 的任意資料型別的資料(通常是 struct 和 指標)儲存到 lua 變數中呼叫。

print

(type

("hello world"))

--string

print

(type

(print)

)--function

print

(type

(3.1415926))

--number

print

(type

(true))

--boolean

print

(type

(nil))

--nil

print

(type

(type)

)--function

print

(type

(type

(a))

)--此時 a 沒有定義,所以是nil型別,type(nil)的type()是string

print

(type()

)--table

false和nil 都是false,其他都是true

if

false

ornil

then

print

("false 和 nil 至少有乙個是 true"

)--輸出

else

print

("false 和 nil 都是false"

)endif1

then

print

("number 是 true"

)--輸出

else

print

("number 是 false"

)end

if"s"

then

print

("string 是 true"

)--輸出

else

print

("string 是 false"

)end

整數、小數、科學計數法 都是number

--e代表10 +1代表1次方 -1代表-1次方

print

(type(2

))--number

print

(type

(2.2))

--number

print

(type

(0.2))

--number

print

(type

(2e+1))

--number

print

(type

(0.2e-1))

--number

print

(type

(7.8263692594256e-06))

--number

--[[

\a 表示響鈴

\b 表示退格

\f 表示提供**

\n 表示換行

\』表示 』

\」 表示 「

\r 表示回車

\\ 表示\

\t 表示水平tab,乙個製表轉義符(tab)

--]]

print

("***\ayyy"

)print

("***\byyy"

)print

("***\fyyy"

)print

("***\nyyy"

)print

("***\ryyy"

)print

("***\tyyy"

)print

("今天天氣好晴朗處處好風光。。\b"

)

lua 基礎語法

print hello wc hahaha a 10 全域性變數 local b tostring a.2 區域性變數 dofile test.lua print type b local c nil local d wwe dad print string.sub d,2,3 獲取子串 lua從1...

Lua 基礎語法總結

第乙個 lua 程式 這是 lua 單行注釋的寫法 print start 多行注釋寫法 lua 區分大小寫 lua 的關鍵字 and break do else elseif end false for function if in local nil not or repeat return t...

lua語法 基礎篇

1.注釋 單行注釋 類似於c 的 多行注釋 類似於c 的 2.語句 分隔符 分號或者空格,一般多行寫一起,建議用分號 語句塊 do end 賦值語句 a,b,c,d 1,2,3,4 global variables local a,b,c 1,2,3 local varialbes a,b b,a ...