This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Konlab's profile picture

"continue" - updated with new code

Started by Konlab, 03 February 2015 - 04:32 PM
Konlab #1
Posted 03 February 2015 - 05:36 PM
Original idea:
In some languages there is a command called continue. What it does? In loops if you use continue the rest of the block won't be done, instead of that, the block will be done from the beginning. How to use it in lua? There's no continue in lua, but you can replace it with a little code:


local var = true
while var do
repeat
(block)
until true
end
If you use this:
The break command will work like the continue, and if you want to exit you have to set var to false and then break.

New idea:
You could use someyhing similar to this to go up in the code:
local a = true
repeat
    repeat
        (Block)
    until true
until a
In the (Block):
If you want to go up to the repeat just set a to false and break.
I know it's almost the same like the first example but I added this here because it's an other example that can maybe help somebody…
Edited on 01 November 2015 - 03:16 PM
TheOddByte #2
Posted 03 February 2015 - 07:37 PM
Uhmm.. Isn't there an 'end' too much there?

local var = true
while var do
    repeat
        --# Block here
    until true --# You put an 'end' here, repeat loops don't need them
end
Bomb Bloke #3
Posted 03 February 2015 - 10:55 PM
Nesting loops is one way to achieve the desired effect. Another is to define a function and have the loop call that.

These solutions aren't "ideal" (if only because they're inelegant), but they're the best you can do under Lua 5.1. In 5.2, "goto" was included to "solve" this sort of problem.
ElvishJerricco #4
Posted 04 February 2015 - 06:25 AM
Well this is actually pretty clever. Nice.
Konlab #5
Posted 02 September 2015 - 05:53 PM
Well this is actually pretty clever. Nice.
Thanks!

Uhmm.. Isn't there an 'end' too much there?

local var = true
while var do
    repeat
        --# Block here
    until true --# You put an 'end' here, repeat loops don't need them
end
Fixed! Thanks!