July 2018
Beginner
202 pages
5h 42m
English
A loop might need to exit mid-execution, before the loop condition could evaluate to false. Or, perhaps there is a certain branch of logic that should exit a loop in case of an error. At any point during the loop's execution, the break keyword stops the execution of a loop immediately, as the following code snippet demonstrates:
x = 0while x < 10 do -- Execute 10 times! print ("x is " .. x) if x == 5 then -- This stops the loop execution at 5 break end x = x + 1end
This code should loop from 0 to 9 and print out the value of x at each step. However, when x is 5, the loop breaks. Because of this break, this code only outputs 0 through 5.