• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Lua > 详解Lua中if ... else语句的使用方法

详解Lua中if ... else语句的使用方法

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了lua if else,lua if else if,lua else,lua源码详解与应用,lua 详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

 if 语句后面可以跟一个可选的else语句,当布尔表达式为假该语句执行。
语法

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

then
   --[ statement(s) will execute if the boolean expression is true --]
else
   --[ statement(s) will execute if the boolean expression is false --]
end</div>

如果布尔表达式的值为true,那么if代码块将被执行,否则else代码块将被执行。

Lua程序设计语言假定布尔true和非零值的任意组合作为true,以及它是否是布尔假或零,则假定为false值。但应当注意的是,在Lua零值被视为true。
 例如:

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)</div>

当建立和运行上面的代码,它会产生以下结果。

value of a is : 100</div>

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编程语言的语法是:

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</div>

例如:

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 )</div>

当建立和运行上面的代码,它会产生以下结果。

Exact value of a is: 100</div>

</div>

您可能想查找下面的文章:

  • Lua中if语句嵌套的使用教程
  • 详解Lua中if ... else语句的使用方法
  • 详解Lua中的if语句的使用方法

相关文章

  • 使用lua实现php的var_dump()函数功能
  • Lua中使用.和:调用函数的区别
  • 深入解读Lua中迭代器与泛型for的使用
  • Lua学习笔记之类型与值
  • Lua协同程序(COROUTINE)运行步骤分解
  • 深入探究Lua中的解析表达式
  • Lua在windows下的安装及环境配置
  • Lua极简入门指南(一):函数篇
  • Lua中的元方法__newindex详解
  • Lua脚本实现递归删除一个文件夹

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Lua中实现php的strpos()以及strrpos()函数
    • lua日志文件处理代码
    • Lua中的函数相关知识点整理汇总
    • Lua极简入门指南(一):基础知识篇
    • Lua字符串库中的几个重点函数介绍
    • Lua中实现递归删除一个文件夹
    • Lua中类的实现
    • 详解Lua中的数组概念知识
    • Lua调用自定义C模块
    • Lua中的table学习笔记

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有