Infinite loops

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" variable

while x > 0 do -- Boolean condition: x > 0
print ("hello, world")

x = x + 1 -- Decrement the "control" variable
end

This loop will run forever, since the value of x keeps increasing and will never reach 0.

If you encounter an infinite loop, force stop the execution of the program in any terminal with the keyboard shortcut Ctrl + C, which kills the current process.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.255.87