詳解Lua中的if語句的使用方法
if語句由一個或多個語句組成一個布爾表達式。
語法
Lua編程語言的if語句語法是:
then
--[ statement(s) will execute if the boolean expression is true --]
end
如果布爾表達式的計算結果為代碼的if語句為true,那么塊將被執(zhí)行。如果if語句的末尾(右大括號后)布爾表達式計算為false,那么第一組代碼將被執(zhí)行。
Lua程序設計語言假定布爾true和非零值的任意組合作為true,以及它是否是布爾假或零,則假定為false值。但應當注意的是,在Lua零值也會被視為true。
示例:
a = 10;
--[ check the boolean condition using if statement --]
if( a < 20 )
then
--[ if condition is true then print the following --]
print("a is less than 20" );
end
print("value of a is :", a);
當建立和運行上面代碼,它會產生以下結果。
value of a is : 10
相關文章
Lua中的元表(metatable)、元方法(metamethod)詳解
這篇文章主要介紹了Lua中的元表(metatable)、元方法(metamethod)詳解,本文對它做了詳細講解,并給出實例來說明,需要的朋友可以參考下2014-09-09