通过lua自带例子学习lua 05

-- Example 21 -- repeat until statement.

a=0
repeat
a=a+1
print(a)
until a==5


-------- Output ------

1
2
3
4
5

-- Example 22 -- for statement.

-- Numeric iteration form.

-- Count from 1 to 4 by 1.
for a=1,4 do io.write(a) end

print()

-- Count from 1 to 6 by 3.
for a=1,6,3 do io.write(a) end


-------- Output ------

1234
14

-- Example 23 -- More for statement.

-- Sequential iteration form.

for key,value in pairs({1,2,3,4}) do print(key, value) end


-------- Output ------

1 1
2 2
3 3
4 4

-- Example 24 -- Printing tables.

-- Simple way to print tables.

a={1,2,3,4,"five","elephant", "mouse"}

for i,v in pairs(a) do print(i,v) end


-------- Output ------

1 1
2 2
3 3
4 4
5 five
6 elephant
7 mouse

-- Example 25 -- break statement.

-- break is used to exit a loop.

a=0
while true do
a=a+1
if a==10 then
break
end
end

print(a)


-------- Output ------

10

 

posted on 2013-04-06 15:41  cv_ml_张欣男  阅读(152)  评论(0编辑  收藏  举报

导航