0基礎lua學習(六)控制語句

2021-08-09 18:13:42 字數 1568 閱讀 2137

--demo:省略了c語言的括號

if a>b then

else if a>c then

end--lua中不支援switch case

--demo:

--定義變數

a = 10;

--使用 if 語句

if( a < 20 )

then

-- if 條件為true 時列印以下資訊 --

print("a 小於 20" );

endprint("a 的值為:", a);

console:

a 小於 20

a 的值為:    10

while a<5 do

a= a+1

end

local i =0

repeat

i = i +1

print(i..",")

until i>3

console:

1,

2,3,

4,

--repeat until 中定義的區域性變數作用域,包括了until中的語句

function f(x)

print("function")

return x*2

end--函式或是表示式只會執行一次

for i=1,f(5) do print(i)

endprint("\n".."-------------------")

console:

function

123

4567

8910

--(4-1)數字型for迴圈

--for var = from,to,step do

--end

--數值迴圈

for i=10,1,-1 do

print(i)

end print("\n".."-------------------")

consloe:

1098

7654

321

注意:for迴圈的引數作為表示式;或者函式呼叫,只會呼叫一次

--(4-2)泛型for迴圈

--陣列列印

days =

for i,v in ipairs(days) do

print(v)

end

console:

suanday

monday

tuesday

wednesday

thursday

friday

saturday

lua基礎 四 基本控制語句

lua的控制語句 if then esle end local a223 10 local b223 15 if a223print a223 else print b223 end if then elseif then end while do end 語句 local is 1 while i...

Lua基礎 流程控制語句

lua提供了if語句和if else語句作為流程控制語句,當然,符合c的特點,流程語句之間可以實現巢狀操作,當然流程控制也可以和迴圈體結合進行控制。1 if語句 if 布林表示式 then 在布林表示式為 true 時執行的語句 end案例 test3.lua 2 i 0 定義乙個變數i,並初始化為...

Lua學習之8 控制語句

控制結構的條件表示式結果可以是任何值,lua認為false和nil為假,其他值為真。if conditions then then part end if conditions then then part else else part end if conditions then then par...