欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年05月28日 11:51:16   投稿:goldensun  
這篇文章主要介紹了詳解Lua中if ... else語句的使用方法,是Lua入門學習中的基礎(chǔ)知識,需要的朋友可以參考下

 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程序設(shè)計語言假定布爾true和非零值的任意組合作為true,以及它是否是布爾假或零,則假定為false值。但應(yīng)當注意的是,在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)生以下結(jié)果。

復制代碼 代碼如下:
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)生以下結(jié)果。

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

相關(guān)文章

  • Lua中使用元表(metatable)執(zhí)行算術(shù)類元方法實例

    Lua中使用元表(metatable)執(zhí)行算術(shù)類元方法實例

    這篇文章主要介紹了Lua中使用元表(metatable)執(zhí)行算術(shù)類元方法實例,本文給出了加法、減法、乘法、除法、相反數(shù)、取模等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • Lua和C++的通信流程代碼實例

    Lua和C++的通信流程代碼實例

    這篇文章主要介紹了Lua和C++的通信流程代碼實例,本文是上一篇的DEMO,本文用代碼講解Lua和C++之間的通信,需要的朋友可以參考下
    2014-09-09
  • Lua中調(diào)用函數(shù)使用點號和冒號的區(qū)別

    Lua中調(diào)用函數(shù)使用點號和冒號的區(qū)別

    這篇文章主要介紹了Lua中調(diào)用函數(shù)使用點號和冒號的區(qū)別,本文涉及了Lua中面向?qū)ο蟮囊恍┑闹R,并給出了一個簡單的類代碼實例,需要的朋友可以參考下
    2014-09-09
  • Lua中的源代碼預(yù)編譯淺析

    Lua中的源代碼預(yù)編譯淺析

    這篇文章主要介紹了Lua中的源代碼預(yù)編譯淺析,Lua確實允許在運行源代碼之前,將源代碼預(yù)編譯成一種中間形式(類比Python的.pyc),需要的朋友可以參考下
    2014-09-09
  • Lua中table里內(nèi)嵌table的例子

    Lua中table里內(nèi)嵌table的例子

    這篇文章主要介紹了Lua中table里內(nèi)嵌table的例子,本文同時講解了如何訪問內(nèi)嵌table方法的例子,需要的朋友可以參考下
    2015-04-04
  • 最新評論