Jump to content




Loop a whole program?

turtle game computer

1 reply to this topic

#1 a;jkosdf

  • New Members
  • 1 posts

Posted 12 June 2014 - 03: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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 June 2014 - 03: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users