Spoiler
Breaking Out Of Nested Loops
Started by CCJJSax, Sep 29 2013 12:51 AM
7 replies to this topic
#1
Posted 29 September 2013 - 12:51 AM
I tried doing this, but either I'm doing it wrong, or Computercraft doesn't support this. This is really early code. So it really doesn't do much... or anything right now. But I'm trying to break out of the "terminate ()" function. How can I break 2 loops?
#2
Posted 29 September 2013 - 01:35 AM
Tip: Next time show us the full error message you get. It helps us to help you.
You don't need "local" and "terminate" when making a function that way. You also need to call your function using another pair of paranthesis:
You don't need "local" and "terminate" when making a function that way. You also need to call your function using another pair of paranthesis:
(function ()
print("test")
end)()--//Here
#3
Posted 29 September 2013 - 01:42 AM
MKlegoman357, on 29 September 2013 - 01:35 AM, said:
Tip: Next time show us the full error message you get. It helps us to help you.
You don't need "local" and "terminate" when making a function that way. You also need to call your function using another pair of paranthesis:
You don't need "local" and "terminate" when making a function that way. You also need to call your function using another pair of paranthesis:
(function ()
print("test")
end)()--//Here
Oops, I meant to add in the error message. Thanks for the heads up.
Your way looks way easier than the way I was using
#4
Posted 29 September 2013 - 10:33 PM
Hmmm. I guess I'm not understanding what you're suggesting as well as I thought.
error code : [string "temp"]:7: ambiguous syntax (function call x new statement)
while true do
print("loop 1")
sleep(1)
( function()
while true do
print("loop 2")
sleep(1)
end
end) ()
end
print('done')
error code : [string "temp"]:7: ambiguous syntax (function call x new statement)
#5
Posted 29 September 2013 - 11:10 PM
This would be the correct format, if I am understanding you correctly.
function function()
print("do something")
end
while true do
print("loop 1")
sleep(1)
function() --you had an added paranthesis here
while true do
print("loop 2")
sleep(1)
end
end --not sure what the parentheses here were doing
-- there was an extra 'end' here
print("done") --use the correct quotations
#6
Posted 30 September 2013 - 05:51 AM
campicus, on 29 September 2013 - 11:10 PM, said:
print("done") --use the correct quotations
print('Hi')
--//Same as:
print("Hi")
--//Same as:
print([[Hi]])
OP:
What you should really do is call your while loops in a function:
local function loop ()
while true do --//First while loop
local e, p = os.pullEventRaw("key")
if e == "terminate" then
while true do --//Second while loop
print("Are you sure you want to terminate? [y/N]")
local _, k = os.pullEventRaw("key")
if k == keys.y then
return --//Will exit the function
elseif k == keys.n then
break --//Will break the second while loop
elseif k == keys.enter or k == keys.numPadEnter then
break --//Will break the second while loop
end
end
end
end
end
loop()
print("Done")
Edited by MKlegoman357, 30 September 2013 - 05:59 AM.
#7
Posted 30 September 2013 - 06:25 PM
Fixed way:
EDIT: do..end blocks don't work like I thought they did. In this specific instance, using a `return` statement without anything else works just fine. I've updated my code snippet. Test it if you don't believe me.
Spoiler
Best way:
Spoiler
Note: I'm using quotation marks to highlight my comments because of the broken syntax highlighting this forum uses.EDIT: do..end blocks don't work like I thought they did. In this specific instance, using a `return` statement without anything else works just fine. I've updated my code snippet. Test it if you don't believe me.
Edited by AgentE382, 02 October 2013 - 08:50 PM.
#8
Posted 01 October 2013 - 05:19 AM
AgentE382, on 30 September 2013 - 06:25 PM, said:
Fixed way:
Spoiler
Best way:
Spoiler
Note: I'm using quotation marks to highlight my comments because of the broken syntax highlighting this forum uses.Nope. return exits functions not do chunks. You can't exit do using break or return.
EDIT: It's not the Best way to do this.
Edited by MKlegoman357, 01 October 2013 - 05:36 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











