Tutorial on using nested loops in Lua programming

Time:2021-12-30

 Luaprograming languageAllows you to use one loop to embed another. Several examples are presented below to illustrate this concept.
grammar

Nested loop syntax statements in Lua are as follows:

 

Copy codeThe 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 syntax statement of nested while loop in Lua programming language is as follows:

 

Copy codeThe code is as follows:
while(condition)
do
   while(condition)
   do
      statement(s)
   end
   statement(s)
end

 

Lua programming language nested repeat The syntax statement of until loop is as follows:

 

Copy codeThe 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 procedure uses a nested loop:

 

Copy codeThe 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 you build and run the above code, it will produce the following results.

 

Copy codeThe code is as follows:
Value of i is 8
Value of i is 9
Value of i is 10

Recommended Today

JVM + GC parsing (premise knowledge concatenation)

Premise preparation JVM GC garbage collection JVM virtual machine monitoring, tuning and troubleshooting Tomcat and microservice optimization 1. Premise review 1.1. JVM memory structure 1.1.1、 JVM Architecture Overview The gray part in the figure isThread private, there is almost no garbage collectionOrange partThread sharing, the main place where garbage recycling occurs What is the class […]