Jump to content




How to break a loop out of scope

lua

7 replies to this topic

#1 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 02 January 2016 - 11:39 PM

I've discovered there is no way to stop a "while" loop from a function defined out of scope, for example this will not work:
function stop()
  term.setTextColor(colors.white)
  term.clear()
  term.setCursorPos(1, 1)
  break
end

while true do
  print("Exiting program in 2 seconds")
  sleep(2)
  stop()
end
Because, from the scope of the function, there is no loop to break, unless it is defined inside of the loop, as such:
while true do
  local function stop()
    term.setTextColor(colors.white)
    term.clear()
    term.setCursorPos(1, 1)
    break
  end

  print("Exiting program in 2 seconds")
  sleep(2)
  stop()
end

Unfortunately, this function can only be used inside of the loop it is defined within, and loops nested inside that use the function will end the loop the function is defined in.

I would like to be provided with a function that can be defined once and end all loops, to ultimately stop the program, but while clearing the screen and stopping all loops.

#2 Lignum

  • Members
  • 558 posts

Posted 02 January 2016 - 11:52 PM

You could call error without any arguments as such, which will instantly stop the program:
error()

However, in my opinion, this is a very ugly solution. I suggest you do something like this instead:

local keepRunning = true

while keepRunning do
   --# ...
end

Now if you set keepRunning to false, the loop will stop. Keep in mind that if you do this while the loop is running, the loop will finish its current iteration before it stops.

#3 Anavrins

  • Members
  • 775 posts

Posted 03 January 2016 - 12:02 AM

Simply use the "break" keyword
while true do
  print("Exiting in 2 seconds")
  sleep(2)
  break
end
#- Will resume execution after the "end" of the while loop, assuming it's the end of the program


Didn't read correctly, Lignum solution works for your case.

Edited by Anavrins, 03 January 2016 - 12:09 AM.


#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 03 January 2016 - 12:20 AM

The real question here is why would you ever want to do that.

#5 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 03 January 2016 - 01:53 AM

View PostKingofGamesYami, on 03 January 2016 - 12:20 AM, said:

The real question here is why would you ever want to do that.
I guess the op want's to execute some code once you stop the loop without re-initializing the function all the time or copy-pasting the code after each "break".

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 03 January 2016 - 02:08 AM

Yeah, but the obvious way to do that is to simply execute that code after the loop has ended, not before.

#7 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 03 January 2016 - 04:50 AM

View PostLignum, on 02 January 2016 - 11:52 PM, said:

You could call error without any arguments as such, which will instantly stop the program:
error()


I'm wondering if that will stop the program without causing a newline, hmm might have to test it. Going to edit post after testing.

Edit: I have tested it and it works perfectly! Thanks!

View PostLignum, on 02 January 2016 - 11:52 PM, said:

{see above}

However, in my opinion, this is a very ugly solution. I suggest you do something like this instead:

local keepRunning = true

while keepRunning do
   --# ...
end

Now if you set keepRunning to false, the loop will stop. Keep in mind that if you do this while the loop is running, the loop will finish its current iteration before it stops.

I have tried that, and it seems to work most of the time, but I don't think this is the correct solution.

Edited by LoganDark, 03 January 2016 - 04:59 AM.


#8 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 03 January 2016 - 05:04 AM

How I prefer to do it, in other programming languages without using tools like error and system.exit

local function func()
  --#I want to leave my loop
  return "break" --#Or false, or whatever I want my break variable to equal
end

while true do
  local funcStuff = func()
  if funcStuff == "break" then
    break
  end
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users