This article mainly introduces the use of nested loops in LUA programming, is the basic knowledge in Lua's introductory learning, and needs friends to refer to the following
The LUA programming language allows the use of one loop to embed another loop. A few examples are presented below to illustrate this concept.
Grammar
Nested looping syntax statements in LUA are as follows:
The code is as follows:
For init,max/min value, increment
Todo
For init,max/min value, increment
Todo
Statement (s)
End
Statement (s)
End
A syntax statement for a nested while loop in the LUA programming language, as follows:
The code is as follows:
while (condition)
Todo
while (condition)
Todo
Statement (s)
End
Statement (s)
End
The syntax statements for the LUA programming language nesting repeat...until loops are as follows:
The code is as follows:
Repeat
Statement (s)
Repeat
Statement (s)
Until (condition)
Until (condition)
At the end of loop nesting, you can put any type of loop into any other type of loop. For example, a for loop can be within another while loop, and vice versa.
For example:
The following program uses a nested loop:
The code is as follows:
J =2
For i=2,10 do
For j=2, (i/j), 2 do
if (not (I%J))
Then
Break
End
if (J > (i/j)) Then
Print ("Value of I is", i)
End
End
End
When the above code is built and run, it produces the following results.
The code is as follows:
Value of I is 8
Value of I is 9
Value of I is 10