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

Restart a count after reaching a certain value?

Started by mistamadd001, 03 May 2014 - 02:32 AM
mistamadd001 #1
Posted 03 May 2014 - 04:32 AM
I would like to setup a count system that only counts up to 8 then on the next count returns to 1, if you could put a reset counter in as well that would be great so for every 8 counts the counter resets to 1 and adds 1 to say globalCount

I have tried using while true do, but I seem to be using it in the wrong way.

guidance appreciated.
HometownPotato #2
Posted 03 May 2014 - 06:44 AM
globalCount = 0

while true do
globalCount = globalCount + 1
if globalCount == 8 then
globalCount = 0
print("Done")
end
sleep(0)
end
mistamadd001 #3
Posted 03 May 2014 - 06:56 AM
yea I actually figured out why my program wasn't working, I kept forgetting to use the == instead of =, thanks anyway
OczkoSX #4
Posted 03 May 2014 - 09:00 AM
You can use use:

for i=1,8 do
  -- what you want to do
end
Engineer #5
Posted 03 May 2014 - 02:59 PM
You can use use:

for i=1,8 do
  -- what you want to do
end
But that runs only once, use this:

while true do
   for i = 1, 8 do

   end
   -- Make the loop yield here, there are different ways of doing that
end
Edited on 03 May 2014 - 12:59 PM
Sir_Mr_Bman #6
Posted 03 May 2014 - 11:18 PM
Unless I'm thinking about it wrong, couldn't you just…


for i = 1, 8 do
  -- Your code here.
  if i == 8 then
    i = 1
  end
end
OczkoSX #7
Posted 03 May 2014 - 11:31 PM
Unless I'm thinking about it wrong, couldn't you just…


for i = 1, 8 do
  -- Your code here.
  if i == 8 then
	i = 1
  end
end

No, because it will repeat 8 times, var's value is unnecessary
HometownPotato #8
Posted 04 May 2014 - 05:13 AM
for i = 1, math.huge do
local counter = (i % 8) + 1;
–code
end
Engineer #9
Posted 04 May 2014 - 10:09 AM
for i = 1, math.huge do
local counter = (i % 8) + 1;
–code
end
That will stop at some point, use a combination of a for loop and a while loop, where the while loop is infinite
Bomb Bloke #10
Posted 04 May 2014 - 10:40 AM
If you're keeping track of how many total iterations have been performed, eventually the counter variable's going to overflow anyways. Though quite frankly I'm surprised "math.huge" wouldn't crash "for" loops from the get-go.

I suppose this topic's horse has been thoroughly smushed into the ground by now, but rather than taking the modulus of "i" (an operation that gets slower and slower as "i" increases), you could run a binary AND on it:

local counter = bit.band(i,7) + 1
HometownPotato #11
Posted 04 May 2014 - 06:19 PM
It would stop after like, > 2^1000 something iterations, which is not really a big deal.
blipman17 #12
Posted 04 May 2014 - 07:35 PM
It would stop after like, > 2^1000 something iterations, which is not really a big deal.

just pulled the following of the internet.

as compiled by default, the Number is a double, on most compilers that's an IEEE 64-bit floating point. that means 10bit exponent, so the maximum number is roughly 2^1024, or 5.6e300 years. that's a long time.

Edit: they were talking about incrementing a number once everey second.
Edited on 04 May 2014 - 05:36 PM
HometownPotato #13
Posted 04 May 2014 - 07:39 PM
What? I didn't pull anything "off the internet."
I just know from testing that it was about that number.
Bomb Bloke #14
Posted 05 May 2014 - 03:07 AM
He didn't say you did - he was referring to himself.
HometownPotato #15
Posted 05 May 2014 - 04:57 AM
Yeah, I realized that right after I posted after re-reading what he posted. Just forgot to edit.
mistamadd001 #16
Posted 05 May 2014 - 07:02 AM
@lyqyd can you lock this please and thankyou :D/>
Lyqyd #17
Posted 05 May 2014 - 03:25 PM
Threads in Ask a Pro generally don't get locked, they're simply allowed to age down off the page.