Lua break Statement
The break statement in Lua programming language is inserted within the loop body to exit the current loop or statement and begin execution of the statement immediately following it.
If you use nested loops, the break statement will stop the execution of the innermost loop and start executing the next outer loop statement.
Syntax
The syntax for the break statement in Lua programming language is:
break
Flowchart:
Example
The following example executes a while loop that prints the value of variable a while it is less than 20, and terminates the loop when a is greater than 15:
--[ Define variable --]
a = 10
--[ while loop --]
while( a < 20 )
do
print("Value of a is:", a)
a=a+1
if( a > 15)
then
--[ Terminate the loop using break statement --]
break
end
end
The output of the above code is as follows:
Value of a is: 10
Value of a is: 11
Value of a is: 12
Value of a is: 13
Value of a is: 14
Value of a is: 15