Lua loop
In many cases we need to do some regular repetitive operations, so we need to repeatedly execute certain statements in the program.
A group of statements that are executed repeatedly is called the loop body. Whether it can continue to be repeated determines the termination condition of the loop.
The loop structure is a process structure that repeatedly executes a certain program under certain conditions. The repeatedly executed program is called a loop body.
The loop statement is composed of two parts: the loop body and the loop termination condition.
Lua language provides the following loop processing methods:
Loop type | Description |
---|---|
while loop | Let the program execute certain statements repeatedly when the condition is true. Before executing the statement, it is checked whether the condition is true. |
for loop | Repeatedly executes the specified statement. The number of repetitions can be controlled in the for statement. |
Lua repeat...until | Repeat the loop until the specified condition is true |
Loop embedding Set | You can nest one or more loop statements (while, for, do..while) inside the loop |
Loop control statement
Loop control statements are used to control the flow of the program to realize various structural methods of the program.
Lua supports the following loop control statements:
Control statement | Description |
---|---|
break Statement | Exits the current loop or statement and begins script execution of the immediately following statement. |
Infinite loop
If the condition is always true in the loop body, the loop statement will be executed forever. The following takes the while loop as an example:
while( true ) do print("循环将永远执行下去") end