Latest web development tutorials

Lua nested loop

Lua cycle Lua cycle

Lua programming language to allow embedding the loop cycle. The following example demonstrates Lua nested loop applications.

grammar

Lua programming language syntax for nested loop:

for init,max/min value, increment
do
   for init,max/min value, increment
   do
      statements
   end
   statements
end

Lua programming language while loop nested syntax:

while(condition)
do
   while(condition)
   do
      statements
   end
   statements
end

The repeat Lua programming language ... until nested loop syntax:

repeat
   statements
   repeat
      statements
   until( condition )
until( condition )

In addition to these same types of nested loop, we can use different types of nested loops, such as nested for loop while loop.

Examples

The following example uses a nested for loop:

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("i 的值为:",i)
      end
   end
end

The above code is executed as a result of:

i 的值为:	8
i 的值为:	9
i 的值为:	10

Lua cycle Lua cycle