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

loops

Started by reiman2222, 22 June 2012 - 09:16 PM
reiman2222 #1
Posted 22 June 2012 - 11:16 PM
this is my code



print "how many items"
local i = tonumber(read())
counter = 0
while counter ~= i do
counter = counter + 1
redstone.setOutput("right",true)
sleep(1)
redstone.setOutput("right",false)
end

this program is meant to despent the number of items you specify. but it wont work and i dont know why.
reiman2222 #2
Posted 22 June 2012 - 11:21 PM
and when i run this program the redstone stays on and the loop dosent end like its sopose too
Zalerinian #3
Posted 22 June 2012 - 11:27 PM
perhaps try this…


print "how many items"
local i = read()
i = tonumber(i)
counter = 0
while counter ~= i do
counter = counter + 1
rs.setOutput("right",true)
sleep(1)
rs.setOutput("right",false)
end
Calculator #4
Posted 22 June 2012 - 11:32 PM
You need to sleep after turning off the output as well, otherwise it gets immediately turned on again in the next iteration.


while counter ~= i do
counter = counter + 1
redstone.setOutput("right",true)
sleep(1)
redstone.setOutput("right",false)
sleep(1)
end
Zalerinian #5
Posted 22 June 2012 - 11:34 PM
You need to sleep after turning off the output as well, otherwise it gets immediately turned on again in the next iteration.


while counter ~= i do
counter = counter + 1
redstone.setOutput("right",true)
sleep(1)
redstone.setOutput("right",false)
sleep(1)
end

hmm, quite disappointed in myself for missing this.
reiman2222 #6
Posted 22 June 2012 - 11:35 PM
it works thank you
KaoS #7
Posted 23 June 2012 - 09:46 PM
why does everyone do the long way around for a simple loop? sorry I just don't see any advantages in it. why not just

for i=1,tonumber(count) do

end

is there any problems with this method?
MysticT #8
Posted 23 June 2012 - 09:52 PM
why does everyone do the long way around for a simple loop? sorry I just don't see any advantages in it. why not just

for i=1,tonumber(count) do

end

is there any problems with this method?
No, it's just that some people understands better that way. The for loop is pretty easy, but can be confusing when learning.