For i = 1, 1, -1 vs for i = 1, #table

I have seen these two types of for loops for a while now. I understand what the second one does, but not what the first one does. Could someone please explain?

The first will do nothing with the values you have entered but in general lua for loops are 

for i = init\_value, min\_or\_max\_value, increment

so for loop backward though number 1-10 you would do

for i = 10, 1, -1

 so your first loop will start at 1 and decrement i until i equals 1 (so it will never loop)

EDIT see https://www.tutorialspoint.com/lua/lua_for_loop.htm for more

For clarification, what the second one is doing is really no different to the first one

init_value is 1

min_or_max_value is #table where # is unary operator for the length of a table

increment is by default 1

so for a table of length 10, the second loop is the same as typing 

for i = 1, 10, 1

 which looks alot like the first one

Ok, thanks!

best answer? really *shakes head*

if you are removing elements in your loop then you loop backwards

if not removing then it makes no difference which way you loop.

Dont really understand your issue with my answer… it makes a big difference which way you loop… If you loop backwards your iterator goes from max to min, if you loop forwards it goes from min to max, literally the opposite of each other

EDIT - by min i mean initial value

Technically they are the same… but that comes with a big BUT

It is just that if someone reads forward and backwards loop are the same they could get into real trouble if they did something like this

local obj = {} local group = display.newGroup( ) for i = 1, 10 do   obj[i] = display.newRect( group,0,0,100,100 ) end for i = 1, group.numChildren do   group[i]:removeSelf() end

as the second loop will crash

Fair enough, what I meant by them being the same is that they have the same syntax and that there is no two types of for loops. But yes specifying different values will result in different functionality, and looping over an object, while removing elements of that object is something to be careful of no matter what language you are working with.

The first will do nothing with the values you have entered but in general lua for loops are 

for i = init\_value, min\_or\_max\_value, increment

so for loop backward though number 1-10 you would do

for i = 10, 1, -1

 so your first loop will start at 1 and decrement i until i equals 1 (so it will never loop)

EDIT see https://www.tutorialspoint.com/lua/lua_for_loop.htm for more

For clarification, what the second one is doing is really no different to the first one

init_value is 1

min_or_max_value is #table where # is unary operator for the length of a table

increment is by default 1

so for a table of length 10, the second loop is the same as typing 

for i = 1, 10, 1

 which looks alot like the first one

Ok, thanks!

best answer? really *shakes head*

if you are removing elements in your loop then you loop backwards

if not removing then it makes no difference which way you loop.

Dont really understand your issue with my answer… it makes a big difference which way you loop… If you loop backwards your iterator goes from max to min, if you loop forwards it goes from min to max, literally the opposite of each other

EDIT - by min i mean initial value

Technically they are the same… but that comes with a big BUT

It is just that if someone reads forward and backwards loop are the same they could get into real trouble if they did something like this

local obj = {} local group = display.newGroup( ) for i = 1, 10 do   obj[i] = display.newRect( group,0,0,100,100 ) end for i = 1, group.numChildren do   group[i]:removeSelf() end

as the second loop will crash

Fair enough, what I meant by them being the same is that they have the same syntax and that there is no two types of for loops. But yes specifying different values will result in different functionality, and looping over an object, while removing elements of that object is something to be careful of no matter what language you are working with.