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

Set number of loops

Started by Early_Sep_99_Baby, 21 December 2012 - 09:39 AM
Early_Sep_99_Baby #1
Posted 21 December 2012 - 10:39 AM
I probably sound like a huge noob, but is there any way to have a loop of code be repeated a set number of times. I know i could just write it out several times, but I really don't have the patience to write of my code 10 times in a row. if there is a solution to my problem, it would be greatly appreciated.

Thank you
Goof #2
Posted 21 December 2012 - 10:48 AM
use :


print("How many times would you repeat?")
write(" ")
times = tonumber(read())
for i = 1, times do
term.clear()
term.setCursorPos(1,1)
print(i)
sleep(1)
end

its the "for" loop you are looking for:D
you can also just use:

for i = 1, 10 do -- this will loop until i (1) = 10 --(it increments with +1 per time)
term.clear()
term.setCursorPos(1,1)
print(i)
sleep(0.5)

you can also do this ( a bit more advanced :D/>)


for K = 10, 1, -1 do -- this loop will start at 10, and then DEcrement with 1 (-1) until it reaches the "1"
term.clear()
term.setCursorPos(1,1)
print(K)
sleep(0.2)
end


if that did NOT help you that much, then look at the Tutorials section :D/>

i hope i could help :D/>
:D/>
remiX #3
Posted 21 December 2012 - 11:27 AM
You can also jump in 2's, 3's, 4's etc:

for i = 1, 100, 3 do  -- will jump every 3 times, so first 5 would be = 1, 4, 7, 10, 13
  print(i)
end
AndreWalia #4
Posted 21 December 2012 - 11:29 AM
You can also jump in 2's, 3's, 4's etc:

for i = 1, 100, 3 do  -- will jump every 3 times, so first 5 would be = 1, 4, 7, 10, 13
  print(i)
end
I never new that :P/>