/
Infinite loop
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.
, multiple selections available,
Related content
while loop
while loop
More like this
for loop
for loop
More like this
Loops
More like this
Recursion
Recursion
More like this
Javascript Closure
Javascript Closure
More like this
for in loop
for in loop
More like this