濮阳杆衣贸易有限公司

主頁 > 知識庫 > 詳解Lua中if ... else語句的使用方法

詳解Lua中if ... else語句的使用方法

熱門標簽:??谥悄苷Z音電銷機器人好用嗎 免費門店地圖標注注冊入駐 昆明電話外呼系統(tǒng)好么 電話機器人每天搜索多少次 艾比利外呼系統(tǒng) 外呼系統(tǒng)一天耗費多少流量 陜西便宜電銷機器人軟件 杞縣地圖標注app 衡水外呼線路解決

 if 語句后面可以跟一個可選的else語句,當布爾表達式為假該語句執(zhí)行。
語法

在Lua編程語言中的if ... else語句的語法是:

復制代碼 代碼如下:
if(boolean_expression)
then
   --[ statement(s) will execute if the boolean expression is true --]
else
   --[ statement(s) will execute if the boolean expression is false --]
end

如果布爾表達式的值為true,那么if代碼塊將被執(zhí)行,否則else代碼塊將被執(zhí)行。

Lua程序設計語言假定布爾true和非零值的任意組合作為true,以及它是否是布爾假或零,則假定為false值。但應當注意的是,在Lua零值被視為true。
 例如:

復制代碼 代碼如下:
--[ local variable definition --]
a = 100;
--[ check the boolean condition --]
if( a 20 )
then
   --[ if condition is true then print the following --]
   print("a is less than 20" )
else
   --[ if condition is false then print the following --]
   print("a is not less than 20" )
end
print("value of a is :", a)

當建立和運行上面的代碼,它會產(chǎn)生以下結果。

復制代碼 代碼如下:
a is not less than 20
value of a is : 100

if...else if...else 語句

if語句后面可以跟一個可選的else if ... else語句,這是非常有用的使用,以測試各種條件單個if...else if 語句。

當使用if , else if , else語句有幾點要記住使用:

  •     if 可以有零或一個 else ,但必須在elseif之前。
  •     if 之后可以有零到很多else if在else之前。
  •     一旦一個else if成功,其它的elseif將不會被測試。

語法

if...else if...else...else語句在Lua編程語言的語法是:

復制代碼 代碼如下:
if(boolean_expression 1)
then
   --[ Executes when the boolean expression 1 is true --]

else if( boolean_expression 2)
   --[ Executes when the boolean expression 2 is true --]

else if( boolean_expression 3)
   --[ Executes when the boolean expression 3 is true --]
else
   --[ executes when the none of the above condition is true --]
end

例如:

復制代碼 代碼如下:
--[ local variable definition --]
a = 100

--[ check the boolean condition --]
if( a == 10 )
then
   --[ if condition is true then print the following --]
   print("Value of a is 10" )
elseif( a == 20 )
then  
   --[ if else if condition is true --]
   print("Value of a is 20" )
elseif( a == 30 )
then
   --[ if else if condition is true  --]
   print("Value of a is 30" )
else
   --[ if none of the conditions is true --]
   print("None of the values is matching" )
end
print("Exact value of a is: ", a )

當建立和運行上面的代碼,它會產(chǎn)生以下結果。

復制代碼 代碼如下:
None of the values is matching
Exact value of a is: 100

您可能感興趣的文章:
  • Lua檢測數(shù)組(tabble)中是否包含某個值
  • Lua中遍歷文件操作代碼實例
  • Linux下編寫Lua擴展so文件和調(diào)用方法實例

標簽:泰安 營口 宿遷 昌都 南京 臨滄 西寧

巨人網(wǎng)絡通訊聲明:本文標題《詳解Lua中if ... else語句的使用方法》,本文關鍵詞  詳解,Lua,中,...,else,語句,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《詳解Lua中if ... else語句的使用方法》相關的同類信息!
  • 本頁收集關于詳解Lua中if ... else語句的使用方法的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    七台河市| 石嘴山市| 忻城县| 高州市| 丰台区| 大姚县| 五指山市| 桐柏县| 诏安县| 申扎县| 香港| 浏阳市| 凤阳县| 监利县| 黄浦区| 墨脱县| 永济市| 玛纳斯县| 塔城市| 汤阴县| 福建省| 抚州市| 弋阳县| 固安县| 新余市| 宿州市| 颍上县| 仁寿县| 琼海市| 喜德县| 镶黄旗| 勃利县| 上杭县| 邯郸县| 固始县| 延长县| 革吉县| 东台市| 叶城县| 京山县| 湘潭市|