How to Use nested loops in Lua Programming
This article describes how to use nested loops in Lua programming. It is the basic knowledge of Lua beginners. For more information, see
The Lua programming language allows one loop to embed another loop. The following describes several examples to illustrate this concept.
Syntax
The nested loop syntax statement in Lua is as follows:
The Code is as follows:
For init, max/min value, increment
Do
For init, max/min value, increment
Do
Statement (s)
End
Statement (s)
End
The nested while LOOP syntax statement in Lua programming language is as follows:
The Code is as follows:
While (condition)
Do
While (condition)
Do
Statement (s)
End
Statement (s)
End
The syntax statement of the Lua programming language nested repeat... until loop is as follows:
The Code is as follows:
Repeat
Statement (s)
Repeat
Statement (s)
Until (condition)
Until (condition)
At the end of loop nesting, it should be noted that any type of loop can be put into any other type of loop. For example, a for loop can be in 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 created and run, it will produce the following results.
The Code is as follows:
Value of I is 8
Value of I is 9
Value of I is 10