July 2018
Beginner
202 pages
5h 42m
English
One of the dangers of loops is an infinite loop. You get into an infinite loop when the condition of the loop never evaluates to false. Because the condition keeps being true, the loop goes on forever. The following code snippet demonstrates a simple infinite loop:
while true do print ("forever")end
A more real-life example of an infinite loop would look like this:
x = 10 -- Initialize a "control" variablewhile x > 0 do -- Boolean condition: x > 0 print ("hello, world") x = x + 1 -- Decrement the "control" variableend
This loop will run forever, since the value of x keeps increasing and will never reach 0.