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

Question about looping?

Started by skillx423, 25 April 2016 - 09:53 PM
skillx423 #1
Posted 25 April 2016 - 11:53 PM
i have a fairly basic digging program that lookes something like this.


shell.run('clear')
print("how many blocks should i go down?")
local x = io.read()
for i = 1, x do
CheckFuel()
Mine()
Stairs()
end

Yes i have functions setup for Mine() Stairs() CheckFuel() and Torch(). (didnt inclue because i wanted this post to stay short and simple.)

my question is how do i make it so every 5 loops it will send Torch()?
Bomb Bloke #2
Posted 26 April 2016 - 12:58 AM
Inside the loop, you could add a check:

if x % 5 == 0 then Torch() end

… where % is the modulus operator, returning the remainder of x divided by 5, in this instance.
Anavrins #3
Posted 26 April 2016 - 12:59 AM
Inside the for loop, add if i % 5 == 0 then Torch() end
i%5 == 0 basically means the statement is going to be true when i is a multiple of 5.
Edited on 26 April 2016 - 12:31 AM
Bomb Bloke #4
Posted 26 April 2016 - 01:47 AM
Gah, yes, it would need to be "i", not "x".
skillx423 #5
Posted 26 April 2016 - 02:07 AM
Thanks! Worked Great :D/>
lieven121 #6
Posted 01 May 2016 - 02:08 AM
i have a fairly basic digging program that lookes something like this.


shell.run('clear')
print("how many blocks should i go down?")
local x = io.read()
for i = 1, x do
CheckFuel()
Mine()
Stairs()
end

Yes i have functions setup for Mine() Stairs() CheckFuel() and Torch(). (didnt inclue because i wanted this post to stay short and simple.)

my question is how do i make it so every 5 loops it will send Torch()?
off topic but if you use shell.run("clear") to clear you could also use term.clear()

and I suggest using this it prevents a error when you would type a word

term.clear()
print("how many blocks should i go down?")
local x = io.read()
if type(x) == "number" then
for i = 1, x do
  CheckFuel()
  Mine()
  Stairs()
end
else
term.setCursorPos(1,2)
print("please use a number")
end