Infinite loop
This is a common programming pattern using a while loop to create loops that run infinitely until an external condition is met.
This is done by setting the condition to (true) - this is always true, so the loop continues to execute indefinitely.Â
while (true) do
-- code executes infinitley
end
This is often used in conjunction with if statements to loop until a specific condition is checked within the loop and exited if false.
For example, the following function will loop infinitely until counter == 5.
function countNumbers()
local counter = 1
while (true) do
counter = counter + 1
trace(counter)
if counter == 5 then
return counter -- Exit the function and return the value
end
end
end
Try it out for yourself in the Developing in the Translator.