while trueloop 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?
Breaking a loop
Started by Heliomance, Apr 08 2013 10:35 AM
8 replies to this topic
#1
Posted 08 April 2013 - 10:35 AM
I've got a program with a
#2
Posted 08 April 2013 - 10:39 AM
There are 2 ways:
If you just wanna break the loop, use break:
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
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
#3
Posted 08 April 2013 - 10:40 AM
break breaks a loop
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
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
#4
Posted 08 April 2013 - 10: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.
#5
Posted 08 April 2013 - 10: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
#6
Posted 09 April 2013 - 11:26 AM
awsumben13, on 08 April 2013 - 10:40 AM, said:
break breaks a loop
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
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
#7
Posted 21 April 2013 - 01: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
#8
Posted 21 April 2013 - 02:09 PM
you can just do a for loop, so it will break at your given point.
#9
Posted 22 April 2013 - 05:44 AM
Spongy141, on 21 April 2013 - 02:09 PM, said:
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()
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











