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

[LUA] [PROGRAMMING] If / Else if statements not working :S

Started by Sarcastic_Bannana, 13 November 2012 - 08:12 AM
Sarcastic_Bannana #1
Posted 13 November 2012 - 09:12 AM
Okay, so since my last problem was solved so effectivly Ill plague the ComputerCraft community with annother of my frustrating (if only for me) errors.

Im attempting to control my ractor through several terminals that send rednet meesaages to a control terminal that then actions the messages ie opening doors, turning the reactor on/off.

However multiple outputs get turned on in every instance but the first despite the use of if and else if commands.
Open Reactor Door works fine but Emergency Shutdown doesnt work and infact opens the reactor door whislt sealing the bulkhead and turning on the lights and turning off the cooling.

This is likely down to my amateur programming skills and fundemental failure to grasp Lua programming.

Thanks in advance!

http://pastebin.com/X221Rq9B
XoX #2
Posted 13 November 2012 - 09:15 AM
Why all the "do" 's ?
billysback #3
Posted 13 November 2012 - 09:15 AM
your adding "ends" after your if and elseif statements, closing the if statement.

elseif continues an if statement, they don't create a new one, like so:

if x == y then

elseif x == z then

end
not

if x == y then

end

elseif x == z then

end
Sarcastic_Bannana #4
Posted 13 November 2012 - 09:18 AM
Why all the "do" 's ?
I thought it was the right thing to… well…er do. I suppose now that you mention it…
your adding "ends" after your if and elseif statements, closing the if statement.

elseif continues an if statement, they don't create a new one, like so:

if x == y then

elseif x == z then

end
not

if x == y then

end

elseif x == z then

end
I didnt know that. Do you think that is causing my problems?
billysback #5
Posted 13 November 2012 - 09:21 AM
definatly…
Spoiler


-- Definitions
blue = colors["blue"]
green = colors["green"]
purple = colors["purple"]
white = colors["white"]
yellow = colors["yellow"]
orange = colors["orange"]
i = 1

--Basic Startup
rednet.open("top")

-- Loop
while i == 1 do
id,message = rednet.receive()

-- Reactor Door Open
if message == "rdo1" then
rs.setBundledOutput("right", rs.getBundledOutput("right") + green)

--Reactor Door Close
elseif message == "rdo0" then
rs.setBundledOutput("right", rs.getBundledOutput("right") - green)

--Lights On
elseif message == "l1" then
rs.setBundledOutput("right", rs.getBundledOutput("right") + orange)

--Lights Off
elseif message == "l0" then
rs.setBundledOutput("right", rs.getBundledOutput("right") - orange)

--Cooling On
elseif message == "c1" then
rs.setBundledOutput("right", rs.getBundledOutput("right") - blue)

--Cooling Off
elseif message == "c0" then
rs.setBundledOutput("right", rs.getBundledOutput("right") + blue)

-- reactor Startup
elseif message == "rsu" then
rs.setBundledOutput("right", rs.getBundledOutput("right") + yellow)
sleep(5)
rs.setBundledOutput("right", rs.getBundledOutput("right") + white)

-- Reactor Shutdown
elseif message == "shu" then
rs.setBundledOutput("right", rs.getBundledOutput("right") - yellow)
rs.setBundledOutput("right", rs.getBundledOutput("right") - white)

-- Emergency Shutdown
elseif message == "emgcc1" then
rs.setBundledOutput("right", rs.getBundledOutput("right") + purple)
rs.setBundledOutput("right", rs.getBundledOutput("right") - white)
rs.setBundledOutput("right", rs.getBundledOutput("right") - blue)
rs.setBundledOutput("right", rs.getBundledOutput("right") + green)
rednet.reieve()
		if message == "emgcc0" then
		os.reboot()
		break
		end
elseif message == "break" then
break
end

end
^^^Fixed code^^^

also, "do" is an operator for loops (for, while), "then" is the operator of ifs, don't use both.

so to recap:
elseif statements continue an if statement, they don't create one
do is not for ifs, then is.
Sarcastic_Bannana #6
Posted 13 November 2012 - 09:35 AM
Okay so the door works fine both opening and closing without setting anything else off but none of the other commands work :S Have I got too many ends! I really dont know, sorry.
Fiery Arcanine #7
Posted 13 November 2012 - 10:02 AM
If you got to many ends. Try to make pairs of openings and ends
If it seems that you have too much ends, remove them (logically)

Do you get a eof error by the way? If yes, do what I stated above. It helped me alot

EDIT: Found a typo!

rednet.reieve()

reieve instead of receive
Sarcastic_Bannana #8
Posted 13 November 2012 - 10:22 AM
If you got to many ends. Try to make pairs of openings and ends
If it seems that you have too much ends, remove them (logically)

Do you get a eof error by the way? If yes, do what I stated above. It helped me alot

EDIT: Found a typo!

rednet.reieve()

reieve instead of receive

No, i dont get any eof errors. What do you mean by pairs of openings and ends?
Thanks, keep spelling that wrong.
Fiery Arcanine #9
Posted 13 November 2012 - 10:24 AM
Lets say I have 4 if statements (opening)
therefore I have 4 end statements (end)

One if + One end is a pair
but you don't have too many ends, I checked for it (I might be wrong)

EDIT: If you still dont understand, message me and I will tell it tommorow. Or maybe someone else who understands what I'm saying can explain better than me
remiX #10
Posted 13 November 2012 - 10:38 AM
Okay so the door works fine both opening and closing without setting anything else off but none of the other commands work :S Have I got too many ends! I really dont know, sorry.

Post the latest code you have now, please.
billysback #11
Posted 13 November 2012 - 10:41 AM
think of it like this:
if opens a "statement", end will close this statement.

placing an "else" in the "statement" will split the statement in two, but only by two, with else there is only two possible solutions, so two "sub-statements", if you want it call it that.

placing and "elseif" in the "statement" will split the statement in to however many elseif, else and if's there are in the statement.
examples:

if check then
<statement>
end
1 statement.


if check then
<statement>
else
<statement>
end
2 statements


if check then
<statement>
elseif check2 then
<statement>
elseif check3 then
<statement>
else
<statement>
end
2 elseif + 1 else + 1 if = 4 statements.
Sarcastic_Bannana #12
Posted 13 November 2012 - 10:45 AM
This is my latest code:

-- Definitions
blue = colors["blue"]
green = colors["green"]
purple = colors["purple"]
white = colors["white"]
yellow = colors["yellow"]
orange = colors["orange"]
i = 1
--Basic Startup
rednet.open("top")
-- Loop
while i == 1 do
id,message = rednet.receive()
-- Reactor Door Open
if message == "rdo1" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") + green)
end
end

--Reactor Door Close
if message == "rdo0" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") - green)
end
end
--Lights On
if message == "l1" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") + orange)
end
end

--Lights Off
if message == "l0" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") - orange)
end
end

--Cooling On
if message == "c1" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") - blue)
end
end

--Cooling Off
if message == "c0" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") + blue)
end
end

-- reactor Startup
if message == "rsu" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") + yellow)
sleep(5)
rs.setBundledOutput("right", rs.getBundledOutput("right") + white)
end
end

-- Reactor Shutdown
if message == "shu" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") - yellow)
rs.setBundledOutput("right", rs.getBundledOutput("right") - white)
end

-- Emergency Shutdown
if message == "emgcc1" then do
rs.setBundledOutput("right", rs.getBundledOutput("right") + purple)
rs.setBundledOutput("right", rs.getBundledOutput("right") - white)
rs.setBundledOutput("right", rs.getBundledOutput("right") - blue)
rs.setBundledOutput("right", rs.getBundledOutput("right") + green)
rednet.reieve()
  if message == "emgcc0" then do
  os.reboot()
  break end
  end
end

elseif message == "break" then do
break end
end
end
end
billysback #13
Posted 13 November 2012 - 10:51 AM
I don't understand what you don't understand any more, if you want someone to fix your code; I did that
If you want someone to explain if statements, we definitely did that.

o.o
Sarcastic_Bannana #14
Posted 13 November 2012 - 11:01 AM
Well the programe still doesnt work. Every time I turn the lights on the door open, everytime I turn the cooling off the door open and the lights come on.
The script is bugged but I cant see how.
Thats what I don't understand.
billysback #15
Posted 13 November 2012 - 11:09 AM
are you using the code you posted a couple of posts up?
Sarcastic_Bannana #16
Posted 13 November 2012 - 11:22 AM
Yes, both the script a few posts up (I undersatnd what you said about then and do and if'ss and elseif's I just havent updated yet) and the one you suggested both produce exactly the same result when I run them.

Because your code is much cleaner though we should probably work from that one.
billysback #17
Posted 13 November 2012 - 11:36 AM
I don't think colors["color"] will work as colors is not a table, it is a script, or API, and the colors are all integer values preset in the API.

otherwise, I think any errors happening are happening in the code sending the messages…

by the way, instead of id, message = rednet.recieve() you could try doing

event, p1, p2, p3 = os.pullEvent()
if event == "rednet_message" then
local id = p1
local message = p2
local dist = p3
<code>
end

also, you aren't actually using your while safety catch (i == 1), try using that instead of or as well as break;
Lyqyd #18
Posted 13 November 2012 - 02:04 PM
I don't think colors["color"] will work as colors is not a table, it is a script, or API, and the colors are all integer values preset in the API.

This is incorrect. APIs are loaded as tables, and are indexed as tables. The colors are set as values in the API and accessed through the table of the loaded API. This is why they're usually accessed through colors.blue or similar. Notice the table.key notation. colors["blue"] would work just as well.
Sarcastic_Bannana #19
Posted 15 November 2012 - 12:11 AM
Problem solved.

Just did away with bundled cables and attached wireless transmitters to every face of the computer.
Not quite as tidy but don't suppose anyones gonna see it.

Thanks