What does the goto keyword do?

What does the goto keyword do?

goto is a key word in Regular Lua that allows you to go back to specific points of your code, like for example:

:: start :: -- format

-- code here

if Condition then
    goto start -- goes back to the 'start' part of the code
end

:: ending ::

Like a Loop.

However, it does not Exist in Luau because of Compatability

2 Likes

goto is a low level construct that dictates looping and such. It’s used primarily in C or assembly language contexts (basically a direct relation to jump statements) and really isn’t a good idea for use in high level programming like Lua. You have for loops and while loops for a reason.

2 Likes

The “goto” keyword is used to create a jump in the program’s execution flow.
When the keyword is used, the program will immediately jump to a specific label in the code.

Here’s an example:

local i = 0

::loop::
i = i + 1
print(i)
if i < 10 then
   goto loop
end

In this example, the code creates a loop that prints the values of “i” from 1 to 10. The “goto loop” statement jumps back to the “loop” label, causing the loop to continue until the “if” statement condition is no longer true.
It’s worth noting that the use of “goto” can make code more difficult to read and maintain, so it should be used with caution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.