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

[Lua][Question] alternative to goto ::label:: ?

Started by damnedsky, 20 October 2012 - 01:15 PM
damnedsky #1
Posted 20 October 2012 - 03:15 PM
Just found out that we don't have a "goto label" option because CC uses lua 5.1 right?….. bummer
In this case could you guys recommend a way to call a redo for an IF statement from inside the IF itself! I can't use break because I'm inside 3 FORs and if I'm not mistaken that kills all the FORs
The code is something like this

for do
  for do
	for do
		::label::
		if condition then
		   ......
		   goto label
		else
		   .....
		end
	end
  end
end

My immediate solution would be to use use a WHILE loop similar to this:

for do
  for do
	for do
	   local aux = true
	   while condition and aux do
		 ......
		 ......
		 -- eventually
		 aux = false
	   end
	   ......
	   ......
	end
  end
end

This however would complicate the rest of my code.
Cloudy #2
Posted 20 October 2012 - 04:01 PM
Generally if you have to use goto your code isn't structured properly into functions. However it's not entirely clear what you actually want - break only does break out of one for, not all of them.
ChunLing #3
Posted 20 October 2012 - 05:57 PM
Yes. 'return' is the one that will exit an entire function. 'break' only affects the innermost current loop.
damnedsky #4
Posted 20 October 2012 - 09:40 PM
Generally if you have to use goto your code isn't structured properly into functions. However it's not entirely clear what you actually want - break only does break out of one for, not all of them.

I took your advice and it's getting pretty hard to make functions out of the algorithm. A goto would make it all so easy, I would have to over-complicate it with "global" variables or parameters. It is a small piece of code that I want to use in the CC competition. It's part of the algorithm that does the first search for trees in the neighborhood… it's a long story to explain…. sorry :)/>/>

The thing is I like making small algorithms and I try to avoid as much as possible "step by step" instructions, but I guess there is no choice :)/>/>
ChunLing #5
Posted 20 October 2012 - 09:47 PM
Hooray for parameters! You'll be fine.