Lua break Statement

Lua 循环Lua Loops

In Lua programming language, the break statement is inserted in the loop body to exit the current loop or statement, and start executing the statement immediately following the script.

If you use nested loops, the break statement will stop the execution of the innermost loop and start executing the outer loop statement.

Syntax

In Lua programming languagebreakThe statement syntax format is:

break

Flowchart:

Example

The following example executes a while loop, outputs the value of a when variable a is less than 20, and terminates the loop when a is greater than 15:

--[ 定义变量 --]
a = 10

--[ while 循环 --]
while( a < 20 )
do
   print("a 的值为:", a)
   a=a+1
   if( a > 15)
   then
      --[ 使用 break 语句终止循环 --]
      break
   end
end

The execution result of the above code is as follows:

a 的值为:    10
a 的值为:    11
a 的值为:    12
a 的值为:    13
a 的值为:    14
a 的值为:    15

Lua 循环Lua Loops

Other Extensions