if(布爾表達式)
then
--[ 在布爾表達式為 true 時執(zhí)行的語句 --]
end
案例:test3.lua
i = 0 ; --定義一個變量i,并初始化為0
if i 5 --如果i 小于 5
then
while(true) --此時做循環(huán)加1
do
i = i+1 ;
print("i:",i);
if i == 5 --如果i 等于 5
then
break ; --退出循環(huán)
end
end
end
解釋運行: lua test3.lua
結果:
i: 1
i: 2
i: 3
i: 4
i: 5
2、if else語句
if(布爾表達式)
then
--[ 布爾表達式為 true 時執(zhí)行該語句塊 --]
else
--[ 布爾表達式為 false 時執(zhí)行該語句塊 --]
end
案例:test4.lua
num = 3 ;
if num 0
then
print("num 比 0小!");
else
print("num 比 0大!");
end