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

Goto Statement?

Started by DemonAssassin, 14 February 2012 - 11:29 PM
DemonAssassin #1
Posted 15 February 2012 - 12:29 AM
How exactly would you do a goto statement? I've looked at the Lua wiki, but it didn't help. I have an if and elseif accounting for yes/no in an input, but I'm trying to make it so that it also accounts for something that isn't either of them, then brings the person using it back to a certain line to re-enter the yes/no. How would I go about doing this?
Advert #2
Posted 15 February 2012 - 12:31 AM
Goto is bad (it causes more problems than it solves, and these problems are already solved with functions and loops), it also does not exist in Lua 5.1.
You should try using functions and loops instead.

I'll make an example for you, but I'm still not exactly sure what you're trying to accomplish, would you mind going into more detail?
DemonAssassin #3
Posted 15 February 2012 - 12:34 AM
I'm in the process of making an AntiVirus for fun on an SMP server I play on, and have a write statement for inputting Yes or No. I currently have everything working except for that it does not know what to do with something other then "Yes" or "No" and terminates the program if the input string is not either of them. So I've been trying to figure out how to return the program to the write statement to re-enter "Yes" or "No". I can supply the current code I have for it if needed.
Advert #4
Posted 15 February 2012 - 12:42 AM
I'm in the process of making an AntiVirus for fun on an SMP server I play on, and have a write statement for inputting Yes or No. I currently have everything working except for that it does not know what to do with something other then "Yes" or "No" and terminates the program if the input string is not either of them. So I've been trying to figure out how to return the program to the write statement to re-enter "Yes" or "No". I can supply the current code I have for it if needed.

Make it into a loop, or a function (and call that function in a loop, or loop inside that function):


local answer;
repeat
print("Question? Yes/No")
local response = read()
if response == "Yes" or response == "No" then
  answer = response
else
  print("Invalid answer! Try again.")
end
until answer;
print("You answered: " .. answer)





There are countless ways to do this, this is just one example.
DemonAssassin #5
Posted 15 February 2012 - 01:09 AM
Thanks! After reworking my code a little, this works perfectly!
Advert #6
Posted 15 February 2012 - 01:13 AM
Thanks! After reworking my code a little, this works perfectly!

You're welcome. If you have any questions reguarding alternative ways of doing this, or how the code works, feel free to ask.
DemonAssassin #7
Posted 15 February 2012 - 01:28 AM
I've reviewed the code you posted, and paired with my previous knowledge of some Java and C++ I believe I fully understand this. Thanks again!