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

Loop a whole program?

Started by a;jkosdf, 12 June 2014 - 02:05 AM
a;jkosdf #1
Posted 12 June 2014 - 04:05 AM
I have a code and I want it to loop completely again, so I don't have to write the program 5 times.
Here is the code I have currently

for i = 1, 5 do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
end
for i = 1, 1 do
  turtle.turnLeft()
  turtle.dig()
  turtle.digUp()
  turtle.forward()
  turtle.turnLeft()
end
for i = 1, 5 do
  turtle.dig()
  turtle.forward()
end
for i =  1, 1 do
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.turnRight()
end
What I want to do is repeat this code all over again. I guessed i would just put an i = x, y do statement, but that didn't work. I dont know what else i could do, so if anyone could help, that would be nice :)/>
This is supposed to clear an area, just in case you want to know
theoriginalbit #2
Posted 12 June 2014 - 04:33 AM
okay so I'm not completely sure you understand how loops work. Especially due to the following


for i = 1, 1 do
  --# code
end

that code will run once, the format of a for loop is as follows

for variable_name = initial_value, value_limit [, optional_increment] do
  --# code
end

meaning that doing for i = 1, 1 do tells the loop to run until i equals 1, which is once.

now to run all that code 5 times, you just simply put it inside of a for loop as well, like so

for y = 1, 5 do
  for i = 1, 5 do
    turtle.dig()
    turtle.forward()
    turtle.digUp()
  end


  --# removed the needless loop
  turtle.turnLeft()
  turtle.dig()
  turtle.digUp()
  turtle.forward()
  turtle.turnLeft()


  for i = 1, 5 do
    turtle.dig()
    turtle.forward()
  end

  --# removed the needless loop

  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.turnRight()
end