Lua break statement


Release date:2023-09-28 Update date:2023-10-13 Editor:admin View counts:273

Label:

Lua break statement

Lua programing language break statement is inserted into the body of the loop to exit the current loop or statement and start the script to execute the statement that follows.

If you use loop nesting break statement stops the execution of the innermost loop and starts the outer loop statement of execution.

Grammar

In Lua programming language break statement syntax format:

break

Flow chart:

Image0

Example

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

--[ Define variables --]
a = 10

--[ While loop --]
while( a < 20 )
do
   print("The value of a is:", a)
   a=a+1
   if( a > 15)
   then
      --[ Terminating a loop using a break statement --]
      break
   end
end

The execution result of the above code is as follows:

The value of a is: 10
The value of a is: 11
The value of a is: 12
The value of a is: 13
The value of a is: 14
The value of a is: 15

Powered by TorCMS (https://github.com/bukun/TorCMS).