lua學習筆記4之語法

2021-06-17 17:10:44 字數 1259 閱讀 3297

a,b,c = 0,1;	--c為nil

print(a,b,x);

a,b,c = a+1,b+1,b+1; --怎麼會是1,2,2呢 b不是+1 =2了嗎 c= b+1 = 3啊

print(a,b,c);

--------迴圈--------------

x =10;

local i=1;

while i10 then

local x;

x=20;

print(x+2);

else

print(x);

end-------------for------------------

for i =1,10,1 do --初試為1 最大為<=10, 步長為1

print(i);

endfor i =10,1,-1 do --初試為1 最大為》=1, 步長為-1

print(i);

end-------------------------------

print(os.date()); --列印日期

function f(a,b)

return a or b; --可以返回多個值

endprint(f(3));

print(f(3,4));

s,e = string.find("hello world","world");--可以返回多個值find 函式返回2個值 分別起始和結束的值

print(s,e);

------------------------------------

a =function max(b)

local index = 1;

local value = b[index];

for i ,val in ipairs(b) do --i會遞增陣列最大,這樣可以遍歷整個陣列

print(i);

print(val);

if val >value then

value = val;

index = i;

endend

return i,value;

endprint(max(a));

--------------do..while----------------

i =10;

repeat

print(i);

i=i-1;

until i<1;

Lua學習筆記 基本語法

print hello world 輸出的結果為 hello world print hello world 這是單行注釋 print hello world 使用兩個減號與兩個中括號來進行多行注釋並以中括號結尾 lua表示符用來定義乙個變數,並且表示符的選取有相關規則。表示符一般使用字母或者下劃線...

Lua學習之基本語法

堅持 成長 每日一篇 lua支援對多個變數同時賦值,當變數個數和值的個數不一致時,lua 會一直以變數個數為基礎採取以下策略。a.變數個數 值的個數 按變數個數補足 nil b.變數個數 值的個數 多餘的值會被忽略 例如 a,b,c 0,1 print a,b,c 0 1 nil a,b a 1,b...

lua 語法筆記

建立檔案 helloworld.lua,內容如下 print hello world 執行指令碼 lua helloworld.lua 單行注釋 兩個減號 注釋 多行注釋 多行注釋 多行注釋 索引訪問 t i t.i 當索引為字串型別時的一種簡化寫法 gettable event t,i 採用索引訪...