lua中模拟“continue”的几种方法

  1. 使用repeat循环包住需要要continue跳过的代码,使用break跳出循环, 需要注意的是,lua中的repeat语句,在循环条件为真的时候退出
    1 for i = 1, 10 do
    2     repeat
    3         if i%2 == 0 then
    4             break
    5         end
    6         print(i)
    7         break
    8     until true 
    9 end
  2. 使用while循环包住需要continue跳过的代码, 使用break跳出循环
    1 for i = 1, 10 do
    2     while true do
    3         if i%2 == 0 then
    4             break
    5         end
    6         print(i)
    7         break
    8     end
    9 end
  3. 在lua5.2版本之后,可以使用goto语句来模拟
    1 for i = 1, 10 do
    2     if i%2 == 0 then
    3         goto continue
    4     end
    5     print(i)
    6     ::continue::
    7 end

     

文章来自:https://www.cnblogs.com/elspsy/p/8797549.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3