Lua break Statement
The Lua programming language break statement is inserted in the loop body to exit the current loop or statement and begin execution of the statement immediately following the script.
If you use nested loops, the break statement stops the execution of the innermost loop and begins executing the outer loop statement.
Syntax
In the Lua programming language, the break statement syntax is as follows:
break
Flow diagram:
Example
The following example executes a while loop, outputting the value of a when variable a is less than 20, and terminating the loop when a is greater than 15:
--[ Define variables --]
a = 10
--[ while loop --]
while( a < 20 )
do
print("a 的值为:", a)
a=a+1
if( a > 15)
then
--[ Use the break statement to terminate the loop --]
break
end
end
The result of executing the above code is as follows:
a 的值为: 10
a 的值为: 11
a 的值为: 12
a 的值为: 13
a 的值为: 14
a 的值为: 15
Lua Loops