您的位置:首页 > 编程语言 > Lua

lua基础语法

2016-06-25 10:52 411 查看

if(0)
02.then
03.    print("0 is true")
04.end
05.
06.a = 1
07.
08.if(a)
09.then
10.    print("1 is false")
11.else
12.    print("a is true")
13.end
14.
15.a = 100
16.if(a == 50)
17.then
18.    print("the value of a is 50")
19.elseif(a == 100)
20.then
21.    print("the value of a is 100")
22.else
23.    print("the value of a is not matched")
24.end
25.
26.-- test loop of 'for'
27.
28.days = {'sunday','monday','tuesday','wednesday','thursday','friday','saturday'}
29.for i,v in ipairs(days) do print(i,v) end
30.
31.-- test loop of 'repeat'
32.a = 10
33.repeat
34.    print('the value of a is ',a)
35.    a = a + 1
36.until(a > 15)
37.
38.-- test for function in lua
39.myprint = function(param)
40.    print("this is print fun ###",param,"###")
41.end
42.
43.function add (num1,num2,functionprint)
44.    result = num1 + num2
45.    -- call function from passed
46.    functionprint(result)
47.end
48.
49.myprint(100)
50.
51.add(10,20,myprint)
52.
53.-- test for the number of args is not sure
54.function average(...)
55.    result = 0
56.    local arg = {...}
57.    for i,v in ipairs(arg)
58.    do
59.        result = result + v
60.    end
61.    print('the number of arg is'..#arg..'size')
62.    return result/#arg
63.end
64.
65.print("the average is ",average(10,5,6,20,2))

运行结果如下:
<img alt="" src="http://img.blog.csdn.net/20160324180820443?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: