通过lua自带例子学习lua 04

-- Example 16 -- if statement.

-- Simple if.

a=1
if a==1 then
print ("a is one")
end


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

a is one

-- Example 17 -- if else statement.

b="happy"
if b=="sad" then
print("b is sad")
else
print("b is not sad")
end


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

b is not sad

-- Example 18 -- if elseif else statement

c=3
if c==1 then
print("c is 1")
elseif c==2 then
print("c is 2")
else
print("c isn't 1 or 2, c is "..tostring(c))
end


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

c isn't 1 or 2, c is 3

-- Example 19 -- Conditional assignment.

-- value = test and x or y

a=1
b=(a==1) and "one" or "not one"
print(b)

-- is equivalent to
a=1
if a==1 then
b = "one"
else
b = "not one"
end
print(b)


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

one
one

-- Example 20 -- while statement.

a=1
while a~=5 do -- Lua uses ~= to mean not equal
a=a+1
io.write(a.." ")
end


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

2 3 4 5

 

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

导航