Lua cycle


Release date:2023-09-27 Update date:2023-10-13 Editor:admin View counts:279

Label:

Lua cycle

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

A group of statements that are repeatedly executed are called loop bodies, and whether they can be repeated or not determines the termination conditions of the loop.

The loop structure is the process structure that repeatedly executes a certain program under certain conditions, and the program that is executed repeatedly is called the loop body.

The loop statement consists of two parts: the body of the loop and the termination condition of the loop.

Image0

Lua language provides the following loop processing methods:

Cycle type

Description

While cycle

Have the program execute certain statements repeatedly when the condition istrue. Check whether the condition is true before executing the statement.

For cycle

The specified statement is executed repeatedly, and the number of repetitions can be controlled in the for statement.

repeat…until

Repeat the loop until the specified condition is true

Loop nesting

You can nest one or more loop statements within a loop (while do… End;for… Do… End;repeat… Until;)

Loop control statement

The loop control statement is used to control the flow of the program in order to realize the various structures of the program.

Lua supports the following loop control statements:

Control statement

Description

Break statement

Exits the current loop or statement and starts the script to execute the following statement.

Goto statement

Transfer the control point of the program to a label.

Infinite cycle

In the body of a loop, if the condition is always true , the loop statement will be executed forever, as follows while loops as an example:

Example

while( true )
do
   print("The loop will always execute")
end

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