通用迴圈
for 迴圈的通用形式使用三個引數:
- 在需要下一個值時呼叫的迭代器函式。它接收不變狀態和控制變數作為引數。返回 
nil訊號終止。 - 在不變的狀態是,迭代期間不發生變化的值。它通常是迭代器的主題,例如表,字串或使用者資料。
 - 的控制變數代表用於迭代的初始值。
 
我們可以編寫一個 for 迴圈來使用下一個函式迭代表中的所有鍵值對。
local t = {a=1, b=2, c=3, d=4, e=5}
-- next is the iterator function
-- t is the invariant state
-- nil is the control variable (calling next with a nil gets the first key)
for key, value in next, t, nil do
  -- key is the new value for the control variable
  print(key, value) 
  -- Lua calls: next(t, key)  
end