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

how do i Make a endless loop

Started by protexxi, 09 October 2012 - 01:37 PM
protexxi #1
Posted 09 October 2012 - 03:37 PM
i have this code

x = 64
if x <5 then
getfuel()
refuel()
tunnel()
end
else if x >5
getfuel()
refuel()
down()
empty()
end
end


how would i make this loop endlessly?
sjele #2
Posted 09 October 2012 - 03:40 PM

while true do
--code here
end
billysback #3
Posted 09 October 2012 - 03:41 PM
I would put in a variable which controls the loop though, so that you can cancel it, like

local isOn = true
while isOn do
--code here
--if something then isOn = false end
end
protexxi #4
Posted 09 October 2012 - 03:44 PM

x = 64
while true do
if x <5 then
getfuel()
refuel()
tunnel()
end
else if x >5
getfuel()
refuel()
down()
empty()
end
end
end

dose not work
protexxi #5
Posted 09 October 2012 - 03:46 PM
bios:206: [string "mine"]:67: 'end' expected (to close 'while' at line 61)
sjele #6
Posted 09 October 2012 - 03:47 PM
You haver to many ends, and missuse of elseif


x = 64
while true do
if x <5 then
getfuel()
refuel()
tunnel()
elseif x >5 then
getfuel()
refuel()
down()
empty()
end
end
I made that part work,,, dident see edit, you need to add a end to the while loop

while true do
end
Orwell #7
Posted 09 October 2012 - 03:48 PM

x = 64
while true do
if x <5 then
getfuel()
refuel()
tunnel()
end
else if x >5
getfuel()
refuel()
down()
empty()
end
end
end

dose not work

If you use 'else if' then don't close the previous 'if then' with an end. So it would be like this:

x = 64
while true do
  if x <5 then
	getfuel()
	refuel()
	tunnel()
  elseif x >5 then
	getfuel()
	refuel()
	down()
	empty()
  end
end

Also, 'else if' needs a 'then' after it. You also had an 'end' to many at the bottom.

EDIT: I've been ninja'd like a pro >.<
EDIT2: sjele forgot the 'then' after the 'else if' line though
Lyqyd #8
Posted 09 October 2012 - 03:51 PM

x = 64
while true do
if x <5 then
getfuel()
refuel()
tunnel()
end
else if x >5
getfuel()
refuel()
down()
empty()
end
end
end

dose not work

If you use 'else if' then don't close the previous 'if then' with an end. So it would be like this:

x = 64
while true do
  if x <5 then
	getfuel()
	refuel()
	tunnel()
  else if x >5 then
	getfuel()
	refuel()
	down()
	empty()
  end
end

Also, 'else if' needs a 'then' after it. You also had an 'end' to many at the bottom.

EDIT: I've been ninja'd like a pro >.<
EDIT2: sjele forgot the 'then' after the 'else if' line though

It's "elseif" when used that way, not "else if".
sjele #9
Posted 09 October 2012 - 03:52 PM

x = 64
while true do
if x <5 then
getfuel()
refuel()
tunnel()
end
else if x >5
getfuel()
refuel()
down()
empty()
end
end
end

dose not work

If you use 'else if' then don't close the previous 'if then' with an end. So it would be like this:

x = 64
while true do
  if x <5 then
	getfuel()
	refuel()
	tunnel()
  elseif x >5 then
	getfuel()
	refuel()
	down()
	empty()
  end
end

Also, 'else if' needs a 'then' after it. You also had an 'end' to many at the bottom.

EDIT: I've been ninja'd like a pro >.<
EDIT2: sjele forgot the 'then' after the 'else if' line though

x = 64
while true do
if x <5 then
getfuel()
refuel()
tunnel()
end
else if x >5
getfuel()
refuel()
down()
empty()
end
end
end

dose not work

If you use 'else if' then don't close the previous 'if then' with an end. So it would be like this:

x = 64
while true do
  if x <5 then
	getfuel()
	refuel()
	tunnel()
  elseif x >5 then
	getfuel()
	refuel()
	down()
	empty()
  end
end

Also, 'else if' needs a 'then' after it. You also had an 'end' to many at the bottom.

EDIT: I've been ninja'd like a pro >.<
EDIT2: sjele forgot the 'then' after the 'else if' line though


derp, fixed in post :P/>/> Thanks.
protexxi #10
Posted 09 October 2012 - 04:01 PM
it works fine now cheers guys just one more thing


function getfuel()
turtle.select(15)
turtle.placeUp()
turtle.select(14)
turtle.suckUp(5)
turtle.select(15)
turtle.digUp()
end
function refuel()
	    if turtle.getFuelLevel() < 10 then
	    turtle.select(14)
	    turtle.refuel(1)
	    end
  end
function digup()
turtle.digUp()
turtle.up
x= x+1()
end
x=0
while x <= 63 do
getfuel()
refuel()
digUp90
end
end

has a error on 18
sjele #11
Posted 09 October 2012 - 04:12 PM

x= x+1()
So when did you define the answer of x+1 to be a function?
it's supposed to be x + 1
Doyle3694 #12
Posted 09 October 2012 - 04:17 PM
you don't need a variable to close the while loop,
break
is alot easier
protexxi #13
Posted 09 October 2012 - 04:20 PM
is working now was a few missing () where my shit didnt work thats all


function refuel()
	    if turtle.getFuelLevel() < 10 then
	    turtle.select(14)
	    turtle.refuel(1)
	    end
  end
function down()
turtle.digDown()
turtle.turnLeft()
turtle.dig()
turtle.turnRight(2)
turtle.dig()
turtle.turnLeft()
turtle.dig()
shell.run("tunnel", "5")
turtle.back (4)
x = x-1
end
function tunnel ()
getfuel()
turle.refuel()
shell.run ("tunnel", "10")
end
function empty()
turtle.select(16) turtle.placeUp()
turtle.select(2) turtle.dropUp()
turtle.select(3) turtle.dropUp()
turtle.select(4) turtle.dropUp()
turtle.select(5) turtle.dropUp()
turtle.select(6) turtle.dropUp()
turtle.select(7) turtle.dropUp()
turtle.select(8) turtle.dropUp()
turtle.select(9) turtle.dropUp()
turtle.select(10) turtle.dropUp()
turtle.select(11) turtle.dropUp()
turtle.select(12) turtle.dropUp()
turtle.select(13) turtle.dropUp()
turtle.select(14) turtle.dropUp()
turtle.select(1) turtle.dropUp()
turtle.select(16)
turtle.digUp()
endfunction getfuel()
turtle.select(15)
turtle.placeUp()
turtle.select(14)
turtle.suckUp(5)
turtle.select(15)
turtle.digUp()
end
print "you need 2 ender chest slots 15 and 16 and 3 fuel in 14"
x = 64
while true do
  if x <5 then
    getfuel()
    refuel()
    tunnel()
  else if x >5 then
    getfuel()
    refuel()
    down()
    empty()
  end
end
end


has bugs that i cnt find/fix anyone test it for me?
Doyle3694 #14
Posted 09 October 2012 - 04:25 PM

turtle.select(2) turtle.dropUp()
turtle.select(3) turtle.dropUp()
turtle.select(4) turtle.dropUp()
turtle.select(5) turtle.dropUp()
turtle.select(6) turtle.dropUp()
turtle.select(7) turtle.dropUp()
turtle.select(8) turtle.dropUp()
turtle.select(9) turtle.dropUp()
turtle.select(10) turtle.dropUp()
turtle.select(11) turtle.dropUp()
turtle.select(12) turtle.dropUp()
turtle.select(13) turtle.dropUp()
turtle.select(14) turtle.dropUp()
turtle.select(1) turtle.dropUp()

Can be
for i=2,14 do
   turtle.select(1)
   turtle.dropUp()
end

turtle.select(1)
turtle.dropUp()
btw, please indentate your code abit more, making blanklines is part of indentation just as well as tabs(I don't really know nif it's part of indentations but hell yeah it makes your code more readable).

But yeah, overall great code, you'll understand after getting more skilled what can be looped instead and so on and so forth
Lyqyd #15
Posted 09 October 2012 - 07:44 PM

turtle.select(2) turtle.dropUp()
turtle.select(3) turtle.dropUp()
turtle.select(4) turtle.dropUp()
turtle.select(5) turtle.dropUp()
turtle.select(6) turtle.dropUp()
turtle.select(7) turtle.dropUp()
turtle.select(8) turtle.dropUp()
turtle.select(9) turtle.dropUp()
turtle.select(10) turtle.dropUp()
turtle.select(11) turtle.dropUp()
turtle.select(12) turtle.dropUp()
turtle.select(13) turtle.dropUp()
turtle.select(14) turtle.dropUp()
turtle.select(1) turtle.dropUp()

Can be
for i=2,14 do
   turtle.select(1)
   turtle.dropUp()
end

turtle.select(1)
turtle.dropUp()
btw, please indentate your code abit more, making blanklines is part of indentation just as well as tabs(I don't really know nif it's part of indentations but hell yeah it makes your code more readable).

But yeah, overall great code, you'll understand after getting more skilled what can be looped instead and so on and so forth

The use of indentation is indenting, not indentating. "Indentate" is not a word.
Doyle3694 #16
Posted 10 October 2012 - 06:19 PM
Sorry for not being native english speaker lol
Klausar #17
Posted 10 October 2012 - 06:41 PM
An easy way would be:

y = 1
repeat
-- code here --
until y == 2
end