Modifying the loop variable within the loop

I have a stupid/simple for loop question.

I try to skip a number in a loop by doing this:

for i=1, 10 do print("i = " .. i) if (i == 5) then i = i+1 end end

It’s a fairly common thing to do in C, but it seems to have no effect in lua.

The output is this:

i = 1

i = 2

i = 3

i = 4

i = 5

i = 6

i = 7

i = 8

i = 9

i = 10

Isn’t the two i’s in the same scope? 

I haven’t been able to find lua examples where this is mentioned.

Although I haven’t tested it extensively myself, I don’t think you can do that kind of thing with a for loop in Lua: I don’t think you can manipulate the very index system that the whole loop is based around. I’m not exactly sure what you are trying to get the end loop to do, but one possible option could be :

for i=1, 10 do

   if (i ~= 5) then

     print("i = " … i)

   end

end

Seems like this is easily done by using the while loop. Still don’t know why it doesn’t work with the for loop though…

Could be a lot of reasons ; this is generally not a good idea.

According to “the manual” (http://www.lua.org/pil/4.3.4.html)

“You should never change the value of the control variable: The effect of such changes is unpredictable”

The code I displayed was just an example to show that modifying the control variable didn’t seem to affect the looping. Not much sensible happening inside that example I’m afraid.

As to why I needed to change the control variable - I needed to control the loop iteration based on control strucures inside the loop. In C/C++ I do this all the time and it has worked as intended for the last 25 years…

Yes, that’s why I switched to a while loop, because there you  have to change the control variable manually and thus I have full control over the loop.

Yep, understood - just wanted to provide the link to the book at lua.org in case you didn’t know the site already - great source for all things Lua since it’s written by the guy who designed the language.

Ok, thanks  :slight_smile:

Although I haven’t tested it extensively myself, I don’t think you can do that kind of thing with a for loop in Lua: I don’t think you can manipulate the very index system that the whole loop is based around. I’m not exactly sure what you are trying to get the end loop to do, but one possible option could be :

for i=1, 10 do

   if (i ~= 5) then

     print("i = " … i)

   end

end

Seems like this is easily done by using the while loop. Still don’t know why it doesn’t work with the for loop though…

Could be a lot of reasons ; this is generally not a good idea.

According to “the manual” (http://www.lua.org/pil/4.3.4.html)

“You should never change the value of the control variable: The effect of such changes is unpredictable”

The code I displayed was just an example to show that modifying the control variable didn’t seem to affect the looping. Not much sensible happening inside that example I’m afraid.

As to why I needed to change the control variable - I needed to control the loop iteration based on control strucures inside the loop. In C/C++ I do this all the time and it has worked as intended for the last 25 years…

Yes, that’s why I switched to a while loop, because there you  have to change the control variable manually and thus I have full control over the loop.

Yep, understood - just wanted to provide the link to the book at lua.org in case you didn’t know the site already - great source for all things Lua since it’s written by the guy who designed the language.

Ok, thanks  :slight_smile: