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

Q:How to start an program all over again with permision?

Started by Zambonie, 24 December 2012 - 04:53 AM
Zambonie #1
Posted 24 December 2012 - 05:53 AM
Ok,so I am making my calculater,as proboly you could see me asking q. in another thread.But now,Im improving it to ask you if you want to restart it to do another calculation.I got held of the part for the input,so my q. is how do I start it over again?And yes,I proboly dont want to use "while true do".I want it so it asks for permision.And the funny part is that when you type in something,I jsut made it print "Feature coming soon". :P/> .So its like in jeopordy.So any answers?Thanks if you answer!

*Note*If its going to complicated a little,then can you explain it to me?You proboly should know Im knew to lua :/ !

Edit:Alright,I proboly could take and "while true do" answer,but you still only with permision…
Zambonie #2
Posted 24 December 2012 - 06:50 AM
No answers :(/>
Doyle3694 #3
Posted 24 December 2012 - 06:56 AM
you could do

-- declaration
::loop:
-- realtime code
goto loop
PixelToast #4
Posted 24 December 2012 - 06:59 AM
you could do

-- declaration
::loop:
-- realtime code
goto loop
isnt that only on 5.2 lua .-.
i dont rember there being a goto in the 5.1 manual
nor have i seen anyone use goto

proper way to loop:

while true do
--code
end
the only difference is that it creates a new block so anything localized will go out of scope once you break the loop
Doyle3694 #5
Posted 24 December 2012 - 07:01 AM
oh hmm… don't know actually. A real pro would have to confirm, just read a lua manual part somewhere about goto, linked by lyqyd, I believe.
PixelToast #6
Posted 24 December 2012 - 07:05 AM
oh hmm… don't know actually. A real pro would have to confirm, just read a lua manual part somewhere about goto, linked by lyqyd, I believe.
yea, i remember that post it had a link to the 5.2 documentation
here is the section in the manual that shows it:
http://www.lua.org/m...nual.html#3.3.4

its nowhere in the 5.1 documentation

also, you will have to create a label outside a goto loop in order to break it
unless the break command jumps to the closest goto command, not sure
ChunLing #7
Posted 24 December 2012 - 07:08 PM
If you want to allow the user to break out of the loop, then use:
repeat
--code to repeat goes here
  write("Enter 'x' to exit")
until read() == "x"
remiX #8
Posted 24 December 2012 - 10:12 PM
Or

cX, cY = term.getCursorPos()
repeat
    term.setCursorPos(cX, cY)  -- set and clear so it doesn't spam the screen
    term.clearLine()
    write("Enter 'x' to exit")
    e = {os.pullEvent("char")}
until e[2] == "x"
ChunLing #9
Posted 25 December 2012 - 01:26 AM
In that case it would be "Press 'x' to exit, any other letter to continue".
remiX #10
Posted 25 December 2012 - 01:30 AM
In that case it would be "Press 'x' to exit, any other letter to continue".

Uhm, no? Check my until condition ><
ChunLing #11
Posted 25 December 2012 - 02:01 AM
The code pauses until it gets a char event, and if that char is "x", the loop ends, otherwise it continues. That's "Press a letter" and "'x' to exit" (other letters continue).

I just mean that the meaning differs from "Enter 'x' to exit", which implies use of the Enter key.

I suppose you mean that "continue" should mean exiting the loop…which can be true or false depending on the overall function of the loop in the program. If it is just there to get a "correct" response, then exiting the loop is continuing the program. But in this case, the primary function of the program is supposed to be inside our loop, exiting the loop is not continuing the program.

All of which is very meta. I just wanted to make it clear that the user didn't need to use Enter, and decided to throw in that other letters would restart the loop. But I guess I didn't.
Zambonie #12
Posted 25 December 2012 - 05:09 AM
Uhm,What I know jus tto break a loop,just type break :/ .
ChunLing #13
Posted 26 December 2012 - 02:37 PM
Yes, but all Lua loops have a proper exit condition as well, and it is usually better to use that. There are times (such as breaking a for loop early) when a break is very useful, but generally you should use your other other options.
PixelToast #14
Posted 26 December 2012 - 03:44 PM
Yes, but all Lua loops have a proper exit condition as well, and it is usually better to use that. There are times (such as breaking a for loop early) when a break is very useful, but generally you should use your other other options.
letting the loop end itself is no better than using break nor is it more efficient
ChunLing #15
Posted 26 December 2012 - 06:03 PM
Um…in a strict technical sense, this may be true, but in practical coding terms it is not. If you don't include an extra conditional test (if then or whatever) for the break, then you shouldn't use a loop at all, and if you do have an extra conditional test then…well, it's extra.

Exiting via break doesn't cost more than exiting by the loop's defined exit, but simply having extra exits does cost more. And it makes your code uglier.