Lua 中的 if...else语句函数

首页 / Lua入门教程 / Lua 中的 if...else语句函数

if 语句后可以跟可选的 else 语句,该语句在布尔表达式为false时执行。

if...else statement - 语法

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

if...else statement - 流程图

Lua if...else statement

if...else statement - 示例

--[ 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)

当您构建并运行以上代码时,它将产生以下输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/lua/if-else-statement-in-lua.html

来源:LearnFk无涯教程网

a is not less than 20
value of a is :	100

if...else statement - else if ... else语句

if 语句后可以跟可选的 else if ... else 语句,这对于使用单个if ... else if语句测试各种条件非常有用。

Lua编程语言中的 if ... else if ... else 语句的语法是-

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

if...else statement - 示例

--[ 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 )

当您构建并运行以上代码时,它将产生以下输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/lua/if-else-statement-in-lua.html

来源:LearnFk无涯教程网

None of the values is matching
Exact value of a is:	100

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

硅谷产品实战36讲 -〔曲晓音〕

浏览器工作原理与实践 -〔李兵〕

苏杰的产品创新课 -〔苏杰〕

架构实战案例解析 -〔王庆友〕

Selenium自动化测试实战 -〔郭宏志〕

React Hooks 核心原理与实战 -〔王沛〕

云计算的必修小课 -〔吕蕴偲〕

高并发系统实战课 -〔徐长龙〕

Dubbo源码剖析与实战 -〔何辉〕

好记忆不如烂笔头。留下您的足迹吧 :)