This article introduces the use of a for-loop statement in Lua, which is the basic knowledge of LUA's introductory learning, and needs friends to refer to the
A For loop is a circular control structure that effectively writes a specific number of loops that need to be performed.
Grammar
The syntax for the for loop of the LUA programming language is:
The code is as follows:
For init,max/min value, increment
Todo
Statement (s)
End
The following is the process of controlling a loop:
The initialization step is executed first and only once. This step allows you to declare and initialize any of the loop control variables.
Next is Max/min, which is the maximum or minimum value until the loop continues to execute. It creates a comparison between the initial and maximum/minimum values of a condition check internally.
After the For loop body executes, the control flow jumps back to the increment/decrement declaration. This statement can update any loop control variable.
Conditions are now recalculated for evaluation. If this is true, the loop executes and repeats the process (the loop body, then adds one step, then the condition). If the condition is false, the loop terminates.
Flow chart:
The code is as follows:
For i=10,1,-1
Todo
Print (i)
End
When the above code is built and executed, it produces the following results:
The code is as follows:
10
9
8
7
6
5
4
3
2
1