Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua for loop control variable

Tags:

lua

I need to be able to advance my for loop control index variable - from within the loop

for i=1,5 do
   io.write(("i: %d; "):format(i))
   i = i + 2 -- this has no effect, as 'for' is in control of the index var.
end
like image 423
Ronnie Smith Avatar asked Oct 27 '25 03:10

Ronnie Smith


1 Answers

I recommend you to use 'while'.

local i = 1
while i <= 5 do
  io.write(("i: %d; "):format(i))
  i = i + 2 -- it works
end
like image 136
NormalToad Avatar answered Oct 30 '25 05:10

NormalToad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!