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

breaking multiple loops

Started by pinksheep00, 20 March 2013 - 11:18 PM
pinksheep00 #1
Posted 21 March 2013 - 12:18 AM
so say there's a while loop in a while loop in a while loop…

simplified version of the code:

while not a() do --first loop
while not b() do --second loop
  while not c() do --third loop
   while not d() do --fourth loop
   end
  end
end
end

the b() function is suppose to make the a() function true but if it failed then the c() function will run and try to make the b() function true
and so on…

is there a way to jump from the fourth loop back to the first loop, like break the loop 2, 3, 4 once the computer is inside the fourth loop?
I tried to put the whole code in a function and called it foo() then put the foo() function inside the fourth loop so once the fourth loop is called it will jump back to the first loop, but the program completely messed up :/
Kingdaro #2
Posted 21 March 2013 - 01:10 AM
Wrap it in a function and use return.


function something()
while not a() do --first loop
while not b() do --second loop
  while not c() do --third loop
   while not d() do --fourth loop
    if condition then
     return
    end
   end
  end
end
end
end
theoriginalbit #3
Posted 21 March 2013 - 01:18 AM
Wrap it in a function and use return.
that wont break the 4th loop back to the first loop like the op wants. that just breaks them all. also in OP they said they put it in a function already.

EDIT: maybe try something like this, its really the only thing i can think of at this point in time


local btimes = 0

local function check()
  if btimes > 0 then
	btimes = btimes - 1
	if btimes < 0 then btimes = 0 end
	return true
  end
  return false
end

while not a() do
  if check() then break end
  while not b() do
	if check() then break end
	while not c() do
	  if check() then break end
	  while not d() do
		if check() then break end
		if someCondition then
		  btimes = 3 -- this could be any number of times you want to break down the chain.
		end
	  end
	end
  end
end
Edited on 21 March 2013 - 12:22 AM
Kingdaro #4
Posted 21 March 2013 - 01:26 AM
OP wants to go back to the first loop, I see.

Common practice is to just use a "running" boolean, and break if it's not true in every loop but the first.


while a do
  local running = true
  while b do
    while c do
      while d do
        if condition then
          running = false
          break
        end
        if not running then break end
      end
      if not running then break end
    end
    if not running then break end
  end
  boop()
end

That, or just not complicate the process and put every loop you want broken in a separate function.


local function looper()
  while b do
    while c do
      while d do
        if condition then return end
      end
    end
  end
end

while a do
  looper()
  boop()
end
pinksheep00 #5
Posted 21 March 2013 - 01:43 AM
I'm really not sure about this but i think i found the problem… or not…. anyways its working now so thats a good news
I think if I put the code in a function and put that function inside the last loop its creating a loop of function
e.g: function 1, 2, 3… then magically at function 100 the first loop turn to true then it will continue…. back to function 99..

so i did this :/

function foo()
while not a() do --first loop
  while not b() do --second loop
   while not c() do --third loop
    while not d() do --fourth loop
	 foo()
	 return
    end
   end
  end
end
end

"return" saves the day :)/>
Kingdaro #6
Posted 21 March 2013 - 02:12 AM
You really shouldn't do that. That has the potential to create a stack overflow from recursion.