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

exit a program?

Started by !!!!!!!!!!ExclaimationMark, 12 August 2014 - 02:04 AM
!!!!!!!!!!ExclaimationMark #1
Posted 12 August 2014 - 04:04 AM
How do you exit a program?
CCJJSax #2
Posted 12 August 2014 - 04:06 AM
My favorite way is to use

error()
!!!!!!!!!!ExclaimationMark #3
Posted 12 August 2014 - 04:11 AM
My favorite way is to use

error()
I know
error()
but… that's like an error. I know
shell.exit()
but I have a while loop.
EDIT: o got it working, I just disabled the while loop. no need to use shell.exit() and/or error()
Edited on 12 August 2014 - 02:13 AM
KingofGamesYami #4
Posted 12 August 2014 - 04:14 AM
This may be a little late, but you can use break to exit a loop, or return to exit a function.
!!!!!!!!!!ExclaimationMark #5
Posted 12 August 2014 - 04:23 AM
This may be a little late, but you can use break to exit a loop, or return to exit a function.
I know it, but I am outside a loop, so I am using another variable.
theoriginalbit #6
Posted 12 August 2014 - 04:48 AM
programs are run in Lua as functions, meaning that if you design your program correctly you can return out of it. if you don't, or its just too difficult to do so, use error, using error with no arguments will exit your program with no output to the user. do not use shell.exit, this does exactly what it says, it exists the shell program, which yes inherently exits your program, but is not a good way to do it!
!!!!!!!!!!ExclaimationMark #7
Posted 12 August 2014 - 05:30 AM
programs are run in Lua as functions, meaning that if you design your program correctly you can return out of it. if you don't, or its just too difficult to do so, use error, using error with no arguments will exit your program with no output to the user. do not use shell.exit, this does exactly what it says, it exists the shell program, which yes inherently exits your program, but is not a good way to do it!
so…

function exit()
-- exit thing
end
function loop()
while true do
  -- do looping stuff
end
end
loop()
What should be in exit()? error()?
Edited on 12 August 2014 - 03:31 AM
natedogith1 #8
Posted 12 August 2014 - 05:35 AM
You'd instead have something along the lines of
local continue=true
function exit()
continue=false-- exit thing
end
function loop()
while continue do
  -- do looping stuff
end
end
loop()
and, unless you're accessing 'exit' outside of your program, I'd recommend using local functions
flaghacker #9
Posted 12 August 2014 - 07:47 AM
Or using break:

while true do --main loop
  --stuff
  break --to exit, use the "break" keyword.  it can be in an if statement
end
Edited on 12 August 2014 - 05:48 AM
!!!!!!!!!!ExclaimationMark #10
Posted 12 August 2014 - 07:51 AM
Or using break:

while true do --main loop
  --stuff
  break --to exit, use the "break" keyword.  it can be in an if statement
end
I told you I need to exit the loop outside the loop
flaghacker #11
Posted 12 August 2014 - 07:55 AM
Or using break:

while true do --main loop
  --stuff
  break --to exit, use the "break" keyword.  it can be in an if statement
end
I told you I need to exit the loop outside the loop

Eh, what? You can't stop your program from somewhere else in itself, code that isn't executed simply doesn't do anything.

Or am I misunderstanding what you mean?
!!!!!!!!!!ExclaimationMark #12
Posted 12 August 2014 - 08:33 AM
Or using break:

while true do --main loop
  --stuff
  break --to exit, use the "break" keyword.  it can be in an if statement
end
I told you I need to exit the loop outside the loop

Eh, what? You can't stop your program from somewhere else in itself, code that isn't executed simply doesn't do anything.

Or am I misunderstanding what you mean?

I mean:

function fn1()
 print("doSomething")
end


function fn2()
 fn1()
 fn3()
end


function fn3()
 -- Exit
end
function loop()
-- the main program
end
loop()

Bomb Bloke #13
Posted 12 August 2014 - 11:37 AM
If you "must" use that structure, then yeah, it's likely easiest for you to stick error() into fn3.