for loop lua code example

Example 1: lua for loop

-- For K,V in table
for k,v in pairs(tbl) do
 print(k)
 print(v)
end
-- For i=0, num
for i=0, num do
 print(i)  
end

Example 2: infinite loop lua

-- We need to create a wait function to slow down the while true do loop.
-- The loop will run so fast, that we need to slow it down. Otherwise, it'll crash.

function wait(seconds)
  local start = os.time() 
  repeat until os.time() > start + seconds
end

while true do -- Create the infinite loop
	print("Hello World")
   	
    wait(1) -- The number you place in the paramater is how many seconds it will wait until looping again.
end

Example 3: For loop lua

for startValue, EndValue, [increments] do
        --code to execute
end
--The increments value is optional.  If it isn't defined, it is assumed to be "1"

Example 4: for loop lua

local t = {}
for index, value in ipairs(t) do
	print(index, value)
end

Example 5: for loop lua

for x = 0, 10, 1 do
	print(x)
end

Example 6: for loop lua

local t = {}
for key, value in pairs(t) do
	print(key, value)
end

Tags:

Lua Example