Lua loop nesting


Release date:2023-09-28 Update date:2023-10-13 Editor:admin View counts:414

Label:

Lua loop nesting

Lua loop is allowed to be embedded in a loop in a programming language. The following example demonstrates Lua application of loop nesting.

Grammar

In the Lua programming language for loop nested syntax format:

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

In the Lua programming language while loop nested syntax format:

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

In the Lua programming language repeat...until loop nested syntax format:

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

In addition to the same type of loop nesting above, we can also use different loop types to nest, such as for nesting in the loop body while cycle.

Example

The following example uses the for loop nesting:

Example

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("The value of i is:",i)
      end
   end
end

The result of the above code execution is:

The value of i is: 8
The value of i is: 9
The value of i is: 10

Powered by TorCMS (https://github.com/bukun/TorCMS).