例如,如果我执行以下操作:
local iStop = 32
for i = 1, iStop do
iStop = iStop + 1
end
循环是否保证只运行 32 次迭代,还是有可能无限运行?
最佳答案
请引用Lua Reference Manual 3.3.5: For statement
for v = e1, e2, e3 do block end
is equivalent to the code:
do local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3) if not (var and limit and step) then error() end var = var - step while true do var = var + step if (step >= 0 and var > limit) or (step < 0 and var < limit) then break end local v = var block end end
...
All three control expressions are evaluated only once, before the loop starts.
循环将运行 32 次。实际的循环限制是循环开始前 iStop
值的副本,无论您在循环中对 iStop
执行什么操作,都不会影响循环限制。
关于for-loop - Lua - 在 for 循环中更改 'stop' 变量的值,它还会正常终止吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62478389/