604 posts
Location
Spring Hill, Fl
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…
604 posts
Location
Spring Hill, Fl
Posted 24 December 2012 - 06:50 AM
No answers :(/>
818 posts
Posted 24 December 2012 - 06:56 AM
you could do
-- declaration
::loop:
-- realtime code
goto loop
2217 posts
Location
3232235883
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
818 posts
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.
2217 posts
Location
3232235883
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.4its 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
2005 posts
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"
2088 posts
Location
South Africa
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"
2005 posts
Posted 25 December 2012 - 01:26 AM
In that case it would be "Press 'x' to exit, any other letter to continue".
2088 posts
Location
South Africa
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 ><
2005 posts
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.
604 posts
Location
Spring Hill, Fl
Posted 25 December 2012 - 05:09 AM
Uhm,What I know jus tto break a loop,just type break :/ .
2005 posts
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.
2217 posts
Location
3232235883
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
2005 posts
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.