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

 更新时间:2015年05月28日 11:51:16   投稿:goldensun  
这篇文章主要介绍了详解Lua中if ... else语句的使用方法,是Lua入门学习中的基础知识,需要的朋友可以参考下

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

在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代码块将被执行,否则else代码块将被执行。

Lua程序设计语言假定布尔true和非零值的任意组合作为true,以及它是否是布尔假或零,则假定为false值。但应当注意的是,在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)

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

复制代码 代码如下:
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 )

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

复制代码 代码如下:
None of the values is matching
Exact value of a is: 100

相关文章

  • Lua中..和#运算符的使用方法

    Lua中..和#运算符的使用方法

    这篇文章主要介绍了Lua中..和#运算符的使用方法,是Lua入门学习中的基础知识,需要的朋友可以参考下
    2015-05-05
  • phpredis执行LUA脚本示例代码

    phpredis执行LUA脚本示例代码

    这篇文章主要给大家介绍了关于phpredis执行LUA脚本的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • Lua的迭代器使用中应该避免的问题和技巧

    Lua的迭代器使用中应该避免的问题和技巧

    这篇文章主要介绍了Lua的迭代器使用中应该避免的问题和技巧,本文介绍了避免创建闭合函数、利用恒定状态创造更多变量、不需要for循环的迭代器等内容,需要的朋友可以参考下
    2014-09-09
  • Lua 学习笔记之C API 遍历 Table实现代码

    Lua 学习笔记之C API 遍历 Table实现代码

    这篇文章主要介绍了Lua 学习笔记之C API 遍历 Table实现代码,需要的朋友可以参考下
    2014-12-12
  • Lua协程(coroutine)程序运行分析

    Lua协程(coroutine)程序运行分析

    这篇文章主要介绍了Lua协程(coroutine)程序运行分析,本文讲解分析了一段lua 协程代码是如何运行的,需要的朋友可以参考下
    2015-05-05
  • 简单谈谈lua和c的交互

    简单谈谈lua和c的交互

    要理解Lua和C++交互,首先要理解Lua堆栈。简单来说,Lua和C/C++语言通信的主要方法是一个无处不在的虚拟栈。栈的特点是先进后出。
    2016-01-01
  • Lua中的基本数据类型详细介绍

    Lua中的基本数据类型详细介绍

    这篇文章主要介绍了Lua中的基本数据类型详细介绍,本文详细的讲解了Lua中的8种基本数据类型,需要的朋友可以参考下
    2014-09-09
  • Lua教程(十七):C API简介

    Lua教程(十七):C API简介

    这篇文章主要介绍了Lua教程(十七):C API简介,本文讲解了基础知识、栈、C API中的错误处理、Lua调用C程序、C程序调用Lua代码的错误处理等内容,需要的朋友可以参考下
    2015-04-04
  • Lua中实现递归删除一个文件夹

    Lua中实现递归删除一个文件夹

    这篇文章主要介绍了Lua中实现递归删除一个文件夹,本文给出了使用C++和使用纯LUA两种方式实现,需要的朋友可以参考下
    2015-01-01
  • Lua编程示例(三):稀疏表、双端队列、格式化输出、表和循环表的格式化输出

    Lua编程示例(三):稀疏表、双端队列、格式化输出、表和循环表的格式化输出

    这篇文章主要介绍了Lua编程示例(三):稀疏表、双端队列、格式化输出、表和循环表的格式化输出,本文直接给出实例代码,代码中包含详细注释,需要的朋友可以参考下
    2015-07-07

最新评论