Lua 中得for while

来源:互联网 发布:淘宝网运动服女装套装 编辑:程序博客网 时间:2024/05/17 02:24

LUA中的for循环改成while和repeat怎么改

for i=1,5 doprint(“i is now “ .. i)    if i < 2 then           print(“small”)       elseif i < 4 then           print(“medium”)       else           print(“big”)       end end
i = 1while i <= 5 doprint("i is now " .. i)    if i < 2 thenprint("small")    elseif i < 4 thenprint("medium")    elseprint("big")    endi = i + 1end---i = 1repeatprint("i is now " .. i)    if i < 2 thenprint("small")    elseif i < 4 thenprint("medium")    elseprint("big")    endi = i + 1until i > 5



在Lua中for语句跟其他语言的for类似,语法上有一点点区别。Lua的for语句有两种:数字型和泛型

数字型:

[plain] view plaincopyprint?
  1. for var=exp1,exp2,exp3 do  
  2.     <执行体>  
  3. end  
var从exp1变化到exp2,每次变化以exp3为步长递增var,并执行一次“执行体”。exp3是可选的,如果不指定,默认为1。例如

[ruby] view plaincopyprint?
  1. for i=1,f(x) do print(i) end  
  2. for i=10,1,-1 do print(i) end  

0 0
原创粉丝点击