14 posts
Posted 08 April 2013 - 11:35 AM
I've got a program with a
while true
loop in it. All well and good, but now I need to edit the code and I can't work out how to stop the program running. Do I have to break the turtle and put it down again?
122 posts
Posted 08 April 2013 - 11:39 AM
There are 2 ways:
If you just wanna break the loop, use break:
while true do
stuff()
if someCondition then
break -- here
end
end
If you want to just stop the current program manually hold ctrl + t for at least 1 second. you can also use ctrl+s to shut down the turtle/computer or ctrl+r to reboot it
797 posts
Posted 08 April 2013 - 11:40 AM
break breaks a loop
while true do
event = os.pullEventRaw( )
if event == "terminate" then
break
end
end
edit: damn ninja'd
edit 2:
you can also use return but that will exit the currently executing function or the program if there is no function running
169 posts
Posted 08 April 2013 - 11:43 AM
You mean break out of the loop programatically, or just terminate the program? If you just want to stop the program, open up the turtle and hold Ctrl+T. If you want to break out of the loop within the program, then there are several things you can do. The simplest would be to use the "break" keyword. But some consider that bad practice. For while loops, it's better not to use "while true do" if you aren't looking for an infinite loop.
122 posts
Posted 08 April 2013 - 11:46 AM
Actually sometimes a while true loop is easier. i usually use it when reading from streams. Java example when reading from a socket:
String s = "", line;
while (true) {
line = reader.readLine();
if (line == null) break;
s += line;
}
see what i mean? But yeah, usually its better to not use while true
1852 posts
Location
Sweden
Posted 09 April 2013 - 12:26 PM
break breaks a loop
while true do
event = os.pullEventRaw( )
if event == "terminate" then
break
end
end
edit: damn ninja'd
edit 2:
you can also use return but that will exit the currently executing function or the program if there is no function running
You could also do something like this
bla = true
while bla do --This means only does the loop when bla is true and if it turns false it breaks from the loop
input = read()
if input == "Hello" then
print("Hello!")
sleep(1)
elseif input == "Bye" then
print("Bye!")
sleep(1)
bla = false --Here it breaks the loop
else
print("What?")
sleep(1)
end --'if' statement
end --'while <condition> do' loop
122 posts
Posted 21 April 2013 - 02:54 PM
Yes, but if you instantly wanna leave the look break is your friend. also break can break for loops which your solution can't
645 posts
Location
'Merica
Posted 21 April 2013 - 03:09 PM
you can just do a for loop, so it will break at your given point.
1852 posts
Location
Sweden
Posted 22 April 2013 - 06:44 AM
you can just do a for loop, so it will break at your given point.
for i = 100, 1, -1 do
term.clear()
term.setCursorPos(1,1)
print("Seconds to something awesome: "..i)
end
term.clear()
term.setCursorPos(1,1)
print("Fooled Ya!")
sleep(1)
os.shutdown()