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

[Q] Multiple conditions for loops.

Started by Mrkalo981, 27 December 2012 - 12:28 AM
Mrkalo981 #1
Posted 27 December 2012 - 01:28 AM
Hi everyone I have just started programming for Computercraft and I am trying to write an automated strip mine program. It is fairly basic so far (I plan to make it more complicated later) but I have a couple of questions regarding loops:

1. How to you put multiple conditions in for loops? In this for loop I have for the turtle returning when it is full (I has been counting up as it dug the tunnel). I want it to have 2 conditions, 1 being the counting up to I (so If there is no chest it will not mine for ever in the other direction) and another for checking whether the block in front of it is not a chest - turtle.compare(). How do I put both conditions in a single loop?

2. How do I reverse the boolean output of turtle.compare()


for num=0, I do
turtle.dig()
turtle.digDown()
turtle.forward()
end

Thank you for helping and sorry if the awsner is already out there. Looked through the tutorials and could not find it.
remiX #2
Posted 27 December 2012 - 01:34 AM
1. Have you tried using an if statement?
if condition 1 and condition 2 then
-- code
end

2. boolean = not turtle.compare()
Mrkalo981 #3
Posted 27 December 2012 - 01:57 AM
Would this work using functions?

I = A

function stuff()
if turtle.compare() then
A = 1
else

turtle.dig()
turtle.forward()

end

for num= 1, A do
stuff()
end

Thank you for reading.

Edit: Reworked code with separate variable and if statement within the function.
remiX #4
Posted 27 December 2012 - 02:35 AM
Yeah it will, but you forgot an 'end' to end the function.


function stuff()
  if turtle.compare() then -- so if it compare returns true, it will make A = 1, else it will dig and move forward
    A = 1
  else
    turtle.dig()
    turtle.forward()
  end

  for num= 1, A do
    stuff()
  end
end

PS: Use code tags :)/>
[*code]code here[€/code]
Mrkalo981 #5
Posted 27 December 2012 - 03:07 AM
Thank you very much :)/>