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

How do i loop this?

Started by CardingiSFun, 19 October 2012 - 10:22 PM
CardingiSFun #1
Posted 20 October 2012 - 12:22 AM
if xr == "run" then

print("The process will start in 5 seconds")
sleep(5)
print("Pulsing White")
rs.setBundledOutput("back", colors.white)
sleep(3)
rs.setBundledOutput("back", 0)

end

So i was wondering how do i make this repeate
Shazz #2
Posted 20 October 2012 - 12:29 AM

xr = "run"

while xr == "run" do
   print("The process will start in 5 seconds")
   sleep(5)
   print("Pulsing White")
   rs.setBundledOutput("back", colors.white)
   sleep(3)
   rs.setBundledOutput("back", 0)
end
darkrising #3
Posted 20 October 2012 - 03:01 AM
or just…

while true do
   print("The process will start in 5 seconds")
   sleep(5)
   print("Pulsing White")
   rs.setBundledOutput("back", colors.white)
   sleep(3)
   rs.setBundledOutput("back", 0)
end

or do you mean just the pulsing part?

   print("The process will start in 5 seconds")
   sleep(5)
   print("Pulsing White")
while true do
   rs.setBundledOutput("back", colors.white)
   sleep(3)
   rs.setBundledOutput("back", 0)
end

Although that wouldn't pulse it would just stay on because there is no pause between on and off.

   print("The process will start in 5 seconds")
   sleep(5)
   print("Pulsing White")
while true do
   sleep(1.5)
   rs.setBundledOutput("back", colors.white)
   sleep(1.5)
   rs.setBundledOutput("back", 0)
end
Lettuce #4
Posted 20 October 2012 - 03:55 AM
I assume you don't want it running for infinity, if you have a if xr == "run" then check, a for loop works for a set period, like so:

if xr == "run" then
   print("The process will start in 5 seconds")
	sleep(5)
	print("Pulsing White")
   for i = 1, 5 do
	   rs.setBundledOutput("back", colors.white)
		 sleep(3)
	   rs.setBundledOutput("back", 0)
   end
end
The "i" variable is local only to that code block, you can name it whatever you like, "i" is the most common. You can even have multiple for i = 1, x do code blocks in the same code. It also accepts variables, like if you wanted to request an amount of iterations. Like the one above. It works if x is global, and set to a number. My personal favorite loop.
ChunLing #5
Posted 20 October 2012 - 09:00 AM
Hmm…I don't really have a favorite loop. But I like repeat until a lot compared to how much other people seem to use it. If you want to let a loop run until you do something to terminate it, repeat until is usually a good choice. While is for when you want to be able to skip a loop entirely if you don't need it. for is when you want the loop to execute a definite number of times based on the program state when the loop starts.