通常,我的目标是模拟 Lua 中不存在的 continue 语句。我已经阅读了一些关于使用 goto 执行此操作的线程。我试过:
for i=0, 9, 1 do
if i<5 then goto skip end
print(i)
::skip::
end
Run Code Online (Sandbox Code Playgroud)
它提出了“lua:test.lua:2:'='预期在'跳过'附近”
有什么解决方法吗?提前致谢
您可以continue使用repeat .... until true和break(适用于 Lua 5.1+)的组合来模拟(在一定程度上):
for i=0, 9, 1 do repeat
if i<5 then break end
print(i)
until true end
Run Code Online (Sandbox Code Playgroud)
请注意,它使break行为为continue,因此您不能以其正常含义使用它。有关更多详细信息和替代解决方案,请参阅此 SO 问题。