Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Questions on the 'break' keyword?

Asked by
RedCombee 585 Moderation Voter
9 years ago

Does the 'break' keyword only break the loop that it is in? (In other words, if I have a loop inside another loop, does 'break' break both loops or just the one it's in?)

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

break will only exit the immediate loop.

There isn't a way to break up multiple levels of loops directly (you would need to set a variable and break again in the next loop)


return will break through all loops and exit the current function.


Lua 5.2 includes labeled gotos, which can emulate the functionality of breaking up arbitrarily high.

The practice of using GOTOs at all is fairly contested in the modern world. Historically, GOTOs have been a poor solution in comparison to other options; in higher level languages their evilness fades somewhat so many modern authors argue that there is nothing wrong with using GOTO anymore.

Nevertheless, ROBLOX has not yet upgraded to Lua 5.2, so in ROBLOX, that is not available to you.

Ad

Answer this question