for in lua code example

Example 1: lua in pairs

--Table is the table to iterate through
--Index is the current index
--Value is the value at the current index

for index, value in pairs(table) do

end

Example 2: 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 3: for i in pairs lua

local table =  {2,3,12, "Hello"} --a simple array
for i, item in pairs(table) do -- for i in pairs loop goes through all the items in an array/table
	print(item)
end

Example 4: 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 5: for loop lua

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

Example 6: for in r

for (val in x) {
if(val %% 2 == 0)  count = count+1
}

Tags:

Lua Example