【Lua】【2】Lua基本的函数和控制结构

来源:互联网 发布:农大网络远程教育 编辑:程序博客网 时间:2024/05/22 03:57
【2】Lua基本的函数和控制结构
  
【1】使用Lua
(1)创建函数
 
hello=function()
print("Hello World")
return   99
end
print(hello())
 


(2)局部函数
 
语法糖 
function  hello()
    print("Hello World")
end
hello()
--//=====================>>lua
hello=function()
print("Hello World")
end
hello()
 
 local  关键字
local  function  hello()
    print("Hello World")
end
hello()
--//==========================>>使local
local  hello=function()
print("Hello World")
end
hello()
 
(3)函数的参数和返回值

将摄氏温度转化为华氏温度
 
convert_c2f=function(celsius)
    local  converted=(celsius*1.8)+32
    return  converted

end
print(convert_c2f(0))
print(convert_c2f(-39))
 



 空参数:
当没有值通过参数传递的时候,参数会获得一个nil 值。因为convert_c2t的第一行尝试将Celsius乘以1.8,所以她产生了错误:一个nil 值不能作为算术表达式的一部分。 
print(convert_c2f())
 

没有返回值:
并不是所有的函数都需要返回值

(4)函数作为lua值
在Lua中每一个函数只是一个简单的Lua 值,它的类型是function.这些值可以被比较(使用==  和~=),绑定到变量名,传递给函数,从函数中返回。或者作为table中的关键字。能以这种方式对待的Lua值被称为头等对象,而支持这种方式的函数被称为头等函数。

 
hello=function  ()  print("Hello World")  end
print(hello ==hello)
hello2=hello
print(hello2==hello)
hello2=function  ()  print("Hello World")  end
print(hello2==hello)
 

创建一个新的函数,并且被绑定到hello2。尽管这个新的函数具有相同的定义和函数体,但事实上却是另外一独立的函数


【2】用if语句进行判断

 (1)一个简单的条件
function  condition_test(num)
    print(":"..num)
    if   num==6   then
        print(":"..num)
    end
end
condition_test(4)
condition_test(6)
 

(2)复杂 的表达式




 
name="lisong"
if  name  then
    print("true")
end
if  type(name)=="string"  then
    print("true")
elseif  name==nil  then
    print("nil")
end
 


(3)扩展的条件语句

if  表达式   then

elseif  表达式  then

elseif  表达式  then

else

end

(4)显示个人问候

 
function   greeting(name)
    if  type(name)=="string"  then
        print(""..name.."")
    elseif  type(name)=="nil"  then
        print("")
    else
        print("")
    end
end
greeting("")
greeting()
greeting(10)
 
\

如果name 的参数是nil ,就以为着没有参数传递给函数

【3】用while 语句来重复动作
语法:
while   表达式   do


end

(1)计算阶乘
定义一个阶乘函数  factorial

 
function  factorial(num)
    local  total=1
    while   (num>1)  do
        total=total*num
        num=num-1
        print("total= "..total.."   nun="..num)
    end
    return   total
end
num=5
print(num..""..factorial(num))
 



 通过递归 实现  1+2+...+num  的值
 
function  digui(num)
    if  num==0  then
        return   0
    else
    return  digui(num-1)+num
    end
end
print(digui(10))
print(digui(100))
 

 


(2)while 和repeat 之间的差异
repeat/until  循环是while 循环的一个变种

repeat 
        body
    
until   <boolean 表达式>

 repeat /until  至少执行一次  


function  factorial2(num)
    local  total=1
    repeat
        total=total*num
        num=num-1
    until   num<1
    return  total
end
print(factorial2(4))
print(factorial2(5))
print(factorial2(-3))
 


【4】 用数值执行for循环

for    varivablename=start_value ,end_value,step_value     do
        --body
 end

varivablename:  标示符,计数变量
start_value:       计数的初始值
end_value:        计数的终止值
step_value:    每次循环之后,计数变量的增量

 
for  i=1,5,1  do
    print(i)
end
print()
for  i=5,1,-1  do
    print(i)
end
 


等同于如下的代码

 
do
    local   i=1
    while  i<=3 do
        print(i)
        i=i+1
    end
end
print()
do
    local  i=3
    while  i>=1  do
        print(i)
        i=i-1
    end
 


(1)for  循环计算阶乘
 
function  factorial3(num)
    local  total=1
    for  i=1num  do
        total=total*i
    end
    return   total
end
print(factorial3(3))

print(factorial3(4))

print(factorial3(5))


 

(2)循环条件的求值

 如下代码并不会造成死循环
upper=3
for  i=1,upper  do
    print(i)
    upper=upper+1
end
 
这个例子没有死循环,因为循环条件表达式只求了一次值
 
(3)循环中的变量作用域

 /////////////////////////////
i=15
for   i=1 ,5  do
    print(i)
end
print(i)
 
最终  i  的值是全局中的  15  
  

///////////////////////////////////
 
--i=15
for   i=1 ,5  do
    print(i)
end
print(i)
 
此时 i  的值为 nil  
 




 ///=========================
upper=10
do
    local  max =0
    for i =1,upper   do
        max=i
    end
    print(max)
end


 
如果要保存控制变量的值,就可以在for 循环前面声明一个局部变量,这样就可以保存你所有需要的数字 

0 0
原创粉丝点击