Created
September 27, 2023 19:05
-
-
Save arkenidar/2e6f66e369eb1dfe2062c2268cd0903c to your computer and use it in GitHub Desktop.
not-wrong, useful and common use cases of "Lua's go-to"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ "break" as key-word breaks only the inner-most loop when nesting loops ]] | |
--[[ "goto" to exit/break nested loops ]] | |
--[[ "goto" to emulate "continue" and "redo", besides "break" key-word]] | |
--[[ other cases possible ]] | |
--[[ limitation: in Lua you can "go to" only to local "goto-labels", local to the scope/function]] | |
local redo_limit = false | |
for i=1,5 do | |
:: redo :: -- redo as a label | |
if i==1 then | |
goto continue -- "continue" EXPLANATION: skips, continue without | |
end | |
print(i) | |
if i==2 and not redo_limit then | |
redo_limit = true | |
goto redo -- "redo" EXPLANATION: do again, re-do | |
end | |
-- break as key-word | |
if i==4 then break end -- "break" EXPLANATION: ends the loop, exit the loop (innermost loop when nesting loops) | |
:: continue :: -- continue as a label | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment