Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua loop


May 12, 2021 Lua


Table of contents


Lua loop

In many cases we need to do some regular repetition, so we need to repeat certain statements in the program.

A set of statements that are repeated is called a loop body, and whether it can continue to repeat determines the termination condition of the loop.

A circular structure is a process structure in which a program is repeatedly executed under certain conditions, and a program that is repeatedly executed is called a loop body.

A loop statement is made up of two parts: the body of the loop and the termination condition of the loop.

Lua loop

The Lua language provides several ways to cycle:

The type of loop Describe
While loop When the condition is true, have the program execute certain statements repeatedly. The condition is checked for true before executing the statement.
For loop Repeat the specified statement, and the number of repetitions can be controlled in the for statement.
Lua repeat... u ntil Repeat the loop until the specified condition is true
Loop nesting You can nest one or more loop statements (while, for, do. while)

Loop control statements

Loop control statements are used to control the process of a program in order to implement its various structural methods.

Lua supports the following loop control statements:

The control statement Describe
Break statement Exit the current loop or statement and start the script executing the statement immediately following.

Unlimited loops

In a loop body, if the condition is always true, the loop statement is executed forever, as in the case of the while loop:

while( true )
do
   print("循环将永远执行下去")
end