2

I am trying to keep a 1-10 count and if the count goes over 10 it starts back at 1

Tick := 5;
currentTick := 8;

Now FinalTick is going to be CurrentTick + Tick but once the value is 10 it should stat over at 1 thus in this case

5 + 8 = 3

How do I do this?

Glen Morse
  • 2,437
  • 8
  • 51
  • 102

4 Answers4

5

If you want count to be 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4 etc, you can:

procedure IncCount(var ACount: Integer);
begin
  ACount := (ACount + 1) mod 10;
end;

If you want count to be 1,2,3,4,5,6,7,8,9,10,1,2,3,4 etc, you can:

procedure IncCount(var ACount: Integer);
begin
  ACount := ACount mod 10 + 1;
end;
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
5

I would use the so called modulo operator ( http://en.wikipedia.org/wiki/Modulo_operation).

FinalTick := ( CurrentTick + Tick ) mod 10;

jcklie
  • 4,054
  • 3
  • 24
  • 42
3

You should use modulo operator, such as

Val:=(5+8) mod 10
seeker
  • 3,255
  • 7
  • 36
  • 68
2

MOD operator is a solution as most answers here.

Sometimes this may help:

procedure Foo;
var I: Integer;
begin
  I := 0;
  repeat
    I := I + 1;
    // Do your stuff here
    ShowMessage(IntToStr(I));
    // Add some exit conition, like:
    // if (..) then break;
    if I = 10 then I := 0;
  until False;
end;
Marcodor
  • 4,578
  • 1
  • 20
  • 24