Posted 25 January 2015 - 03:52 PM
Hi guys,
I have something that I find a little confusing with some of my code - I've read over the computercraft wiki, through the tutorials I can find here but I haven't found the answer to this yet (although it's completely possible I've missed it like a derp)
So, to put this in persepctive, I'm doing a tutorial on biofuel and because I find big systems fun to work with I'm controlling the flow to each biorector based on how full they are - for this I'm using openCCsensors. So that I could explain how all that worked, I set up a little program to show them in action and I had an error cropping up to tell me that I was missing an "end" statement for the while loop.
I indent my coding, so I know I wasn't missing one, and although it was simple fix and I can see what is happening, I am intrigued as to why it is happening.
So, here's an overview of the code I'm using for a while loop (I've taken out a lot of the gubbins to make it easier to read)
If I remove the "else" statement then the second "end" isn't needed and it's obvious that it isn't registering the end for that if+elseif+else when the variable is changed to true. I've looked over the computercraft wiki and every example I've found of something using if, else if and else all at once only have a single "end" statement for them.
I hope I'm not being an eedjit, but from my understanding whether lua is using a "while" or a "do while" loop (I've honestly not had a reason to check into this as nothing I do at the moment matters either way), the "exit_loop ~= true" should be evaluated either before the internal code is executed or after, it shouldn't be quitting out of the loop at the point of the variable being switched to true?
So it should be either:
Start loop, check exit_loop ~= true, execute code, check exit_loop ~= true, execute code, etc, etc (while)
or
Start loop, execute code, check exit_loop ~= true, execute code, check exit_loop ~= true, etc, etc (do while)
anyway, it's nothing serious or major, I'd just be interested to know what's going on there so I can adapt the code without having to have a second "elseif" with a ton of conditions on it. Plus the second end is annoying the hell out of me (I know, that's purely aesthetic and I'm being anal about it)
Thanks for any help or information people :)/>
I have something that I find a little confusing with some of my code - I've read over the computercraft wiki, through the tutorials I can find here but I haven't found the answer to this yet (although it's completely possible I've missed it like a derp)
So, to put this in persepctive, I'm doing a tutorial on biofuel and because I find big systems fun to work with I'm controlling the flow to each biorector based on how full they are - for this I'm using openCCsensors. So that I could explain how all that worked, I set up a little program to show them in action and I had an error cropping up to tell me that I was missing an "end" statement for the while loop.
I indent my coding, so I know I wasn't missing one, and although it was simple fix and I can see what is happening, I am intrigued as to why it is happening.
So, here's an overview of the code I'm using for a while loop (I've taken out a lot of the gubbins to make it easier to read)
while exit_loop ~= true do
os.startTimer(1)
local event, a, b, c = os.pullEvent()
if event == "timer" then
screen_display(selection)
elseif event == "monitor_touch" then
local width, height = monitor.getSize()
if b<11 and c<= #keys then
selection = c
screen_display(selection)
elseif b<= 4 and c == height then
monitor.clear()
monitor.setCursorPos(1,1)
monitor.write("You don't want to do that dave")
os.sleep(2.0)
monitor.clear()
exit_loop = true
else
selection = 0
screen_display(selection)
end
end
end
end --this is the second end I have to put in
If I remove the "else" statement then the second "end" isn't needed and it's obvious that it isn't registering the end for that if+elseif+else when the variable is changed to true. I've looked over the computercraft wiki and every example I've found of something using if, else if and else all at once only have a single "end" statement for them.
I hope I'm not being an eedjit, but from my understanding whether lua is using a "while" or a "do while" loop (I've honestly not had a reason to check into this as nothing I do at the moment matters either way), the "exit_loop ~= true" should be evaluated either before the internal code is executed or after, it shouldn't be quitting out of the loop at the point of the variable being switched to true?
So it should be either:
Start loop, check exit_loop ~= true, execute code, check exit_loop ~= true, execute code, etc, etc (while)
or
Start loop, execute code, check exit_loop ~= true, execute code, check exit_loop ~= true, etc, etc (do while)
anyway, it's nothing serious or major, I'd just be interested to know what's going on there so I can adapt the code without having to have a second "elseif" with a ton of conditions on it. Plus the second end is annoying the hell out of me (I know, that's purely aesthetic and I'm being anal about it)
Thanks for any help or information people :)/>