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

(编辑:jimmy 日期: 2024/10/5 浏览:2)

 在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

一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。