Lua中if語句嵌套的使用教程

來源:互聯網
上載者:User

Lua中if語句嵌套的使用教程

   這篇文章主要介紹了Lua中if語句嵌套的使用教程,是Lua入門學習中的基礎知識,需要的朋友可以參考下

  在Lua編程內嵌if-else語句,這意味著可以使用一個 if 或 else if 在另一個語句if或else if 語句中。

  文法

  if語句的嵌套文法如下:

  代碼如下:

  if( boolean_expression 1)

  then

  --[ Executes when the boolean expression 1 is true --]

  if(boolean_expression 2)

  then

  --[ Executes when the boolean expression 2 is true --]

  end

  end

  您可以嵌套else if...else 以類似if語句的方式。

  例子:

  代碼如下:

  --[ local variable definition --]

  a = 100;

  b = 200;

  --[ check the boolean condition --]

  if( a == 100 )

  then

  --[ if condition is true then check the following --]

  if( b == 200 )

  then

  --[ if condition is true then print the following --]

  print("Value of a is 100 and b is 200" );

  end

  end

  print("Exact value of a is :", a );

  print("Exact value of b is :", b );

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

  代碼如下:

  Value of a is 100 and b is 200

  Exact value of a is : 100

  Exact value of b is : 200

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.