r/ComputerCraft • u/Regeneric • Jun 12 '23
Alternatives to `continue` in loops
Hi,
I am not a LUA developer, I write my code most of the time in C and JS so it is natural for me that I've got access to continue keyword inside loops. But not in LUA.
I've seen that people use goto to achieve that:
local cnt = 0
while cnt < 10 do
cnt = cnt + 1
if cnt == 5 then goto continue end
::continue::
end
But it doesn't work in CC.
Is there any other way to get continue functionality in here?
1
u/Geekmarine72 Jun 12 '23
You could always use 'repeat until' for almost the same functionality as continue?
local cnt = 0
repeat
cnt = cnt + 1
if cnt == 5 then cnt = cnt + 1 end
-- code
until cnt >= 101
1
u/9551-eletronics Computercraft graphics research Jun 12 '23
good like with using this in something more advanced than just counting up. like a for loop with the pairs iterator
1
u/Harmed_Burglar You are the nil to my peripheral.wrap( ) Jun 18 '23
Woah, goto? Didn't know this was Assembly
1
2
u/9551-eletronics Computercraft graphics research Jun 12 '23
You will have to just go with inverted if statements.
Would be something like this
Also goto doesn't work because we use an older version of Lua.
probably for the better, goto is cursed