When a break statement is encountered in a loop, the loop terminates immediately, and the program control continues after the next loop statement.
If you are using a nested loop (that is, another loop inside a loop), the break statement stops the execution of the innermost loop and begins the following segment of the next line of code that executes.
Grammar
The Lua break statement syntax is as follows:
Copy Code code as follows:
Example:
Copy Code code as follows:
--[local variable definition--]
A = 10--[while loop execution--]
while (a < 20)
Todo
Print ("Value of A:", a)
A=a+1
if (a > 15)
Then
--[terminate the loop using break statement--]
Break
End
End
When the above code is built and run, it produces the following results.
Copy Code code as follows:
Value of A:10
Value of A:11
Value of A:12
Value of A:13
Value of A:14
Value of A:15