1. 程式人生 > >lua中模擬“continue”的幾種方法

lua中模擬“continue”的幾種方法

版本 條件 bsp col div then 註意 退出 repeat

  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

lua中模擬“continue”的幾種方法