示例工程 06-control-flow-and-truthiness

06-control-flow-and-truthiness/01-goto-continue/main.lua

lua · 0.4 KB

-- @runtimes lua54 luajit
--- 演示:用 goto continue 模拟 continue。
 
local M = {}
 
--- @return table { kept = table }
function M.run(log)
	log = log or print
	local kept = {}
	for i = 1, 6 do
		if i % 2 == 0 then
			goto continue
		end
		kept[#kept + 1] = i
		::continue::
	end
	log(("kept=%s"):format(table.concat(kept, ",")))
	return { kept = kept }
end
 
if not ... then
	M.run()
end
 
return M