Lua For loop question

Lua can handle for loops in two ways,

for i = 1,2,3,4 do
print(i)
end
and

for i=1,20 do
print(i)
end

What would happen if I was to use

for i=1,30,3 do
print(i)
end

Should it consider these are three values of 1, 30 and 3 or think of it as a loop from 1 to 30 with stepping of 3?
or should it be for i in 1,2,3,4,5 do

??

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13458 reply_id: 313458[/import]

think of it as stepping [import]uid: 7911 topic_id: 13458 reply_id: 49385[/import]

LUA’s numeric for loop construct is;

for = , , do

end

For example;
1. To loop from 1 to 100
[lua]for i = 1, 100 do
print(i)
end[/lua]

2. To loop from 1 to 100 in increments of 2
[lua]for i = 1, 100, 2 do
print(i)
end[/lua]

Your first code example
[lua]for i = 1,2,3,4 do
print(i)
end[/lua]
raises an error as you have too many arguments in the for loop expression.

LUA supports a generic for loop which calls a function for each iteration of the loop.

The ‘for loop’ in the following example will execute the pairs() function for each item in the names table. The key to the table item is stored in i and the result of the pairs() function is stored in name
[lua]local names = {“joe”, “Mary”, “Steve”};

for i, name in pairs(names) do
print("item " … i … " = " … name)
end[/lua]

[import]uid: 26397 topic_id: 13458 reply_id: 49393[/import]

Thanks Everyone,
I know about the for loops with the start, end, step and with the ipairs() and pairs()
I guess I had misread that it worked like the unix for loop where you can pass a series of values to the for loop. So I had this question, however I am unable to find the source where I got that confusion from, so I will take it that it is not possible with for in lua.

Thank you for trying to explain how for works, but that was not my question.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13458 reply_id: 49417[/import]